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 = "TEL.BIGPOND.PORTAL.ENTERTAINMENT";
    if (typeof(MediaSmart.area) == "undefined") 
        MediaSmart.area = "ENTERTAINMENT.BIGPOND.HOMEPAGE";
    
    
    MediaSmart.newAd = function(json){
	    if(MediaSmart.Utilities.aboveBelowFold == false)	{
			json.abf = false;
		}
        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>");
        }
    }
}

MediaSmart.Utilities = new Object;
MediaSmart.Utilities.initCalled = 0;
//Above and Below the fold targeting. Default is false
MediaSmart.Utilities.aboveBelowFold = true;
MediaSmart.Utilities.initialise = function(){

    if (arguments.length > 0) {
        var zmsJson = arguments[0];
        
        if (zmsJson.area != "" && typeof(zmsJson.area) == "string") {
            MediaSmart.area = zmsJson.area;
        }
        if (zmsJson.site != "" && typeof(zmsJson.site) == "string") {
            MediaSmart.site = zmsJson.site.toUpperCase();
        }       
    }
    if ((document.location + "").indexOf("https://") != -1) 
        MediaSmart.domain = "https://media.sensis.com.au";
    else 
        MediaSmart.domain = "http://media.sensis.com.au";
    MediaSmart.pageId = MediaSmart.Utilities.random();
    
    
}
MediaSmart.Utilities.random = function(){
    return Math.round(Math.abs(Math.sin((new Date()).getTime())) * 1000000000000);
}

MediaSmart.setRefresh = function(time){
    //if(time > 0)
    //{
    //	setInterval("MediaSmart.Utilities.refreshAd()", (time * 1000));
    //}
}

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 title='Advertisement' 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 + "' ";
        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 ((((document.location + "").indexOf("http://webmail") != -1) && ((document.location + "").indexOf("bigpond.com/webedge") != -1)) || (document.location + "").indexOf("webedge.bigpond.com") != -1)
			adCallString += "/AREA=ENTERTAINMENT.BIGPOND.OLDWEBMAIL"
		else
			adCallString += "/AREA=" + MediaSmart.area;
	}
    adCallString += "/AAMSZ=" + ad.width + "x" + ad.height;
    if (ad.position != "" && typeof(ad.position) != "undefined") {
        adCallString += "/POSITION=";
        if (ad.containerId == "footerAdPosition") {
	    	adCallString += "BLW";    
    	}     
        else if (MediaSmart.innerHeight - ad.top - 90 > 0 || typeof(ad.top) == "undefined") {
            adCallString += "ABV";
        }
        else {
            adCallString += "BLW";
        }
        adCallString += ad.position;
	}    
    if (ad.keyword != "" && typeof(ad.keyword) != "undefined") 
        adCallString += "/KEYWORD=" + ad.keyword.toUpperCase().replace(/\s+|\//g, "_");
    else 
        if (MediaSmart.keyword != "" && typeof(MediaSmart.keyword) != "undefined") 
            adCallString += "/KEYWORD=" + MediaSmart.keyword.toUpperCase().replace(/\s+|\//g, "_");
    
    for (key in MediaSmart.tags) {
        if (typeof(ad.tags[key]) == "undefined") {
            if (MediaSmart.tags[key] != "") 
                adCallString += "/" + key.toUpperCase().replace(/\s+|\//g, "_") + "=" + MediaSmart.tags[key].toUpperCase();
        }
    }
    if (typeof(ad.tags) != "undefined") {
        for (key in ad.tags) {
            if (ad.tags[key] != "") 
                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.getAdById(ad);
        if (ad) {
            if (elementId == "" || typeof(elementId) == "undefined") {
                if (document.getElementById("zMSplacement" + MediaSmart.Utilities.adIndex) != null) 
                    element = document.getElementById("zMSplacement" + MediaSmart.Utilities.adIndex);
                else 
                    element = document.getElementById(ad.containerId);
            }
            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.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.Utilities.refreshAd = function(){
    MediaSmart.insertAd(MediaSmart.ads);
}

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]);
    }
}

document.write("<scr" + "ipt src='//medrx.sensis.com.au/images/sensis/BigPond/Sha.js'></scr" + "ipt>");

function zMSsetId(zMSid){
	if(zMSid != "") {
		zMSid = zMSid.toUpperCase().replace("@BIGPOND.COM", "");
		zMSid = zMSid.toUpperCase().replace("@TELSTRA.COM", "");
	    var zMShashed = (sha1Hash(zMSid.toLowerCase())).toUpperCase();
	    document.write('<iframe title="" src="https://media.sensis.com.au/hserver/SITE=Z_ZORRO/AAMSZ=1x8/KEYWORD=URUSER?name=GUID&value=' + zMShashed + '&duration=90" name="userReg" width="0" height="0" frameborder="0" MARGINWIDTH="0" MARGINHEIGHT="0" SCROLLING="no">\n');
	    document.write("</iframe>\n");
	}
}

MediaSmart.Utilities.initialise();


