MediaSmart = new Object();
MediaSmart.Utilities = new Object();
MediaSmart.ads = new Array();


MediaSmart.site = "TEL.BIGPOND.NEWS.NEWS_WEATHER";
MediaSmart.areaPattern = "NEWS_WEATHER.BIGPOND.NEWS.$";
MediaSmart.subArea = "OTHER";


//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.remarketing = true;


MediaSmart.Utilities.initialise = function(){
	if (arguments.length > 0) {
		var zmsJson = arguments[0];
		if (zmsJson.area != "" && typeof(zmsJson.area) == "string") {
			var zmsString = MediaSmart.Utilities.makeURLSafe(zmsJson.area);
		   	MediaSmart.subArea = zmsString.toUpperCase();
		}
		if (zmsJson.site != ""&& typeof(zmsJson.site) == "string") {
			var zmsString = MediaSmart.Utilities.makeURLSafe(zmsJson.site);
			MediaSmart.site = zmsString.toUpperCase();
		}
		if(zmsJson.tags != null){
			MediaSmart.tags = zmsJson.tags;
		}
	}

	//generate area
	MediaSmart.area = MediaSmart.areaPattern.replace(/\$/g,MediaSmart.subArea);


	MediaSmart.pageId = MediaSmart.Utilities.random();
	if ((document.location + "").indexOf("https://") != -1){
		MediaSmart.domain = "https://media.sensis.com.au";
		MediaSmart.protocol = "https";
		MediaSmart.Utilities.remarketing = false;
	}else{
		MediaSmart.domain = "http://media.sensis.com.au";
		MediaSmart.protocol = "http";
	}

	if ((document.location + "").indexOf("MSTOOTHPASTE") != -1) {
		MediaSmart.keyword = "MSTOOTHPASTE";
	}
	if ((document.location + "").indexOf("zmstestingenvironment375") != -1) {
		MediaSmart.site = "Z_ZORRO";
		MediaSmart.area = "MOB.TEST.DEMO";
	}
}

MediaSmart.newAd = function(json){
	if(MediaSmart.Utilities.aboveBelowFold == false){
		json.abf = false;
	}
	if(json.width == 1 || json.height == 1){
		json.polite = false;
	}
	if((json.width == 300 && json.height == 250)){
    	var politeDecider = (Math.floor(Math.random()*100));
    	var expandPercent = 50;
    	if(politeDecider < expandPercent){
	    	json.polite = 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;margin:auto;'></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 (MediaSmart.Utilities.remarketing == true && ((ad.width == 300 && ad.height == 250) || (ad.width == 728 && ad.height == 90) || ((ad.width == 160 || ad.width == 120)&& ad.height == 600)))
		 	adCallString += "src='http://ad.sensismediasmart.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();
	}

	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;
	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, "_");
	
	
	//add "ARTICLE" keyword for BP news article pages
	if ((document.location + "").indexOf("/articles/") != -1) {
	    if(adCallString.indexOf("/KEYWORD=") != -1)
    		adCallString += "+ARTICLE";
    	else
    		adCallString += "/KEYWORD=ARTICLE";
	}
    if(!polite){
	    if(adCallString.indexOf("/KEYWORD=") != -1)
    		adCallString += "+EXPANDABLE";
    	else
    		adCallString += "/KEYWORD=EXPANDABLE";
    }
	
	for (key in MediaSmart.tags) {
		if (typeof(ad.tags[key]) == "undefined") {
			if (MediaSmart.tags[key] != "")
				adCallString += "/" + MediaSmart.Utilities.makeURLSafe(key.toUpperCase()) + "=" + MediaSmart.Utilities.makeURLSafe(MediaSmart.tags[key].toUpperCase());
		}
	}

	adCallString += "/pageid=" + MediaSmart.pageId;
	if (polite)
		adCallString += "'></iframe>\n";
	else
		adCallString += "'></script>\n";

	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;
			return MediaSmart.ads[i];
		}
	}
	return false;
}
MediaSmart.Utilities.refreshAd = function(){
	MediaSmart.insertAd(MediaSmart.ads);
}

MediaSmart.insertAllAds = function(){
	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;
		}
		document.getElementById("zMSplacement" + (i + 1)).innerHTML = MediaSmart.Utilities.generateAdCall(MediaSmart.ads[i]);
	}
}


MediaSmart.Utilities.makeURLSafe = function(text){
	text = text.replace(/ |-|<|>|\/|\?|\+/g, "_");
	text = text.replace(/\&AMP\;/g, "AND");
	text = text.replace(/&/g, "AND");
	text = text.replace(/_+/g, "_");
	text = text.replace(/^_|_$/g, "");
	return text;
}


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>");
	}
}



/* LEGACY CODE */


var zMSRefreshRate = 60;
var zMSAdCount = 0;
var zMSAdCallArray = new Array();
//setInterval("zMSRefreshAds()", (zMSRefreshRate * 1000));


function zMSRefreshAds(){
	zMSInsertAds();
}


function zMSMakeAdMarker(zMSmyStr){ 
	if (zMSRefreshRate > 0) {//TODO possibly remove ad statement
		
		zMSAdCallArray[zMSAdCount] = zMSmyStr;
		document.write("<div id='zMSAdPlacment" + zMSAdCount + "'></div>");
		document.getElementById("zMSAdPlacment" + zMSAdCount).innerHTML = zMSGenerateAdCall(zMSAdCount);
		zMSAdCount++;
	}
	
}
function zMSGenerateAdCall(index){
	if(zMSkey.indexOf("EXPANDABLE") != -1){
		zMSkey = "";
	}
	
	var zMSmyStr = zMSAdCallArray[index];

	if (typeof(zMSsection) == "undefined") 
			zMSsection = "";
		if (zMSlocation.indexOf("http://www.bigpond.com/news/business/") != -1 && zMSmyStr.indexOf("300x250") != -1) {
			zMSgetJS("adsize=1x1&position=hidden", "businesscookie");
		}
		
		var zMSposMod = "";
		//Check if position 1
		if (zMSmyStr.indexOf("position=1") != -1) {
			zMSposMod = "/POSITION=1";
		}
		//Check if ad is a second position
		if (zMSmyStr.indexOf("position=2") != -1) {
			zMSposMod = "/POSITION=2";
		}
		else {
			//Check if ad is a thrid position
			if (zMSmyStr.indexOf("position=3") != -1) {
				zMSposMod = "/POSITION=3";
			}
		}
		//Check if an advertorial position
		if (zMSmyStr.indexOf("position=advertorial") != -1) {
			zMSposMod = "/POSITION=ADVERTORIAL";
		}
			
		if (zMSsection == "portal" || zMSsection == "news" || zMSmyStr.indexOf("134x1000") != -1) {
			zMSpos = zMSgetValue("position", zMSmyStr);
			if (zMSpos == "island") 
				zMSposMod = "/POSITION=1";
			if (zMSpos == "island2") 
				zMSposMod = "/POSITION=2";
			if (zMSpos == "island3") 
				zMSposMod = "/POSITION=3";
		}
		var e = document.getElementsByTagName("div");
		var uname = "";
		
		
		zMSkey = "";
		if (arguments.length == 2) 
			zMSkey = "/KEYWORD=" + arguments[1].toUpperCase();
		if (zMSsection == "custompages") 
			zMSkey = "/KEYWORD=" + zMSsub_section.toUpperCase();
		if (zMSmyStr.indexOf("amex478sponsor") != -1) 
			zMSkey = "/KEYWORD=AMEX478SPONSOR";
		if (zMSkey.length == 9) 
			zMSkey = "";
		
		
		//add "ARTICLE" keyword for BP news article pages
		if ((zMSlocation.indexOf("/news/") != -1 && zMSlocation.indexOf("/content/") != -1) || ((document.location + "").indexOf("/articles/") != -1)) {
			if (zMSkey == "") {
				zMSkey = "/KEYWORD=ARTICLE";
			}
			else {
				zMSkey += "+ARTICLE";
			}
		}
		if (zMSmyStr.indexOf("navbar") != -1) {
			return "<iframe scrolling='no' allowtransparency='yes' frameborder='0' marginheight='0' marginwidth='0' vspace='0'" + " src=\"" + zMSHome + "/hserver/acc_random=" + zMSRand() + "/SITE=" + zMSACCsite + "/AREA=" + zMSACCarea + uname + "/AAMSZ=194x55" + zMSkey + "/pageid=" + zMSpagenum + zMSposMod +"\">"+ "</iframe>";
		}
		else {
			if (zMSmyStr.indexOf("300x250") != -1 && Math.random() * 100 < 20) {
				var zMSdaVals = 'acc_random=' + zMSRand() + '/SITE=' + zMSACCsite + '/AREA=' + zMSACCarea + zMSkey + uname + '/AAMSZ=' + zMSconvertSize(zMSgetValue("adsize", zMSmyStr), zMSgetValue("position", zMSmyStr)) + zMSkey + '/pageid=' + zMSpagenum + zMSposMod;
				return '<iframe src="http://ad.sensismediasmart.com.au/images/sensis/cookieFix.html?' + escape(zMSdaVals.replace(/\//gi, "&")) + '" allowtransparency="true" width=300 height=250 frameborder="no" MARGINWIDTH="0" MARGINHEIGHT="0" SCROLLING="no">\n</iframe>\n';
			}
			else 
				if (zMSconvertSize(zMSgetValue("adsize", zMSmyStr), zMSgetValue("position", zMSmyStr)).indexOf("728x90") != -1 && Math.random() * 100 < 50) {
					var zMSdaVals = 'acc_random=' + zMSRand() + '/SITE=' + zMSACCsite + '/AREA=' + zMSACCarea + zMSkey + uname + '/AAMSZ=' + zMSconvertSize(zMSgetValue("adsize", zMSmyStr), zMSgetValue("position", zMSmyStr)) + zMSkey + '/pageid=' + zMSpagenum + zMSposMod;
					return '<iframe src="http://ad.sensismediasmart.com.au/images/sensis/cookieFix.html?' + escape(zMSdaVals.replace(/\//gi, "&")) + '" allowtransparency="true" width=728 height=90 frameborder="no" MARGINWIDTH="0" MARGINHEIGHT="0" SCROLLING="no">\n</iframe>\n';
				}
				else {
					
					var size = zMSconvertSize(zMSgetValue("adsize", zMSmyStr)) + "";
					size = size.split("x");
					
					return "<iframe width='"+size[0]+"' height='"+size[1]+"' scrolling='no' allowtransparency='yes' frameborder='0' marginheight='0' marginwidth='0' vspace='0'" + " src=\"" + zMSHome + "/hserver/acc_random=" + zMSRand() + "/SITE=" + zMSACCsite + "/AREA=" + zMSACCarea + uname + "/AAMSZ=" + zMSconvertSize(zMSgetValue("adsize", zMSmyStr), zMSgetValue("position", zMSmyStr)) + zMSkey + "/pageid=" + zMSpagenum + zMSposMod + "\">" + "</iframe>";
				}
		}
}

function zMSInsertAds(){

	for(var i = 0; i < zMSAdCount; i++){
		var objId = "zMSAdPlacment" + i;
		var obj = document.getElementById(objId);
		obj.innerHTML = zMSGenerateAdCall(i);
	}
}