MediaSmart.ads = new Array();

//PreDefined site and area -- Customize for each site
if (typeof(MediaSmart.site) == "undefined") 
	MediaSmart.site = "3RD.MY247.ENTERTAINMENT"; //!!! - Customize site
if (typeof(MediaSmart.area) == "undefined") 
	MediaSmart.area = "OTHER";
	
MediaSmart.newAd = function(json){
	if (json.brick == true)
	{
		brickPercent = 0;
		if ((Math.random() * 100) < brickPercent){
			json.height = 600;
			json.polite = false;
		}
	}
	if((json.width == 300 && json.height == 250)){
		var politeDecider = (Math.floor(Math.random()*100));
		var expandPercent = 30;
		if(politeDecider < expandPercent){
			json.polite = false;
		}
	}
			
	if (typeof(json.polite) != "undefined" && json.polite == false) {
		document.write(MediaSmart.Utilities.generateAdCall(json));
	}
	else {
		MediaSmart.ads[MediaSmart.ads.length] = json;
		MediaSmart.ads[MediaSmart.ads.length-1].top = 0;
		document.write("<div id='zMSplacement" + MediaSmart.ads.length + "' style='width:" + json.width + "px; height:" + json.height + "px;'></div>");
	}
}
	
MediaSmart.Utilities = new Object;
MediaSmart.Utilities.remarketing = true;
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";	
	
	//Check for time-based refresh
	MediaSmart.Utilities.area = "ENTERTAINMENT.MY247.";
	if (typeof(MediaSmart.city) != "undefined" && MediaSmart.city != "") 
		MediaSmart.Utilities.area += MediaSmart.city.toUpperCase() + ".";	
	
	
	
	//newcastle targetting
	if((document.location + "").indexOf("http://www.my247.com.au/newcastle") != -1&&  MediaSmart.city == ""){
		MediaSmart.Utilities.area += "NEWCASTLE.";		
	}else if((document.location + "").indexOf("http://www.my247.com.au/Newcastle") != -1&&  MediaSmart.city == ""){
		MediaSmart.Utilities.area += "NEWCASTLE.";		
	}
	
	
	if (typeof(MediaSmart.area) != "undefined" && MediaSmart.area != ""){
		MediaSmart.Utilities.area += MediaSmart.area.toUpperCase();
	}else{
		

		
		//URL targeting
		if((document.location + "").indexOf("&sp=bars&") != -1){
			MediaSmart.Utilities.area += "BARS_CLUBS";
		}else if((document.location + "").indexOf("&sp=clubs&") != -1){
			MediaSmart.Utilities.area += "BARS_CLUBS";
		}else if((document.location + "").indexOf("?p247=gigguide&") != -1 && (document.location + "").indexOf("&sp=arts&") == -1){
			MediaSmart.Utilities.area += "WHATSON";
		}else{//Default targeting
			MediaSmart.Utilities.area += "OTHER";
		}
		
		
	}
	MediaSmart.area = MediaSmart.Utilities.area;
    if ((document.location + "").indexOf("MSTOOTHPASTE") != -1) {
        MediaSmart.keyword = "MSTOOTHPASTE";
    }

}

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)
		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 + "' ";
		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://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 (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 (MediaSmart.postcode != "" && typeof(MediaSmart.postcode) != "undefined") 
		adCallString += "/PCODE=" + MediaSmart.postcode;
	adCallString += "/POSITION=";
		if(window.innerHeight-ad.top-90>0||typeof(ad.top)=="undefined")
			adCallString += "ABV";
		else
			adCallString += "BLW";
	if (ad.position != "" && typeof(ad.position) != "undefined") 
		 adCallString += ad.position;
	if (ad.keyword != "" && typeof(ad.keyword) != "undefined") 
		adCallString += "/KEYWORD=" + ad.keyword.toUpperCase().replace(/\s+|\//g,"_");
    else if((document.location + "").indexOf("mstoothpaste") != -1)
        adCallString += "/KEYWORD=MSTOOTHPASTE";
	else if (MediaSmart.keyword != "" && typeof(MediaSmart.keyword) != "undefined") 
			adCallString += "/KEYWORD=" + MediaSmart.keyword.toUpperCase().replace(/\s+|\//g,"_");
	if(!polite){
		if(adCallString.indexOf("/KEYWORD=") != -1)
			adCallString += "+EXPANDABLE";
		else
			adCallString += "/KEYWORD=EXPANDABLE";
	}

	if ((document.location + "") == "http://my247.com.au/melbourne/whats-on/category/Chinese-New-Year.69") {
		adCallString += "/TITLE=CHINESE_NEW_YEAR";
	}
	else if ((document.location + "") == "http://my247.com.au/melbourne/Chinatown/whats-on/Chinese-New-Year-Festival-2011.205420") {
		adCallString += "/TITLE=CHINESE_NEW_YEAR";
	}
	else if ((document.location + "") == "http://my247.com.au/melbourne/Chinese-Museum/whats-on/Chinese-New-Year-at-the-Chinese-Museum.205942") {
		adCallString += "/TITLE=CHINESE_NEW_YEAR";
	}
	else if ((document.location + "") == "http://my247.com.au/melbourne/Chinatown/whats-on/Chinese-Lunar-New-Year-Buddhist-Blessing-Ceremony.205166") {
		adCallString += "/TITLE=CHINESE_NEW_YEAR";
	}
	else if ((document.location + "") == "http://my247.com.au/melbourne/Chinatown/whats-on/FCA-Melbourne-Chinese-New-Year-Festival.205167") {
		adCallString += "/TITLE=CHINESE_NEW_YEAR";
	}
			
	for (key in MediaSmart.tags) {
		if (typeof(ad.tags[key]) == "undefined") {
			if (MediaSmart.tags[key] != "")
				adCallString += "/" + key.toUpperCase().replace(/\s+|\//g, "_") + "=" + MediaSmart.tags[key].toUpperCase();
		}
	}
	if(typeof(MediaSmart.var1) != "undefined" && MediaSmart.var1 != "") {
		adCallString += "/AGE=" + MediaSmart.var1;
	}
	if(typeof(MediaSmart.var2) != "undefined" && MediaSmart.var2 != "") {
		adCallString += "/GENDER=" + MediaSmart.var2.toUpperCase();
	}
	if (typeof(ad.tags) != "undefined") {
		for (key in ad.tags) {
			if (ad.tags[key] != "")
				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.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(){
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{
	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.Utilities.initialise();

MediaSmart.hyperLocal = function(){
	if(typeof(MediaSmart.postcode) != "undefined" && MediaSmart.postcode.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.postcode;
			document.getElementsByTagName('body')[0].appendChild(hyperFrame);
		}catch(e){}
	}
}

if(window.addEventListener){ // Mozilla, Netscape, Firefox
	window.addEventListener('load', MediaSmart.hyperLocal, false);
}else{ // IE
	window.attachEvent('onload', MediaSmart.hyperLocal);
}
