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 = "SEN.WHEREIS.SEARCH_DIRECTORIES";

MediaSmart.newAd = function(json){

        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;
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://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 if (typeof(MediaSmart.tags.area) != "undefined" && MediaSmart.tags.area != "") 
    	adCallString += "/AREA=" + MediaSmart.tags.area.toUpperCase();
    else
        adCallString += "/AREA=SEARCH_DIRECTORIES.WHEREIS";
    
    if (ad.section != "" && typeof(ad.section) != "undefined") 
        adCallString += "." + ad.section.toUpperCase();
    else if (typeof(MediaSmart.tags.section) != "undefined" && MediaSmart.tags.section != ""){ 
	    if (MediaSmart.tags.section.toUpperCase() == "MAPS")
	    	adCallString += ".MAP";
	    else
    	adCallString += "." + MediaSmart.tags.section.toUpperCase();}
    else
        adCallString += ".OTHER";
    
    if (ad.subsection != "" && typeof(ad.subsection) != "undefined") 
        adCallString += "." + ad.subsection;
    else if (typeof(MediaSmart.tags.subsection) != "undefined" && MediaSmart.tags.subsection != "") 
    	adCallString += "." + MediaSmart.tags.subsection.toUpperCase();
	
    if (ad.state != "" && typeof(ad.state) != "undefined") 
        adCallString += "." + ad.state.toUpperCase()+ "/AAMGNRC1=" + ad.state.toUpperCase();
    else if (typeof(MediaSmart.tags.state) != "undefined" && MediaSmart.tags.state != "") 
    	adCallString += "." + MediaSmart.tags.state.toUpperCase() + "/AAMGNRC1=" + MediaSmart.tags.state.toUpperCase();
    	
    if (ad.suburb != "" && typeof(ad.suburb) != "undefined") 
        adCallString += "/SUBURB=" + ad.suburb.toUpperCase().replace(/'/g,"").replace(/\W/gi,".") + "/AAMGNRC2=" + ad.suburb.toUpperCase();
    else if (typeof(MediaSmart.tags.suburb) != "undefined" && MediaSmart.tags.suburb != "") 
    	adCallString += "/SUBURB=" + MediaSmart.tags.suburb.toUpperCase().replace(/'/g,"").replace(/\W/gi,".") + "/AAMGNRC2=" + MediaSmart.tags.suburb.toUpperCase();
    
    if (ad.postcode != "" && typeof(ad.postcode) != "undefined") 
        adCallString += "/PCODE=" + ad.postcode;
    else if (typeof(MediaSmart.tags.postcode) != "undefined" && MediaSmart.tags.postcode != "") 
    	adCallString += "/PCODE=" + MediaSmart.tags.postcode;
    	    	    	
	if (ad.camp_name != "" && typeof(ad.camp_name) != "undefined"){
        adCallString += "/GENRE=" + decodeURIComponent(decodeURI(ad.camp_name)).toUpperCase().replace(/'/g,"").replace(/\W+/g,".");
	}
    else if (typeof(MediaSmart.tags.camp_name) != "undefined" && MediaSmart.tags.camp_name != ""){
    	adCallString += "/GENRE=" + MediaSmart.tags.camp_name.toUpperCase().replace(/'/g,"").replace(/\W+/g,".");
    }
						
						
    if (ad.cust_name != "" && typeof(ad.cust_name) != "undefined") 
        adCallString += "/TRAVELREGION=WI.POI." + ad.cust_name.toUpperCase().replace(/\s+|\//gi,"_").replace(/-/g, "_").replace(/\([^)]*\)/g,"").replace(/\_$/g,"").replace(/___/g,"_").replace(/__/g,"_").replace(/'/g,"");
    else if (typeof(MediaSmart.tags.cust_name) != "undefined" && MediaSmart.tags.cust_name != "") 
    	adCallString += "/TRAVELREGION=WI.POI." + MediaSmart.tags.cust_name.toUpperCase().replace(/\s+|\//gi,"_").replace(/-/g, "_").replace(/\([^)]*\)/g,"").replace(/\_$/g,"").replace(/___/g,"_").replace(/__/g,"_").replace(/'/g,"");
         
	if (ad.yp_heading != "" && typeof(ad.yp_heading) != "undefined"){
        adCallString += "/CATEG=" + decodeURIComponent(decodeURI(ad.yp_heading)).toUpperCase().replace(/'/g,"").replace(/\W+/g,".");
	}
    else if (typeof(MediaSmart.tags.yp_heading) != "undefined" && MediaSmart.tags.yp_heading != ""){
    	adCallString += "/CATEG=" + MediaSmart.tags.yp_heading.toUpperCase().replace(/'/g,"").replace(/\W+/g,".");
    }
    	
    if (typeof(MediaSmart.tags.keyword) != "undefined" && MediaSmart.tags.keyword != "")
    	adCallString += "/KEYWORD=" + MediaSmart.tags.keyword.toUpperCase();
    	
    adCallString += "/AAMSZ=" + ad.width + "x" + ad.height;
    if (ad.position != "" && typeof(ad.position) != "undefined")    	
        adCallString += "/POSITION=" + ad.position;

    if (typeof(ad.tags) != "undefined" && ad.tags != "") {
        for (key in ad.tags) {
            adCallString += "/" + key.toUpperCase() + "=" + ad.tags[key].toUpperCase();
        }
    }

    
    if(parent.document != document){
   		if (((ad.width == "300") && (ad.height == "250")) || ((ad.width == "468") && (ad.height == "60"))){
    		if (typeof(parent.page_id_hack) != "undefined")
	   			MediaSmart.pageId = parent.page_id_hack;
    		else{
    			parent.page_id_hack = MediaSmart.pageId;
			}
		}	
	}


    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);
        }
    }
}

function loadAd(elementId, adParms) {
	MediaSmart.ads[MediaSmart.ads.length] = (adParms);
	MediaSmart.insertAd(adParms.id, elementId);
}
            
MediaSmart.Utilities.getAdById = function(id){
    for (i = MediaSmart.ads.length-1; i >= 0; 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)).innerHTML = MediaSmart.Utilities.generateAdCall(MediaSmart.ads[i]);
    }
}
MediaSmart.Utilities.initialise();