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.YELLOWPAGES.SEARCH_DIRECTORIES";

MediaSmart.newAd = function(json){
	if(MediaSmart.Utilities.aboveBelowFold == false){
		json.abf = false;
	}
		if(typeof(json.id)!='undefined' && json.id == "hidden" && json.height == 5 && json.width == 1){
			json.height = 1;
		}
        if (typeof(json.polite) != "undefined" && 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>");
        }
        if (json.width == 150 && json.height == 200) {
	        document.getElementById("zMSplacement" + MediaSmart.ads.length).style.display = "none";
        }
    }
}
	
MediaSmart.Utilities = new Object;
//Above and Below the fold targeting. Default is false
MediaSmart.Utilities.aboveBelowFold = true;
MediaSmart.Utilities.initialise = function(){
    MediaSmart.pageId = MediaSmart.Utilities.random();
    MediaSmart.vibeTag = false;
    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 (typeof(MediaSmart.site) != "undefined" && MediaSmart.site != "")
        adCallString += "/SITE=" + MediaSmart.site;
    else adCallString += "/SITE=SEN.YELLOWPAGES.SEARCH_DIRECTORIES"
    
    adCallString += "/AREA=SEARCH_DIRECTORIES.YELLOWPAGES" ;
        
    if (ad.area != "" && typeof(ad.area) != "undefined") 
        adCallString += "." + ad.area;
    else if (MediaSmart.tags.area !="" && typeof(MediaSmart.tags.area) != "undefined"){
    	if(MediaSmart.tags.area.toLowerCase() == "stdsearch")
    		MediaSmart.tags.area = "YP_stdsearch"
		adCallString += "." + MediaSmart.tags.area.toUpperCase();
	}
	else adCallString += ".OTHER"       
	               
	if (ad.state != "" && typeof(ad.state) != "undefined") 
        adCallString += "." + ad.state;         
	else if (MediaSmart.tags.state !="" && typeof(MediaSmart.tags.state) != "undefined"){
		if(MediaSmart.tags.state == "1")
		MediaSmart.tags.state = "NSW";
		else if(MediaSmart.tags.state == "2")
		MediaSmart.tags.state = "VIC";
		else if(MediaSmart.tags.state == "3")
		MediaSmart.tags.state = "WA";
		else if(MediaSmart.tags.state == "4")
		MediaSmart.tags.state = "QLD";
		else if(MediaSmart.tags.state == "5")
		MediaSmart.tags.state = "SA";
		else if(MediaSmart.tags.state == "6")
		MediaSmart.tags.state = "ACT";
		else if(MediaSmart.tags.state == "7")
		MediaSmart.tags.state = "NT";
		else if(MediaSmart.tags.state == "8")
		MediaSmart.tags.state = "TAS";
		else if(MediaSmart.tags.state == "9")
		MediaSmart.tags.state = "NAT";
		adCallString += "." + MediaSmart.tags.state.toUpperCase();}
	else adCallString += "";  

	if (MediaSmart.tags.s_postcode !="" && typeof(MediaSmart.tags.s_postcode) != "undefined"){
	   for (var i = 0; i < MediaSmart.tags.s_postcode.length ; i++) 
       { 
       	if(MediaSmart.tags.s_postcode.charAt(i) == " ") 
       		MediaSmart.tags.s_postcode = MediaSmart.tags.s_postcode.slice(0,i);
       }
		adCallString += "/PCODE=" + MediaSmart.tags.s_postcode;	
	}	
		
    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;
    }

    var mstemp_categ = "";    
        
    if (ad.keyword != "" && typeof(ad.keyword) != "undefined") 
        mstemp_cate = ad.keyword ;
	else if (MediaSmart.tags.category != "" && typeof(MediaSmart.tags.category) != "undefined") 
            mstemp_categ = MediaSmart.tags.category.toUpperCase().replace(/'/g,"").replace(/\W+/g, ".");
	else if (MediaSmart.tags.keyword != "" && typeof(MediaSmart.tags.keyword) != "undefined") 
            mstemp_categ = MediaSmart.tags.keyword.toUpperCase().replace(/'/g,"").replace(/\W+/g, ".");

   if(mstemp_categ != ""){
	    adCallString += "/CATEG=" + mstemp_categ;
	    if (!MediaSmart.vibeTag){
	    	adCallString += "/VIBE=" + mstemp_categ;
	        MediaSmart.vibeTag = true;
        }
   }
            
    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 (var 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(){
	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.initialise();