if (typeof(MediaSmart) != "object") {
    MediaSmart = new Object;
    MediaSmart.ads = new Array();

//PreDefined site and area -- Customize for each site
/*
if (typeof(MediaSmart.site) == "undefined") {
	if((document.location + "").indexOf("tradingpost.com.au/Automotive/") != -1)
		MediaSmart.site = "SEN.AUTOTRADER.AUTOMOTIVE";
	else
    	MediaSmart.site = "SEN.TRADINGPOST.CLASSIFIED";
}*/

/*if (typeof(MediaSmart.tags1) == "undefined") {
	if((document.location + "").indexOf("tradingpost.com.au/Automotive/") != -1)
		MediaSmart.tags1.area = "AUTOMOTIVE.AUTOTRADER";
	else
    	MediaSmart.tags1.area = "CLASSIFIED.TP";
}*/

MediaSmart.newAd = function(json){        
    	if (typeof(json.polite) != "undefined" && json.polite == false)  {
            document.write(MediaSmart.Utilities.generateAdCall(json));
        }
        else if(json.id.toLowerCase() == "generictower"){
			var towerpercent =0;
			var skypercent = 0;
			var sizedecider = (Math.floor(Math.random()*100));
			json.width = 160 ;
			json.height = 600 ; 
			if(sizedecider<towerpercent){
				
				json.width = 120;
	      	  	json.height = 240;
	       		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("<div style='height: 20px' ></div>");
            	MediaSmart.ads[MediaSmart.ads.length] = json;
            	document.write("<div id='zMSplacement" + MediaSmart.ads.length + "' style='width:" + json.width + "px; height:" + json.height + "px;'></div>");
			}
			else if(sizedecider<(towerpercent+skypercent)){
				json.width = 120 ;
				json.height = 600;
				MediaSmart.ads[MediaSmart.ads.length] = json;
            	document.write("<div id='zMSplacement" + MediaSmart.ads.length + "' style='width:" + json.width + "px; height:" + json.height + "px;'></div>");
			}
			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>");
			}

    	}
    	else if(json.width == 2 && json.height ==4){
            MediaSmart.ads[MediaSmart.ads.length] = json;
            document.write("<div id='zMSplacement" + MediaSmart.ads.length + "' style='width:148px; height:110px;'></div>");
    	}
    	else if((json.width == 300 && json.height == 250) || (json.width == 728 && json.height == 90)){
	    	var sizedecider = (Math.floor(Math.random()*100));
	    	var expandPercent = 50;
	    	
	    	if(sizedecider < expandPercent){
		    	json.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>");
	    	}
    	}
        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";
        }
    }
}
	
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 + "' ";
       // redirect to cookiefix for island and leaderboard
        if ((ad.width == 300 && ad.height == 250) || (ad.width == 728 && ad.height == 90) || (ad.width == 160 && 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 {
        if(MediaSmart.site.indexOf("autotrader") != -1 || MediaSmart.site == "SEN.AUTOTRADER.AUTOMOTIVE")
			MediaSmart.site = "SEN.AUTOTRADER.AUTOMOTIVE";
		else
			MediaSmart.site = "SEN.TRADINGPOST.CLASSIFIED";
	    adCallString += "/SITE=" + MediaSmart.site;
		
    }
    if (ad.area != "" && typeof(ad.area) != "undefined") 
        adCallString += "/AREA=" + ad.area;
    else if(MediaSmart.tags1.area != "" && typeof(MediaSmart.tags1.area) != "undefined")
    	adCallString += "/AREA=" + MediaSmart.tags1.area
    else{
    	if((document.location + "").indexOf("automotive") != -1)
			MediaSmart.tags1.area = "AUTOMOTIVE.AUTOTRADER";
		else
    		MediaSmart.tags1.area = "CLASSIFIED.TP";
        	adCallString += "/AREA=" + MediaSmart.tags1.area.toUpperCase().replace(/\s+|\//gi,"_").replace(/-/, "_").replace(/\([^)]*\)/g,"").replace(/\_$/g,"");
    }
	if (ad.section != "" && typeof(ad.section) != "undefined" ) 
        adCallString += "." + ad.section;
    else if(MediaSmart.tags1.section != "" && typeof(MediaSmart.tags1.section) != "undefined" && MediaSmart.tags1.section.toUpperCase() != "MAIN" && MediaSmart.tags1.section != "null" && MediaSmart.tags1.section.toUpperCase() != "AUTOMOTIVE")
    	adCallString += "." + MediaSmart.tags1.section.toUpperCase().replace(/\s+|\//gi,"_").replace(/-/, "_").replace(/\([^)]*\)/g,"").replace(/\_$/g,"");
   	if (ad.subsection != "" && typeof(ad.subsection) != "undefined") 
        adCallString += "." + ad.subsection.toUpperCase().replace(/\s+|\//gi,"_").replace(/-/, "_").replace(/\([^)]*\)/g,"").replace(/\_$/g,"");
    else if(MediaSmart.tags1.subsection != "" && typeof(MediaSmart.tags1.subsection) != "undefined" && MediaSmart.tags1.subsection != "null" && MediaSmart.tags1.subsection.toUpperCase() != MediaSmart.tags1.page.toUpperCase())
    	adCallString += "." + MediaSmart.tags1.subsection.toUpperCase().replace(/\s+|\//gi,"_").replace(/-/, "_").replace(/\([^)]*\)/g,"").replace(/\_$/g,"");
    if(MediaSmart.tags1.page != "" && typeof(MediaSmart.tags1.page) != "undefined"){
	    if((MediaSmart.tags1.subsection == "" || MediaSmart.tags1.subsection == "null") && MediaSmart.tags1.section.toLowerCase() == "automotive" ){
	    	MediaSmart.tags1.page ="Home";
    	}
    	adCallString += "." + MediaSmart.tags1.page.toUpperCase();
	}
	
	if(MediaSmart.tags1.category != "" && typeof(MediaSmart.tags1.category) != "undefined" && MediaSmart.tags1.category.toLowerCase() != "null"){
    	adCallString += "/CATEG=" + MediaSmart.tags1.category.toUpperCase().replace(/\s+|\//gi,"_").replace(/-/, "_").replace(/\([^)]*\)/g,"").replace(/\_$/g,"").replace(/:/,"").replace(/'/,"");
	}
	
   	if (ad.state != "" && typeof(ad.state) != "undefined") 
        adCallString += "/MCSB=TP." + ad.state;
    else if(MediaSmart.tags1.state != "" && typeof(MediaSmart.tags1.state) != "undefined"){
    	if(MediaSmart.tags1.state == "ALL_STATES")
    	MediaSmart.tags1.state = "NAT";
    	adCallString += "/MCSB=TP." + MediaSmart.tags1.state.toUpperCase();
	}
    else
    	adCallString += "/MCSB=TP.NAT"

    adCallString += "/AAMSZ=" + ad.width + "x" + ad.height;	
    
    if(polite){
    	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;
	}
	else if (ad.position != "" && typeof(ad.position) != "undefined") 	 
			adCallString += "/POSITION=" +ad.position;
	//below is for Vibe
/*	if(MediaSmart.tags1.section.toLowerCase() == "automotive"){
		var msAge = "USED";
		if(typeof(MediaSmart.tags1.yearfrom) != "undefined" && MediaSmart.tags1.yearfrom != "") {
		if(parseInt(MediaSmart.tags1.yearfrom) >= 2006)
			msAge= "NEARNEW";
		}
		var msValue = "null";
		if(typeof(MediaSmart.tags1.pricefrom) != "undefined" && MediaSmart.tags1.pricefrom != "" && typeof(MediaSmart.tags1.priceto) != "undefined" && MediaSmart.tags1.priceto != "")
			msValue = (parseInt(MediaSmart.tags1.pricefrom) + parseInt(MediaSmart.tags1.priceto)) /2;
		if(typeof(MediaSmart.tags1.pricefrom) != "undefined" && MediaSmart.tags1.pricefrom != "" && (typeof(MediaSmart.tags1.priceto) == "undefined" || MediaSmart.tags1.priceto == ""))
			msValue = parseInt(MediaSmart.tags1.pricefrom);
		if((typeof(MediaSmart.tags1.pricefrom) == "undefined" || MediaSmart.tags1.pricefrom == "") && typeof(MediaSmart.tags1.priceto) != "undefined" && MediaSmart.tags1.priceto != "")
			msValue = parseInt(MediaSmart.tags1.priceto);
		if(msValue < 10000)
			msValue = "LOW";
		else if (msValue >= 10000 && msValue <= 20000)
			msValue = "MEDIUM";
		else if (msValue>20000 && msValue < 30000)
			msValue = "HIGH";
		else 
			msValue = "VHIGH";
		adCallString += "/VIBE=VB."
		if(MediaSmart.tags1.subsection != "")
			adCallString += MediaSmart.tags1.subsection.toUpperCase() + ".";
		adCallString += msAge;
		if(typeof(MediaSmart.tags.carclass) != "undefined" && MediaSmart.tags.carclass != "")
			adCallString += MediaSmart.tags.carclass.toUpperCase() + ".";
	}		*/
	
    if (ad.keyword != "" && typeof(ad.keyword) != "undefined" && ad.keyword != "null") 
        adCallString += "/KEYWORD=" + ad.keyword.toUpperCase();
    else if((document.location + "").indexOf("?mstoothpaste") != -1)
        adCallString += "/KEYWORD=MSTOOTHPASTE";
    else 
        if (MediaSmart.keyword != "" && typeof(MediaSmart.keyword) != "undefined" && MediaSmart.keyword != "null") 
            adCallString += "/KEYWORD=" + MediaSmart.keyword.toUpperCase();
    if(MediaSmart.tags1.seller != "" && typeof(MediaSmart.tags1.seller) != "Undefined" && MediaSmart.tags1.seller != "null"){
	    if(adCallString.indexOf("/KEYWORD=") != -1)
    		adCallString += "+" + MediaSmart.tags1.seller.toUpperCase();
    	else
    		adCallString += "/KEYWORD=" + MediaSmart.tags1.seller.toUpperCase();
    }
    if(!polite){
	    if(adCallString.indexOf("/KEYWORD=") != -1)
    		adCallString += "+EXPANDABLE";
    	else
    		adCallString += "/KEYWORD=EXPANDABLE";
    }
    
    for (key in MediaSmart.tags) {
        if (typeof(MediaSmart.tags[key]) != "undefined" && MediaSmart.tags[key] != "" && MediaSmart.tags[key].toLowerCase() != "null") 
            adCallString += "/" + key.toUpperCase() + "=" + MediaSmart.tags[key].toUpperCase().replace(/\s+|\//gi,"_").replace(/-/, "_").replace(/\([^)]*\)/g,"").replace(/\_$/g,"");
    }
    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(){
	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();