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.subArea = "";

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 (arguments.length > 0) {
		var zmsJson = arguments[0];
		if (zmsJson.area != "" && typeof(zmsJson.area) == "string") {
			var zmsString = zmsJson.area;
				MediaSmart.subArea = zmsString.toUpperCase();
		}
	}

    
    if ((document.location + "").indexOf("https://") != -1) {
        MediaSmart.domain = "https://media.sensis.com.au";
    }else{ 
        MediaSmart.domain = "http://media.sensis.com.au";

	}

	// Add campaign specific keyword 40% of the time
	if(Math.floor(Math.random()*10) <4) {
		if(typeof(MediaSmart.keyword) != "undefined" && MediaSmart.keyword != ""){
			MediaSmart.keyword += "+117896_NAB";
		}else{
			MediaSmart.keyword = "117896_NAB";
		}

	}

	
}


MediaSmart.Utilities.random = function(){

    return Math.round(Math.abs(Math.sin((new Date()).getTime())) * 1000000000000);
}

MediaSmart.Utilities.generateAdCall = function(ad){

	// Check to swap 300x250 for 960x250
	if( isTestSite() && ad.subsection == "HOME" ) {
		var sizedecider = (Math.floor(Math.random()*100) );
		if( sizedecider < 50 ) {
			if( ad.width == 300 && ad.height == 250 ) {
				ad.width = 960;
				ad.height = 250;
				ad.iframeWidth = 960;
				ad.iframeHeight = 250;
			}
		}	
	}
	
	
    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 ( MediaSmart.doCookieFix(ad) == true )
         	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") {
		var postCodeValue = ad.suburb.toUpperCase().replace(/'/g,"").replace(/\W/gi,".");
		adCallString += "/SUBURB=" + postCodeValue + "/AAMGNRC2=" + postCodeValue;	
	}       
    else if (typeof(MediaSmart.tags.suburb) != "undefined" && MediaSmart.tags.suburb != "") {
		var postCodeValue = MediaSmart.tags.suburb.toUpperCase().replace(/'/g,"").replace(/\W/gi,".");
		adCallString += "/SUBURB=" + postCodeValue + "/AAMGNRC2=" + postCodeValue;
	}
    	
	
	
	
	// Grab the postcode and set the hyperlocal cookie
    var postcodeValue = "";
    if (ad.postcode != "" && typeof(ad.postcode) != "undefined")
		postcodeValue = ad.postcode;
    else if (typeof(MediaSmart.tags.postcode) != "undefined" && MediaSmart.tags.postcode != "") 
		postcodeValue = MediaSmart.tags.postcode;
		
	if( postcodeValue != "" && postcodeValue.match(/^\d{4}$/) ) {
		adCallString += "/PCODE=" + postcodeValue;
		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?" + postcodeValue;
				document.getElementsByTagName('body')[0].appendChild(hyperFrame);
		}catch(e){}
	}

// Pauls screen hack

if(document.body.clientHeight > 850 && document.body.clientWidth > 800){    
adCallString += "/SITE_CONTENT=BIG_SCREEN"
} else { adCallString += "/SITE_CONTENT=SMALL_SCREEN"
}

	
	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 (ad.homeState != "" && typeof(ad.homeState) != "undefined"){
        adCallString += "/HOMESTATE=" + decodeURIComponent(decodeURI(ad.homeState)).toUpperCase().replace(/'/g,"").replace(/\W+/g,".");
	}
    else if (typeof(MediaSmart.tags.homeState) != "undefined" && MediaSmart.tags.homeState != ""){
    	adCallString += "/HOMESTATE=" + MediaSmart.tags.homeState.toUpperCase().replace(/'/g,"").replace(/\W+/g,".");
    }
	
	if (ad.homeSuburb != "" && typeof(ad.homeSuburb) != "undefined"){
        adCallString += "/HOMESUBURB=" + decodeURIComponent(decodeURI(ad.homeSuburb)).toUpperCase().replace(/'/g,"").replace(/\W+/g,".");
	}
    else if (typeof(MediaSmart.tags.homeSuburb) != "undefined" && MediaSmart.tags.homeSuburb != ""){
    	adCallString += "/HOMESUBURB=" + MediaSmart.tags.homeSuburb.toUpperCase().replace(/'/g,"").replace(/\W+/g,".");
    }
	
	if (ad.homePostcode != "" && typeof(ad.homePostcode) != "undefined"){
        adCallString += "/HOMEPOSTCODE=" + decodeURIComponent(decodeURI(ad.homePostcode)).toUpperCase().replace(/'/g,"").replace(/\W+/g,".");
	}
    else if (typeof(MediaSmart.tags.homePostcode) != "undefined" && MediaSmart.tags.homePostcode != ""){
    	adCallString += "/HOMEPOSTCODE=" + MediaSmart.tags.homePostcode.toUpperCase().replace(/'/g,"").replace(/\W+/g,".");
    }
	
	
	if (ad.workState != "" && typeof(ad.workState) != "undefined"){
        adCallString += "/WORKSTATE=" + decodeURIComponent(decodeURI(ad.workState)).toUpperCase().replace(/'/g,"").replace(/\W+/g,".");
	}
    else if (typeof(MediaSmart.tags.workState) != "undefined" && MediaSmart.tags.workState != ""){
    	adCallString += "/WORKSTATE=" + MediaSmart.tags.workState.toUpperCase().replace(/'/g,"").replace(/\W+/g,".");
    }
	
	if (ad.workSuburb != "" && typeof(ad.workSuburb) != "undefined"){
        adCallString += "/WORKSUBURB=" + decodeURIComponent(decodeURI(ad.workSuburb)).toUpperCase().replace(/'/g,"").replace(/\W+/g,".");
	}
    else if (typeof(MediaSmart.tags.workSuburb) != "undefined" && MediaSmart.tags.workSuburb != ""){
    	adCallString += "/WORKSUBURB=" + MediaSmart.tags.workSuburb.toUpperCase().replace(/'/g,"").replace(/\W+/g,".");
    }
	
	if (ad.workPostcode != "" && typeof(ad.workPostcode) != "undefined"){
        adCallString += "/WORKPOSTCODE=" + decodeURIComponent(decodeURI(ad.workPostcode)).toUpperCase().replace(/'/g,"").replace(/\W+/g,".");
	}
    else if (typeof(MediaSmart.tags.workPostcode) != "undefined" && MediaSmart.tags.workPostcode != ""){
    	adCallString += "/WORKPOSTCODE=" + MediaSmart.tags.workPostcode.toUpperCase().replace(/'/g,"").replace(/\W+/g,".");
    }
	
	
    	
    if (typeof(MediaSmart.tags.keyword) != "undefined" && MediaSmart.tags.keyword != "")
    	adCallString += "/KEYWORD=" + MediaSmart.tags.keyword.toUpperCase();
	
	if ( (document.location + "").indexOf("MSTOOTHPASTE") != -1) {
		if( adCallString.indexOf( "/KEYWORD" ) != -1 )
			adCallString += "+MSTOOTHPASTE";
		else
			adCallString += "/KEYWORD=MSTOOTHPASTE";
	
		// Useful function to find all elemnts of an object
		/*
		for (var i in ad) {
		  var msg;
		  msg = msg + i + "\n" + ad[i] + "\n";	  
	    } 
	    alert( msg );	
		*/
	}
		
	// Temporary checking for results pages
	if ( adCallString.indexOf("SUBURB=ROCKDALE") != -1 && adCallString.indexOf("CATEG=MOTOR.ENGINEERS.REPAIRERS")!=-1 ) {
		if( adCallString.indexOf( "/KEYWORD" ) != -1 )
			adCallString += "+MSTOOTHPASTE";
		else
			adCallString += "/KEYWORD=MSTOOTHPASTE";
	}
	
	/*
	if (document.location == "http://www.whereis.com/" || document.location == "http://www.whereis.com" ) {
		adCallString += "/CARCLASS=ATV";
	}
	*/
	if( isTestSite() ) {
		adCallString += "/CARCLASS=TRAIL";
	}
	
    	
    adCallString += "/AAMSZ=" + ad.width + "x" + ad.height;
    if (ad.position != "" && typeof(ad.position) != "undefined")    	
        adCallString += "/POSITION=ABV" + 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;
			}
		}	
	}

	if((document.location + "").indexOf("MSTOOTHPASTE") != -1){
		adCallString += (document.location + "").slice((document.location + "").lastIndexOf("MSTOOTHPASTE")+12);
	}

    adCallString += "/pageid=" + MediaSmart.pageId;
	if(polite)
		adCallString += "'></iframe>\n";
	else
		adCallString += "'></script>\n";
	
	if( document.location && document.location == "http://www.whereis.com/" && ad.width == 468 ) {
		ad.width = 1;
		ad.height = 1;
		ad.iframeWidth = 1;
		ad.iframeHeight = 1;
		return adCallString + MediaSmart.Utilities.generateAdCall(ad);  
	}
	else
		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);
}


function isTestSite() {
	if ((document.location + "").indexOf("http://whereis.stag2.drs.sensis.com.au") != -1) {
		return true;
	}
	else if ((document.location + "").indexOf("http://wiappd101d:8080/spectrum") != -1) {
		return true;
	}
	else if ((document.location + "").indexOf("http://stg.whereis.com") != -1) {
		return true;
	}
	else if ((document.location + "").indexOf("http://tst.whereis.com") != -1) {
		return true;
	}
	else if ((document.location + "").indexOf("http://localhost") != -1) {
		return true;
	}
	return false;
}


            
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.doCookieFix = function(ad) {
	if( ad.width == 300 && ad.height == 250 )
		return true;
	else if( ad.width == 728 && ad.height == 90 )
		return true;
	else if( ad.width == 160 && ad.height == 600 )
		return true;
	else if( ad.width == 468 && ad.height == 60 )
		return true;
		
	return false;
}


MediaSmart.Utilities.initialise();
