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 = "3RD.NRL.SPORT";
    if (typeof(MediaSmart.area) == "undefined") 
        MediaSmart.area = "SPORT.NRL.HOME";
    
    
    MediaSmart.newAd = function(json){
	if(MediaSmart.Utilities.aboveBelowFold == false){
		json.abf = false;
	}
		MediaSmart.Utilities.manualURLTargeting();
        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;

//Above and Below the fold targeting. Default is false
MediaSmart.Utilities.aboveBelowFold = true;

MediaSmart.Utilities.debug = false;
MediaSmart.Utilities.brick_percentage = 50;
var zMSDate = new Date();
var zMSHour = zMSDate.getHours();
var zMSd = zMSDate.getDate();
		
if((zMSHour > 9 && zMSHour < 14) || (zMSHour > 15 && zMSHour < 21)){
		//	if(zMSd < 14){
	//MediaSmart.Utilities.brick_percentage = 0;
		//	}			
}





MediaSmart.Utilities.debug = false;
//Testing environment
parntLocation = parent.document.location + "";
if (parntLocation.indexOf("zmstestingenvironment375") != -1) {
    MediaSmart.Utilities.debug = true;
   	MediaSmart.Utilities.brick_percentage = 100;
}
MediaSmart.Utilities.brick_present = false;
MediaSmart.Utilities.isPageBrickSafe = function(){
    var safePages = new Array("NewsViews/PlayerTransfers","NewsViews/BigLeague","2009NRLEventCalendar","ReferenceCentre/NRLHistoryStructure", "TelstraPremiership/TelstraPremiershipScores","TelstraPremiership/Ladder/","TelstraPremiership/CasualtyWard","TelstraPremiership/ClubStats");
    
    var pageUrl = parent.document.location + "";
	pageUrl = pageUrl.toUpperCase();
    //testing
    if (pageUrl.indexOf("zmstestingenvironment375") != -1) {
       // return true;
    }
    
    //TODO add more pages
    
    var length = safePages.length;
    for (var i = 0; i < length; i++) {
        if (pageUrl.indexOf(safePages[i].toUpperCase()) != -1) {
            return true;
            break;
        }
    }
    return false;
}


MediaSmart.Utilities.initialise = function(){
    if (arguments.length > 0) {
        var zmsJson = arguments[0];
        if (zmsJson.area != "") {
            var zmsString = zmsJson.area;
            MediaSmart.area = "SPORT.NRL." + zmsString.toUpperCase();
        }
    }
    
    
    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";
    //Check for time-based refresh
    if (MediaSmart.Utilities.isPageBrickSafe() == true) {
        //	MediaSmart.setRefresh(60);
    }
    if ((document.location + "").indexOf("MSTOOTHPASTE") != -1) {
        MediaSmart.keyword = "MSTOOTHPASTE";
    }
	

}
MediaSmart.Utilities.random = function(){
    return Math.round(Math.abs(Math.sin((new Date()).getTime())) * 1000000000000);
}

MediaSmart.setRefresh = function(time){
    if (time > 0) {
		time = 120;
        setInterval("MediaSmart.Utilities.refreshAd()", (time * 1000));
    }
}

MediaSmart.Utilities.generateAdCall = function(ad){
    var index = MediaSmart.ads.length;
    var iframeId = "";
    
    //See if a brick ad should be inserted
    if (MediaSmart.Utilities.isPageBrickSafe() == true) {
        if (ad.width == 300 && ad.height == 250) {
            if ((Math.random() * 100) < MediaSmart.Utilities.brick_percentage) {
                ad.height = 600;
                iframeId = " id='zmsBrick' ";
                MediaSmart.Utilities.brick_present = true;
            }
        }
    }
    
    
    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' ";
        adCallString += iframeId;
        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 
        adCallString += "/AREA=" + MediaSmart.area;
    adCallString += "/AAMSZ=" + ad.width + "x" + ad.height;
    if (ad.position != "" && typeof(ad.position) != "undefined") {
        adCallString += "/POSITION=";
        if (ad.abf != false && polite != false) {
            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();
        }
    }
    adCallString += "/pageid=" + MediaSmart.pageId;
    if (polite) 
        adCallString += "'></iframe>\n";
    else 
        adCallString += "'></script>\n";
    
    
    return adCallString;
}

MediaSmart.insertAd = function(ad, elementId){
	MediaSmart.Utilities.manualURLTargeting();
	
	//fix to allow expanding rich media
	var zmsobj = document.getElementById("dnn_SideBar");
	if(zmsobj){
		zmsobj.style.overflow = "visible";
	}
	
    //Testing environment
    parntLocation = parent.document.location + "";
	
	//NRLKids Targeting fix
	if(parntLocation.indexOf("NRLKids")!= -1 || parntLocation.indexOf("kids")!= -1){
		MediaSmart.site = "TEL.NRL.SPORT.KIDS";
		MediaSmart.area = "BIGPOND.KIDS.HOME";
	}
	
    if (parntLocation.indexOf("zmstestingenvironment375") != -1) {
        MediaSmart.area = "MOB.TEST.DEMO";
        MediaSmart.site = "Z_ZORRO";
		MediaSmart.Utilities.debug = true;
    }
	//Manual State of Origin Targeting
	parntLocation = parntLocation.toUpperCase();
	if((parntLocation.indexOf("STATEOFORIGIN")!= -1 || parntLocation.indexOf("GLADIATORS")!= -1)&& parntLocation.indexOf("LIVESCOREBOARD")== -1){
		MediaSmart.area = "SPORT.NRL.REPGAMES.STATEOFORIGIN";
	}
    //DoubleClick Testing
    if (parntLocation.indexOf("doubleclick_testingenvironment_jscript182") != -1) {
        MediaSmart.area = "MOBILE.BOOMERANG.OTHER";
        MediaSmart.site = "Z_ZORRO";
    }
    if (parntLocation.indexOf("doubleclick_testingenvironment_iframe182") != -1) {
        MediaSmart.area = "MOBILE.BOOMERANG.CHEAPMOBILES";
        MediaSmart.site = "Z_ZORRO";
    }
    
  
	if(parntLocation.indexOf("Fantasy")!= -1 || parntLocation.indexOf("id=53760") != -1 || parntLocation.indexOf("DreamTeam") != -1 || parntLocation.indexOf("10413") != -1 || parntLocation.indexOf("id=53816") != -1){
		MediaSmart.area = "SPORT.NRL.DREAMTEAM";
	}
    
	// Hack 1/2 to make abf work since NRL does not call insertallads. 2nd hack at line 287
	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;
	}
	// end hack
	
    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) != null) {
						if ((document.getElementById(ad[i].containerId).parentNode.parentNode != null)) {
							//document.getElementById(ad[i].containerId).parentNode.parentNode.style.overflow = "visible";
						}
						
						// Hack 2/2 to make abf work since NRL does not call insertallads
						element = document.getElementById(ad[i].containerId);
				        ad[i].top = element.offsetTop;
				        while (element = element.offsetParent) {
				            ad[i].top += element.offsetTop;
				        }
						// end hack				
						
						document.getElementById(ad[i].containerId).innerHTML = MediaSmart.Utilities.generateAdCall(ad[i]);
					}
                }
            }
            else 
                document.getElementById(ad.containerId).innerHTML = MediaSmart.Utilities.generateAdCall(ad);
        }
    }
    
    
    if (MediaSmart.Utilities.brick_present) {
		//MediaSmart.Utilities.updateBrickLayout();
    }
    
    
    
    
}
MediaSmart.Utilities.updateBrickLayout = function(){
    var brickObj = document.getElementById("zmsBrick");
    if (brickObj != "null") {
        var parentObj = brickObj.parentNode;
        var parentId = "zmsBrickHolder";
        parentObj.parentNode.setAttribute("id", parentId);
        parentObj.style.height = "600px";
        //parentObj.style.border = "solid 1px black";
        
        var columnObject = parentObj.parentNode.parentNode;
        var numberOfSiblings = columnObject.childNodes.length;
        columnObject.style.height = (250 * numberOfSiblings) + "px";
        //columnObject.style.border= "solid 1px red";
        
        //console.log("There are %s siblings.", numberOfSiblings)
        for (i = 0; i < numberOfSiblings; i++) {
            if (columnObject.childNodes[i].getAttribute("id") != parentId) {
                //	console.log(columnObject.childNodes[i].offsetTop)
                columnObject.childNodes[i].style.top = (columnObject.childNodes[i].offsetTop + 350) + "px";
                //columnObject.childNodes[i].style.border = "solid 1px green";
            }
        }
    }
}


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(){
	//Refresh Page ID
	MediaSmart.pageId = MediaSmart.Utilities.random();
	
    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]);
    }
}
MediaSmart.Utilities.initialise();



MediaSmart.Utilities.manualURLTargeting = function(){
	var zmsLocation = parent.document.location + "";
	zmsLocation = zmsLocation.toUpperCase();
	
	//STATS Targeting
	if(zmsLocation.indexOf("TELSTRAPREMIERSHIP/CLUBSTATS")!= -1){
		MediaSmart.area = "SPORT.NRL.STATS";
		
	}
	
	//LADDER Targeting 
	if(zmsLocation.indexOf("TELSTRAPREMIERSHIP/LADDER/")!= -1){
		MediaSmart.area = "SPORT.NRL.LADDER";	
	}
	
}


//MediaSmart.setRefresh(60);


//adapter for bp header
function zMSgetJS(zMSString){
    MediaSmart.newAd({
        id: "100x29",
        width: 100,
        height: 29,
        position: 1,
        polite: false
    });
}