if (typeof(MediaSmart) != "object") {
	MediaSmart = new Object;
	MediaSmart.ads = new Array();

//PreDefined site and area -- Customize for each site
if (typeof(MediaSmart.site) == "undefined" || MediaSmart.site == "") 
	MediaSmart.site = "SEN.CITYSEARCH.ENTERTAINMENT";
	
var posarray = new Array();
	
MediaSmart.newAd = function(json){
		if (typeof(json.polite) != "undefined" && json.polite == false) {
			document.write(MediaSmart.Utilities.generateAdCall(json));
		}
		else if(json.id.toLowerCase() == "brick"){
			var brickpercent = 0;
			
			var sizedecider = (Math.floor(Math.random()*100));
			if(brickpercent < sizedecider){
				MediaSmart.newAd({width: 300, height: 250, position: 1, id: "300x250_1"});
			}
			else {
				MediaSmart.newAd({width: 300, height: 600, position: 1, id: "300x600_1"});
			}
		}
		else if(json.id.toLowerCase() == "tower_wsky"){
			var towerpercent = 10;
			var skypercent = 30;
			var sizedecider = (Math.floor(Math.random()*100));
			if(towerpercent > sizedecider){
				MediaSmart.newAd({width: 120, height: 240, position: 1, id: "120x240_1"});
				document.write("<div style='height:20px;width:120' ></div>");
				MediaSmart.newAd({width: 120, height: 240, position: 2, id: "120x240_2"});
			}
			else if(towerpercent + skypercent > sizedecider){
				MediaSmart.newAd({width: 120, height: 600, position: 1, id: "120x600_1"});
			}
			else
				MediaSmart.newAd({width: 160, height: 600, position: 1, id: "160x600_1"});
		}
		else if(json.id.toLowerCase() == "halfpage"){
			var halfpagepercent = 30;
			
			var sizedecider = (Math.floor(Math.random()*100));
			if(halfpagepercent < sizedecider){
				MediaSmart.newAd({width: 300, height: 250, position: 1, id: "300x250_1"});
				document.write("<div style='height:20px;width:300' ></div>");
				MediaSmart.newAd({width: 300, height: 250, position: 2, id: "300x250_2"});
			}
			else {
				MediaSmart.newAd({width: 300, height: 600, position: 1, id: "300x600_1"});
			}
		}
			
		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>");
		}
		if (json.width == 150 && json.height == 200) {
			document.getElementById("zMSplacement" + MediaSmart.ads.length).style.display = "none";
		}
		jsonsize = json.width + "_" + json.height;
		adposition = 0;
		posarray.push(jsonsize);
		for (i = 0; i < posarray.length; i++) {
			if (posarray[i] == jsonsize) {
				adposition ++;
			}
		}
		json.position = adposition;
	}
}
numbertoCname = function(cit){	
	if(cit == "1")
		cit = "canberra";
	else if(cit == "2")
		cit = "sydney";	
	else if(cit == "3")
		cit = "melbourne";
	else if(cit == "4")
		cit = "brisbane";
	else if(cit == "5")
		cit = "adelaide";
	else if(cit == "6")
		cit = "perth";
	else if(cit == "7")
		cit = "hobart";
	else if(cit == "8")
		cit = "darwin";
	else if(cit == "9")
		cit = "cairns";
	else if(cit == "10")
		cit = "goldcoast";
	else if(cit == "11")
		cit = "sunshinecoast";
	else if(cit == "12")
		cit = "centralcoast";
	else if(cit == "13")
		cit = "wollongong";
	else if(cit == "14")
		cit = "townsville";
	else 
		cit = "nat";
	return cit;
}

MediaSmart.Utilities = new Object;
MediaSmart.Utilities.initialise = function(){
	MediaSmart.pageId = MediaSmart.Utilities.random();
	if ((document.location + "").indexOf("https://") != -1) 
		MediaSmart.domain = "https://media.sensis.com.au";
	else 
		MediaSmart.domain = "http://media.sensis.com.au";
}
MediaSmart.Utilities.random = function(){
	return Math.round(Math.abs(Math.sin((new Date()).getTime())) * 1000000000000);
}

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)
		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 + "' ";
		// adCallString += "src='" + MediaSmart.domain + "/hserver/acc_random=" + MediaSmart.Utilities.random();
		
		// 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='http://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();
	}
	
	if ((document.location + "").indexOf("MSTOOTHPASTE") != -1) {
		MediaSmart.tags.keyword = "MSTOOTHPASTE";
	}
	if ((document.location + "").indexOf("zmstestingenvironment375") != -1) {
		MediaSmart.site = "Z_ZORRO";
		MediaSmart.tags.area = "MOB.TEST.DEMO";
	}
	
	
	if (ad.site != "" && typeof(ad.site) != "undefined") 
		adCallString += "/SITE=" + ad.site;
	else if (typeof(MediaSmart.site) != "undefined" && MediaSmart.site != "")
		adCallString += "/SITE=" + MediaSmart.site;
	else adCallString += "/SITE=SEN.CITYSEARCH.ENTERTAINMENT"
		
	if (ad.area != "" && typeof(ad.area) != "undefined") {
		adCallString += "/AREA=" + ad.area;
		
		}
	else if (typeof(MediaSmart.tags.area) != "undefined" && MediaSmart.tags.area != ""){ 
		adCallString += "/AREA=" + MediaSmart.tags.area.toUpperCase();
	}
	else
		adCallString += "/AREA=ENTERTAINMENT.CITYSEARCH" ;
		
	if (ad.city != "" && typeof(ad.city) != "undefined") 
		adCallString += "." + ad.city;
	else if (MediaSmart.tags.city !="" && typeof(MediaSmart.tags.city) != "undefined")
		adCallString += "." + numbertoCname(MediaSmart.tags.city).toUpperCase();
	else adCallString += ".NATIONAL";
	
	if (ad.section != "" && typeof(ad.section) != "undefined") 
		adCallString += "." + ad.section;		 
	else if (MediaSmart.tags.section !="" && typeof(MediaSmart.tags.section) != "undefined"){
		if(MediaSmart.tags.section.toUpperCase() == "MOVIES")
			adCallString += ".FILM";
		else
			adCallString += "." + MediaSmart.tags.section.toUpperCase(); 
	}
	else adCallString += ".HOME";
	
	if (ad.subsection != "" && typeof(ad.subsection) != "undefined" && (ad.section != ad.subsection)) 
		adCallString += "." + ad.subsection;		 
	else if (MediaSmart.tags.subsection !="" && typeof(MediaSmart.tags.subsection) != "undefined" && (MediaSmart.tags.subsection != MediaSmart.tags.section)){
		if ((document.location+"").indexOf("action=viewTVGuide")!=-1)
			adCallString += ".GUIDE";
		else{
			MediaSmart.tags.subsection = MediaSmart.Utilities.makeURLSafe(MediaSmart.tags.subsection);
			adCallString += "." + MediaSmart.tags.subsection.toUpperCase(); 
		}
	}
	else adCallString += "" ; 
			adCallStringTemp = adCallString;
	
	adCallString += "/AAMSZ=" + ad.width + "x" + ad.height;
	

	if(polite){
	if(window.innerHeight-ad.top-90>0||typeof(ad.top)=="undefined")
		adCallString += "/POSITION=ABV";
	else
		adCallString += "/POSITION=BLW";
	}
	if (ad.position != "" && typeof(ad.position) != "undefined" && polite) 	 
		adCallString += ad.position;
	else if (ad.position != "" && typeof(ad.position) != "undefined")
		adCallString += "/POSITION=" + ad.position;

	
	pathArray = window.location.pathname;
	if((pathArray.indexOf("Moonlight+Cinema") != -1) || (pathArray.indexOf("Moonlight Cinema") != -1) || (pathArray.indexOf("moonlight") != -1))
		adCallString += "/KEYWORD=MOONLIGHT";

	else if (ad.keyword != "" && typeof(ad.keyword) != "undefined") 
		adCallString += "/KEYWORD=" + ad.keyword.toUpperCase();
	else if (MediaSmart.tags.yp_heading != "" && typeof(MediaSmart.tags.yp_heading) != "undefined") 
			adCallString += "/KEYWORD=" + MediaSmart.tags.yp_heading.toUpperCase();
	else if (MediaSmart.tags.keyword != "" && typeof(MediaSmart.tags.keyword) != "undefined") 
			adCallString += "/KEYWORD=" + MediaSmart.tags.keyword.toUpperCase();
			
	if(MediaSmart.tags.genre != "" && MediaSmart.tags.genre != "undefined")
		adCallString += "/GENRE=" + MediaSmart.Utilities.makeURLSafe(MediaSmart.tags.genre);
	if(MediaSmart.tags.rating != "" && MediaSmart.tags.rating != "undefined")
		adCallString += "/AGE=" + MediaSmart.tags.rating.toUpperCase();
	if(MediaSmart.tags.pcode != "" && MediaSmart.tags.pcode != "undefined")
		adCallString += "/PCODE=" + MediaSmart.tags.pcode;
	if(MediaSmart.tags.suburb != "" && MediaSmart.tags.suburb != "undefined"){
		MediaSmart.tags.suburb = MediaSmart.Utilities.makeURLSafe(MediaSmart.tags.suburb);
		adCallString += "/SUBURB=" + MediaSmart.tags.suburb.toUpperCase();
	}
		if (typeof(ad.tags) != "undefined" && ad.tags != "") {
			for (key in ad.tags) {
				adCallString += "/" + key.toUpperCase() + "=" + ad.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.Utilities.getAdById(ad);
		if (ad) {
			if (elementId == "" || typeof(elementId) == "undefined") 
				element = document.getElementById("zMSplacement" + MediaSmart.Utilities.adIndex);
			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.Utilities.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.insertAllAds = function(){
	MediaSmart.extraTargeting();
	for (i = 0; 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.hyperLocal();
}


MediaSmart.Utilities.makeURLSafe = function(text){
	text = text.toUpperCase();
	text = text.replace(/^\s*|\s*$/g,'');
	text = text.replace(/ |-|<|>|\/|\?|\+/g, "_");
	text = text.replace(/\&AMP\;/g, "AND");
	text = text.replace(/&/g, "AND");
	text = text.replace(/,/g , "_");
	text = text.replace(/_+/g, "_");
	text = text.replace(/^_|_$/g, "");
	return text;
}

MediaSmart.extraTargeting = function(){
	var zMScateg = "";
	if ((document.location + "").indexOf("/restaurants/") != -1 || (document.location + "").indexOf("/arts/") != -1 || (document.location + "").indexOf("/bars_clubs/") != -1){
		try{var infoPane = document.getElementById("sidebar");}catch(e){}
		try{var infoElements = infoPane.getElementsByTagName("p");}catch(e){}
	}
		
	if ((document.location + "").indexOf("/restaurants/") != -1){
		if(typeof(infoElements) != "undefined"){
			for(var a=0; a<infoElements.length - 1; a++){
				if(infoElements[a].innerHTML == "Cuisine:"){
					zMScateg = MediaSmart.Utilities.makeURLSafe(infoElements[a+1].innerHTML);
					if(MediaSmart.tags.genre == '' && zMScateg != ""){
						MediaSmart.tags.genre = MediaSmart.Utilities.makeURLSafe(infoElements[a+1].innerHTML);
					}
				}
			}
		}
	}
	if ((document.location + "").indexOf("/arts/") != -1){
		if(typeof(infoElements) != "undefined"){
			for(var a=0; a<infoElements.length - 1; a++){
				if(infoElements[a].innerHTML.indexOf("Event Type:") != -1){
					zMScateg = MediaSmart.Utilities.makeURLSafe(infoElements[a+1].innerHTML);
					if(MediaSmart.tags.genre == '' && zMScateg != ""){
						MediaSmart.tags.genre = MediaSmart.Utilities.makeURLSafe(infoElements[a+1].innerHTML);
					}
				}
			}
		}
	}
	if ((document.location + "").indexOf("/bars_clubs/") != -1){
		if(typeof(infoElements) != "undefined"){
			for(var a=0; a<infoElements.length - 1; a++){
				if(infoElements[a].innerHTML == "Type:"){
					zMScateg = MediaSmart.Utilities.makeURLSafe(infoElements[a+1].innerHTML);
					if(MediaSmart.tags.genre == '' && zMScateg != ""){
						MediaSmart.tags.genre = MediaSmart.Utilities.makeURLSafe(infoElements[a+1].innerHTML);
					}
				}
			}
		}
	}
}

MediaSmart.hyperLocal = function(){
	if(MediaSmart.tags.pcode.search(/^\d{4}$/) != -1){
		try{
			hyperFrame = document.createElement('iframe');
			hyperFrame.id = "hyperFrame";
			hyperFrame.width = "0";
			hyperFrame.height = "0";
			hyperFrame.frameBorder = "0";
			hyperFrame.marginWidth = "0";
			hyperFrame.marginHeight = "0";
			hyperFrame.src = "http://medrx.sensis.com.au/images/sensis/hyperSet.html?"+MediaSmart.tags.pcode;
			document.getElementsByTagName('body')[0].appendChild(hyperFrame);
		}catch(e){}
	}
}

