MediaSmart = new Object();
MediaSmart.Utilities = new Object;
MediaSmart.ads = new Array();

MediaSmart.site = "TEL.AFL_HOME.SPORT";//Insert site here
MediaSmart.areaPattern = "SPORT.AFL_HOME.AFL_$";//Insert area pattern here
MediaSmart.subArea = "OTHER"; //Default area
//Are ads polite loaded? Default is true
MediaSmart.Utilities.polite = true;
//Above and Below the fold targeting. Default is false
MediaSmart.Utilities.aboveBelowFold = true;

MediaSmart.Utilities.initialise = function(){
	if (arguments.length > 0) {
		var zmsJson = arguments[0];
		if (typeof(zmsJson.area) == "string" && zmsJson.area != "") {
			var zmsString = zmsJson.area;
			MediaSmart.subArea = zmsString.toUpperCase();
		}
		if (typeof(zmsJson.site) == "string" && zmsJson.site != "") {
			var zmsString = zmsJson.site;
			if(zmsString == "AFL"){
				MediaSmart.site = "TEL.AFL_HOME.SPORT";
			}
		}
		if (typeof(zmsJson.age) == "string" && zmsJson.age != "") {
			if(typeof(MediaSmart.tags)=="undefined"){MediaSmart.tags = new Array();}
			var zmsString = zmsJson.age;
			MediaSmart.tags['age'] = zmsString.toUpperCase();
		}
		if (typeof(zmsJson.gender) == "string" && zmsJson.gender != "") {
			if(typeof(MediaSmart.tags)=="undefined"){MediaSmart.tags = new Array();}
			var zmsString = zmsJson.gender;
			MediaSmart.tags['gender'] = zmsString.toUpperCase();
		}
		if (typeof(zmsJson.team) == "string" && zmsJson.team != "") {
			if(typeof(MediaSmart.tags)=="undefined"){MediaSmart.tags = new Array();}
			var zmsString = zmsJson.team;
			MediaSmart.tags['trid'] = zmsString.toUpperCase();
		}

if (zmsJson.keyword != ""&& typeof(zmsJson.keyword) == "string") {
	        var zmsString = zmsJson.keyword;
	        zmsString = MediaSmart.Utilities.makeURLSafe(zmsString);
	        MediaSmart.keyword = zmsString.toUpperCase();
   	 	}
    

		
	}	
	
	var tmpStringArray = new Array();
	tmpStringArray = MediaSmart.areaPattern.split("$");
	var zmsOutput = "";
	for(var i = 0; i < tmpStringArray.length - 1; i++) {
		zmsOutput += tmpStringArray[i] + MediaSmart.subArea;
	}
	zmsOutput += tmpStringArray[tmpStringArray.length - 1];
	MediaSmart.area = zmsOutput;
	
	
	MediaSmart.pageId = MediaSmart.Utilities.random();
	if ((document.location + "").indexOf("https://") != -1){
		MediaSmart.domain = "https://media.sensis.com.au";
		MediaSmart.protocol = "https";		
	}else{
		MediaSmart.domain = "http://media.sensis.com.au";
		MediaSmart.protocol = "http";	
	}

	if ((document.location + "").indexOf("MSTOOTHPASTE") != -1) {
		MediaSmart.keyword = "MSTOOTHPASTE";
	}
if ((document.location + "").indexOf("/ladder/tabid/74/default.aspx") != -1) {
		MediaSmart.area= "SPORT.AFL_HOME.AFL_LADDER";
	}
if ((document.location + "").indexOf("www.afl.com.au/fixture/") != -1) {
		MediaSmart.area= "SPORT.AFL_HOME.AFL_FIXTURE";
	}	
if ((document.location + "").indexOf("www.afl.com.au/development/nabaflrisingstarsprogram/nabafldraftcamp/draftcampnews/tabid/13527/default.aspx") != -1) {
		MediaSmart.area= "SPORT.AFL_HOME.AFL_COMBINE";
	}	
if ((document.location + "").indexOf("www.afl.com.au/nab%20afl%20draft%20combine/tabid/780/default.aspx") != -1) {
		MediaSmart.area= "SPORT.AFL_HOME.AFL_COMBINE";
	}
if ((document.location + "").indexOf("zmstestingenvironment375") != -1) {
		MediaSmart.site = "Z_ZORRO";
		MediaSmart.area = "MOB.TEST.DEMO";
	}
if ((document.location + "").indexOf("draftmachine.afl.com.au") != -1) {
		MediaSmart.area= "SPORT.AFL_HOME.AFL_DRAFTMACHINE";
	}	

//Tag for Unleash The Giesch page
if ((document.location + "").indexOf("www.afl.com.au/Video/UnleashTheGiesch/tabid/17455/Default.aspx") != -1) {
		MediaSmart.area= "SPORT.AFL_HOME.UNLEASH_THE_GIESCH";
	}

//Tag for Teams Live
if((document.location + "").indexOf("tabid/16469") != -1 || (document.location + "").indexOf("tabid=16469") != -1){
		MediaSmart.area = "SPORT.AFL_HOME.TEAMS_LIVE";
}

//Tag for AFL Playground
if((document.location + "").indexOf("tabid/17405") != -1 || (document.location + "").indexOf("tabid=17405") != -1){
		MediaSmart.area = "SPORT.AFL_HOME.PLAYGROUND";
}

//Area hack for AFL News 
if (MediaSmart.area=="SPORT.AFL_HOME.AFL_NEWSVIEWS"){
		MediaSmart.area="SPORT.AFL_HOME.AFL_NEWS";
	} 

	//AFL Final Ladder Hack
if((document.location + "").indexOf("tabid/74") != -1 || (document.location + "").indexOf("tabid=74") != -1){
		if(typeof(MediaSmart.keyword) != "undefined" && MediaSmart.keyword != ""){
			MediaSmart.keyword += "+LADDERPAGE";
		}else{
			MediaSmart.keyword = "LADDERPAGE";
		}
	}
	//Match Centre Hack
if((document.location + "").indexOf("tabid/16931") != -1 || (document.location + "").indexOf("tabid=16931") != -1){
		if(typeof(MediaSmart.keyword) != "undefined" && MediaSmart.keyword != ""){
			MediaSmart.keyword += "+MATCH_CENTRE";
		}else{
			MediaSmart.keyword = "MATCH_CENTRE";
		}
	}

         //predictor Hack
if((document.location + "").indexOf("tabid/13046") != -1 || (document.location + "").indexOf("tabid=13046") != -1){
		if(typeof(MediaSmart.keyword) != "undefined" && MediaSmart.keyword != ""){
			MediaSmart.keyword += "+PREDICTOR";
		}else{
			MediaSmart.keyword = "PREDICTOR";
		}
	}

	
	
//Whats your decision hack
    if(document.location == "http://www.afl.com.au/video/whatsyourdecision/tabid/17455/default.aspx"){
		if(typeof(MediaSmart.keyword) != "undefined" && MediaSmart.keyword != ""){
			MediaSmart.keyword += "+WHATS_YOUR_DECISION";
		}else{
			MediaSmart.keyword = "WHATS_YOUR_DECISION";
		}
	}

	
}

MediaSmart.newAd = function(json){
	if(MediaSmart.Utilities.aboveBelowFold == false){
		json.abf = false;
	}
	
	// Make 1x1 non polite
	if( json.width == 1 && json.height == 1 && (document.location + "").indexOf("http://www.afl.com.au/tabid/16931/Default.aspx#fixtureid=5458&tab=Preview") != -1)
		json.polite = false;
	
	if (json.width == "300" && json.height== "70"){
		json.polite = false;
		json.abf = false;
		if (json.position =="bppromo1"){
			json.site = "TEL.BPPROMO.AFL_HOME.SPORT";
			json.position = 1;
		}
		if (json.position =="bppromo2"){
			json.site = "TEL.BPPROMO.AFL_HOME.SPORT";
			json.position = 2;
		}
	}
	if ((document.location + "").indexOf("news/newsarticle/tabid/208/newsid/107377") != -1 ||
	(document.location + "").indexOf("news/jilllindsaytribute") != -1 ||
	(document.location + "").indexOf("news/newsarticle/tabid/208/newsid/107379") != -1 ||
	(document.location + "").indexOf("news/newsarticle/tabid/208/newsid/107384") != -1 ||
	(document.location + "").indexOf("news/newsarticle/tabid/208/newsid/107426") != -1 || 
	(document.location + "").indexOf("cms.afl.com.au") != -1){
		if(json.width=="4"&&json.height=="1"){
			return;
		}
	}
	if ((typeof(json.polite) != "undefined" && json.polite == false) || MediaSmart.Utilities.polite == false) {
		document.write(MediaSmart.Utilities.generateAdCall(json));
	} else {
		MediaSmart.ads[MediaSmart.ads.length] = json;
		document.write("<div id='zMSplacement" + MediaSmart.ads.length + "' style='width:" + json.width + "px; height:" + json.height + "px;'></div>");
	}
	

	
}


MediaSmart.Utilities.random = function(){
	return Math.round(Math.abs(Math.sin((new Date()).getTime())) * 1000000000000);
}

MediaSmart.setRefresh = function(time){
	if (time > 0) {
		setInterval("MediaSmart.Utilities.refreshAd()", (time * 1000));
	}
}

MediaSmart.Utilities.generateAdCall = function(ad){
	if (typeof(ad.cmd) == "function") 
		ad.cmd();
	if (typeof(ad.tags) == "undefined") 
		ad.tags = new Array();

	var polite = true;
	if ((typeof(ad.polite) != "undefined" && ad.polite == false) || MediaSmart.Utilities.polite == false) 
		polite = false;
	
	if (polite) {
		adCallString = "<iframe scrolling='no' allowtransparency='yes' frameborder='0' marginheight='0' marginwidth='0' vspace='0' ";
		
		if (ad.iframeHeight != "" && typeof(ad.iframeHeight) != "undefined") 
			adCallString += "height='" + ad.iframeHeight + "' ";
		else 
			adCallString += "height='" + ad.height + "' ";
		if (ad.iframeWidth != "" && typeof(ad.iframeWidth) != "undefined") 
			adCallString += "width='" + ad.iframeWidth + "' ";
		else 
			adCallString += "width='" + ad.width + "' ";
			// redirect to cookiefix for islands leaderboard and skys
		if ((ad.width == 300 && ad.height == 250) || (ad.width == 728 && ad.height == 90) || ((ad.width == 160 || ad.width == 120)&& ad.height == 600))
		 	adCallString += "src='" + MediaSmart.protocol + "://medrx.sensis.com.au/images/sensis/cookieFix.html?acc_random=" + MediaSmart.Utilities.random();
		else
			adCallString += "src='" + MediaSmart.domain + "/hserver/acc_random=" + MediaSmart.Utilities.random();
	}
	else {
		adCallString = "<scr" + "ipt src='" + MediaSmart.domain + "/jserver/acc_random=" + MediaSmart.Utilities.random();
	}
	
pathArray = window.location.pathname;
	if(pathArray.indexOf("newsarticle/tabid/208/newsid/89061") != -1){
		MediaSmart.site = "TEL.AFL_HOME.SPORT";
MediaSmart.area = "SPORT.AFL_HOME.AFL_DREAMTEAM";
MediaSmart.keyword = "AFL_DREAMTEAM";
}

	if (ad.site != "" && typeof(ad.site) != "undefined") 
		adCallString += "/SITE=" + ad.site;
	else 
		adCallString += "/SITE=" + MediaSmart.site;
	if (ad.area != "" && typeof(ad.area) != "undefined") 
		adCallString += "/AREA=" + ad.area;
	else 
		adCallString += "/AREA=" + MediaSmart.area;

	if( ad.width=="300" && ad.height=="250" && ad.position == "other"){
		ad.width = 300;
		ad.height = 300;
	}	

	adCallString += "/AAMSZ=" + ad.width + "x" + ad.height;
	

	if (ad.position != "" && typeof(ad.position) != "undefined") {
		adCallString += "/POSITION=";
		if (ad.abf != false) {
			if (MediaSmart.innerHeight - ad.top - 90 > 0 || typeof(ad.top) == "undefined") {
				adCallString += "ABV";
			}
			else {
				adCallString += "BLW";
			}
		}
		adCallString += ad.position;
	}


	if (ad.keyword != "" && typeof(ad.keyword) != "undefined") {
		adCallString += "/KEYWORD=" + ad.keyword.toUpperCase().replace(/\s+|\//g, "_");
	}
	else {
		if (MediaSmart.keyword != "" && typeof(MediaSmart.keyword) != "undefined") {
			adCallString += "/KEYWORD=" + MediaSmart.keyword.toUpperCase().replace(/\s+|\//g, "_");
		}	
	}
	

	// News landing page
	if( (document.location+"") == "tabid/72" ) {
		if( adCallString.indexOf( "/KEYWORD=" ) == -1 ) {
			adCallString += "/KEYWORD=LANDING";			
		}
		else {
			adCallString += "+LANDING";
		}
	}
	
	// Ladder page
	if( (document.location+"") == "tabid/74" ) {
		if( adCallString.indexOf( "/KEYWORD=" ) == -1 ) {
			adCallString += "/KEYWORD=LADDERPAGE";			
		}
		else {
			adCallString += "+LADDERPAGE";
		}
	}
	
	
	if (MediaSmart.state != "" && typeof(MediaSmart.state) != "undefined") {
		adCallString += "/STATE=" + MediaSmart.state.toUpperCase();
	}
	
//Pauls array
	
	clubsArray={
	"ADELAIDE CROWS" : "CROWS",
	"BRISBANE LIONS" : "LIONS",
	"CARLTON" : "BLUES",
	"COLLINGWOOD" : "MAGPIES",
	"ESSENDON" : "BOMBERS",
	"FREMANTLE" : "DOCKERS",
	"GEELONG CATS" : "CATS",
	"GOLD COAST SUNS" : "SUNS",
	"GREATER WESTERN SYDNEY GIANTS" : "GIANTS",
	"HAWTHORN" : "HAWKS",
	"NORTH MELBOURNE" : "KANGAROOS",
	"MELBOURNE" : "DEMONS",
	"PORT ADELAIDE" : "PORTPOWER",
	"RICHMOND" : "TIGERS",
	"ST KILDA" : "SAINTS",
	"SYDNEY SWANS" : "SWANS",
	"WEST COAST EAGLES" : "EAGLES",
	"WESTERN BULLDOGS" : "BULLDOGS"
	}
	
var y=""	
	
	for (key in MediaSmart.tags) {
		if (typeof(ad.tags[key]) == "undefined") {
			if( key == "CLUB" && MediaSmart.tags[key] != "" ) {
			
			y=MediaSmart.tags[key]
			
			//Pauls for loop for the array hack
				for(key in clubsArray){
					if(y == key){
						y = clubsArray[key];
					}
				}
					
			MediaSmart.tags[key] = y;
			
			adCallString += "/SITE_CONTENT=" + MediaSmart.tags[key].toUpperCase().replace(/\s+|\//g, "_");
				
			}
			else if (MediaSmart.tags[key] != "") {
				adCallString += "/" + key.toUpperCase().replace(/\s+|\//g, "_") + "=" + MediaSmart.tags[key].toUpperCase().replace(/\s+|\//g, "_");
			}
		}
	}

	

	if((document.location + "").indexOf("MSTOOTHPASTE") != -1){
		adCallString += (document.location + "").slice((document.location + "").lastIndexOf("MSTOOTHPASTE")+12);
	}	
	
	
	
	adCallString += "/pageid=" + MediaSmart.pageId;
	if (polite) 
		adCallString += "'></iframe>\n";
	else 
		adCallString += "'></script>\n";
		
	// Add 1x1 to tipping section
/*	if(((document.location+"").indexOf("tipping.afl.com.au")!=-1 || (document.location + "").indexOf("tipping.afl.com.au")!=-1) && ad.width == 300 ) {
        ad.width = 1;
        ad.height = 1;
        ad.iframeWidth = 1;
        ad.iframeHeight = 1;
        return adCallString + MediaSmart.Utilities.generateAdCall(ad);  
    }
    else
*/        return adCallString;

}

MediaSmart.insertAd = function(ad, elementId){
	var element = "";
	if (typeof(ad) == "string") {
		ad = MediaSmart.getAdById(ad);
		if (ad) {
			if (elementId == "" || typeof(elementId) == "undefined") {
				if (document.getElementById("zMSplacement" + MediaSmart.Utilities.adIndex) != null) 
					element = document.getElementById("zMSplacement" + MediaSmart.Utilities.adIndex);
				else 
					element = document.getElementById(ad.containerId);
			}
			else {
				if (document.getElementById(elementId)) 
					element = document.getElementById(elementId);
			}
			
			element.innerHTML = MediaSmart.Utilities.generateAdCall(ad);
		}
	}
	else {
		if (typeof(ad) == "object") {
			if (typeof(ad.length) === 'number' && !(ad.propertyIsEnumerable('length')) && typeof ad.splice === 'function') {
				for (i = 0; i < ad.length; i++) {
					if (document.getElementById(ad[i].containerId)) 
						document.getElementById(ad[i].containerId).innerHTML = MediaSmart.Utilities.generateAdCall(ad[i]);
				}
			}
			else 
				document.getElementById(ad.containerId).innerHTML = MediaSmart.Utilities.generateAdCall(ad);
		}
	}
}

MediaSmart.getAdById = function(id){
	for (i = 0; i < MediaSmart.ads.length; i++) {
		if (MediaSmart.ads[i].id == id) {
			MediaSmart.Utilities.adIndex = i+1;
			return MediaSmart.ads[i];
		}
	}
	return false;
}
MediaSmart.Utilities.refreshAd = function(){
	MediaSmart.insertAd(MediaSmart.ads);
}

MediaSmart.insertAllAds = function(){
if (((document.location + "").indexOf("sdmtestid") != -1)) {var fileref=document.createElement('script');fileref.setAttribute("type","text/javascript");fileref.setAttribute("src", "https://media.sensis.com.au/images/houseads_sensis/sdm_testsite.js");document.getElementsByTagName("head")[0].appendChild(fileref);}else{

	zmsCount = 0;
	if(arguments.length > 0){
		zmsCount = arguments[0];
	}
	
	if(typeof(window.innerWidth) == 'number')
	{
		MediaSmart.innerHeight = window.innerHeight;
	}else if(document.documentElement && document.documentElement.clientHeight){
		MediaSmart.innerHeight = document.documentElement.clientHeight;
	}else if(document.body && document.body.clientHeight){
		MediaSmart.innerHeight = document.body.clientHeight;
	}
	
	for (i = zmsCount; i < MediaSmart.ads.length; i++) {
		element = document.getElementById("zMSplacement" + (i + 1));
		MediaSmart.ads[i].top = element.offsetTop;
		while (element = element.offsetParent) {
			MediaSmart.ads[i].top += element.offsetTop;
		}

		//brownlow news reaction
		if(MediaSmart.ads[i].width=="620"&&MediaSmart.ads[i].height=="90" && MediaSmart.area=="SPORT.AFL_HOME.AFL_NEWS_REACTION"){
			MediaSmart.ads[i].height="60";
			document.getElementById("zMSplacement" + (i + 1)).style.height="60px";
		}






		document.getElementById("zMSplacement" + (i + 1)).innerHTML = MediaSmart.Utilities.generateAdCall(MediaSmart.ads[i]);
	}
}

}

//Only add for Bigpond Sites
MediaSmart.bigpondPromotions = new Object();
MediaSmart.bigpondPromotions.newAd = function(json){
	json.site = MediaSmart.site.replace(/BIGPOND/i, "BPPROMO");
	json.abf = false;
	if ((typeof(json.polite) != "undefined" && json.polite == false) || MediaSmart.Utilities.polite == false) {
		document.write(MediaSmart.Utilities.generateAdCall(json));
	}
	else {
		MediaSmart.ads[MediaSmart.ads.length] = json;
		document.write("<div id='zMSplacement" + MediaSmart.ads.length + "' style='width:" + json.width + "px; height:" + json.height + "px;'></div>");
	}
}



document.write('<scr'+'ipt type="text/javascript" src="http://media.sensis.com.au/jserver/acc_random='+MediaSmart.Utilities.random()+'/PAGEID='+MediaSmart.Utilities.random()+'/AAMSZ=IADVIDEO/SITE=Z_ZORRO/KEYWORD=SETGUID"></scr'+'ipt>');


