if (typeof(MediaSmart) != "object") {
    MediaSmart = new Object;
    MediaSmart.ads = new Array();
var aacount = 0;
var tpurl = "http://medrx.sensis.com.au/images/sensis/carshowroom/";
var tpleaderboard = "yes"; 
//PreDefined site and area -- Customize for each site
if (typeof(MediaSmart.site) == "undefined") 
    MediaSmart.site = "SEN.CARSHOWROOM.AUTOMOTIVE";

var ispolite = true;
    

	
MediaSmart.newAd = function(json){
	if(MediaSmart.Utilities.aboveBelowFold == false){
		json.abf = false;
	}
        if ((typeof(json.polite) != "undefined" && json.polite == false) || ispolite == 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";
        }
        
        //hack to fix display:none leaders
        if (json.width == 728 && json.height == 90) {
	        document.getElementById("zMSplacement" + MediaSmart.ads.length).style.display = "block";
        }
    }
}
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();
    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(MediaSmart.tags1.area) != "undefined" && MediaSmart.tags1.area != "") {
		if  (MediaSmart.tags1.area.toUpperCase() == "AUTOMOTIVE.AUTOCULT"){
			MediaSmart.tags1.area = "OTHER";
		}		
	}	
    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) || ispolite == 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.toUpperCase().replace(/\s+|\//gi,"").replace(/-/, "_").replace(/\([^)]*\)/g,"").replace(/\_$/g,"");
    else if (typeof(MediaSmart.tags1.area) != "undefined" && MediaSmart.tags1.area != "") {
        adCallString += "/AREA=AUTOMOTIVE.CARSHOWROOM." + MediaSmart.tags1.area.toUpperCase().replace(/\s+|\//gi,"").replace(/-/, "_").replace(/\([^)]*\)/g,"").replace(/\_$/g,"");
		
		/* RAY HACK 18-03-2011 Toyota Homepage buyout
		MediaSmartloc = document.location+"";
		if (MediaSmartloc == "http://www.carshowroom.com.au/") {
			adCallString += ".HOME";
		} */
	}
	else
		adCallString += "/AREA=AUTOMOTIVE.CARSHOWROOM.OTHER";
	

        
/*	if (MediaSmart.tags1.area !="" && typeof(MediaSmart.tags1.area) != "undefined")
		adCallString += "." + MediaSmart.tags1.area.toUpperCase();
	else { 		*/
	MediaSmartloc = document.location+"";
	if(MediaSmart.tags1.area == "newcars") {
		if(MediaSmartloc.indexOf("newcardetails")!=-1)
			adCallString += ".DETAILS";
		else if(MediaSmartloc.indexOf("default.aspx")!=-1)
			adCallString += ".HOME";
		else if(MediaSmartloc.toLowerCase().indexOf("review.aspx")!=-1 || MediaSmartloc.toLowerCase().indexOf("reviews.aspx")!=-1)
			adCallString += ".REVIEWS";
		else if(MediaSmartloc.toLowerCase().indexOf("category=dealer")!=-1 )
			adCallString += ".DEALERS";
		else if(MediaSmartloc.length<31)
			adCallString += ".HOME";
		else if(MediaSmartloc.toLowerCase().indexOf("category=search+results")!=-1 || MediaSmartloc.toLowerCase().indexOf("category=layouts")!=-1 )
			adCallString += ".SEARCHRESULTS";
		else if(MediaSmartloc.toLowerCase().indexOf("category=vehicle%20specifications")!=-1 )
			adCallString += ".DETAILS";
		else
			adCallString += ".OTHER";
	}
	else /*{
		if(MediaSmart.tags1.area != "" && typeof(MediaSmart.tags1.area)!="undefined"){
		if(MediaSmart.tags1.area.toLowerCase() == "sedan" || MediaSmart.tags1.area.toLowerCase() == "coupe" || MediaSmart.tags1.area.toLowerCase() == "peoplemover" || MediaSmart.tags1.area.toLowerCase() == "convertable" || MediaSmart.tags1.area.toLowerCase() == "suv" || MediaSmart.tags1.area.toLowerCase() == "utilities" || MediaSmart.tags1.area.toLowerCase() == "hatchback" || MediaSmart.tags1.area.toLowerCase() == "wagon")
			adCallString += ".SEARCHRESULTS";
		else 
			adCallString += "." +MediaSmart.tags1.area.toUpperCase().replace(/\s+|\//gi,"").replace(/-/, "_").replace(/\([^)]*\)/g,"").replace(/\_$/g,"");
		}
		else adCallString += ".OTHER";
	}*/
//	}
/*	if(MediaSmart.tags1.state != "" && typeof(MediaSmart.tags1.state)!="undefined")
		adCallString += "." +MediaSmart.tags1.state.toUpperCase();
	else adCallString += ".NAT";*/
	
	if(MediaSmart.tags1.area.toLowerCase() == "sedan" || MediaSmart.tags1.area.toLowerCase() == "coupe" || MediaSmart.tags1.area.toLowerCase() == "peoplemover" || MediaSmart.tags1.area.toLowerCase() == "convertable" || MediaSmart.tags1.area.toLowerCase() == "suv" || MediaSmart.tags1.area.toLowerCase() == "utilities" || MediaSmart.tags1.area.toLowerCase() == "hatchback" || MediaSmart.tags1.area.toLowerCase() == "wagon"){
		if(MediaSmart.tags1.area.toLowerCase() == "peoplemover")
			MediaSmart.tags1.area = "PEOPLE_MOVERS";
		adCallString += "/CARCLASS=" + MediaSmart.tags1.area.toUpperCase();
	}
	
	if( adCallString.indexOf( "CARCLASS=" ) == -1 ) {
		if( (window.location + "").indexOf( "newcars/list/bodystyle-1" ) != -1 )
			adCallString += "/CARCLASS=SEDAN";
		else if( (window.location + "").indexOf( "newcars/list/bodystyle-2" ) != -1 )
			adCallString += "/CARCLASS=COUPE";
		else if( (window.location + "").indexOf( "newcars/list/bodystyle-3" ) != -1 )
			adCallString += "/CARCLASS=HATCHBACK";
		else if( (window.location + "").indexOf( "newcars/list/bodystyle-4" ) != -1 )
			adCallString += "/CARCLASS=CONVERTIBLE";
		else if( (window.location + "").indexOf( "newcars/list/bodystyle-5" ) != -1 )
			adCallString += "/CARCLASS=SUV";
		else if( (window.location + "").indexOf( "newcars/list/bodystyle-6" ) != -1 )
			adCallString += "/CARCLASS=WAGON";
		else if( (window.location + "").indexOf( "newcars/list/bodystyle-7" ) != -1 )
			adCallString += "/CARCLASS=PEOPLE_MOVER";
		else if( (window.location + "").indexOf( "newcars/list/bodystyle-8" ) != -1 )
			adCallString += "/CARCLASS=UTE";
	}

	
    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;
    }
    else if((document.location + "").indexOf("nrma.php?qld") != -1) 
    	adCallString += "/POSITION=1";
    else if((document.location + "").indexOf("nrma.php") != -1)
    	adCallString += "/POSITION=2";
		
	//Keyword hack
	if (ad.keyword != "" && typeof(ad.keyword) != "undefined") 
        adCallString += "/KEYWORD=" + ad.keyword.toUpperCase();
    else if((document.location + "").indexOf("castrol-Magnatec") != -1)
        adCallString += "/KEYWORD=116022_CASTROL";    	
		

    if (ad.keyword != "" && typeof(ad.keyword) != "undefined") 
        adCallString += "/KEYWORD=" + ad.keyword.toUpperCase();
    else if((document.location + "").indexOf("MSTOOTHPASTE") != -1)
        adCallString += "/KEYWORD=MSTOOTHPASTE";    
    else
        if (MediaSmart.tags1.kw != "" && typeof(MediaSmart.tags1.kw) != "undefined") 
            adCallString += "/KEYWORD=" + MediaSmart.tags1.kw.toUpperCase();
    else if ((document.location + "").indexOf("/newcars/for-sale") != -1) 
        adCallString += "/KEYWORD=HYUNDAI_Q2"; 
    if(polite == false && adCallString.indexOf("KEYWORD")!= -1) 
    	adCallString += "+EXPANDABLE";
    else if(polite == false)
    	adCallString += "/KEYWORD=EXPANDABLE";
    	
    //below is a hack for Mitsubishi Slideshow pages 1/03/11
    if((document.location + "").toLowerCase().indexOf("mitsubishi")!=-1)
    	MediaSmart.tags.carmake = "Mitsubishi";
		
	
	// Quick fix to allow targeting section homepages
	if((document.location + "")=="http://www.carshowroom.com.au/newcars/news/" || (document.location + "")=="http://www.carshowroom.com.au/newcars/reviews/" || (document.location + "")=="http://www.carshowroom.com.au/newcars/models/" )
    	adCallString += "/SITE_CONTENT=HOME";
	else if((document.location + "")=="http://www.carshowroom.com.au/newcars/compare/" || (document.location + "")=="http://www.carshowroom.com.au/newcars/research/buying-guide/" || (document.location + "")=="http://www.carshowroom.com.au/newcars/for-sale/" )
    	adCallString += "/SITE_CONTENT=HOME";
	else if((document.location + "")=="http://www.carshowroom.com.au/newcars/dealers/" || (document.location + "")=="http://www.carshowroom.com.au/newcars/photos/" || (document.location + "")=="http://www.carshowroom.com.au/newcars/videos/" )
    	adCallString += "/SITE_CONTENT=HOME";
		
    
    for (key in MediaSmart.tags) {
        if (typeof(MediaSmart.tags[key]) != "undefined" && MediaSmart.tags[key] != "") 
            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();
        }
    }

	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";
		 
    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(){
	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{


		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.bigpondPromotions = new Object();
MediaSmart.bigpondPromotions.newAd = function(json){
	json.site = "SEN.BPPROMO.CARSHOWROOM.AUTOMOTIVE";
	json.abf = false;
	if ((typeof(json.polite) != "undefined" && json.polite == false) || MediaSmart.Utilities.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>");
	}
}

MediaSmart.refreshAllAds = function() {
	aacount = 0;
	MediaSmart.insertAllAds();
}
