MediaSmart = new Object();
MediaSmart.Utilities = new Object();
MediaSmart.ads = new Array();

MediaSmart.site = "3RD.REALESTATEVIEW.REALESTATE";
MediaSmart.areaPattern = "REALESTATE.REALESTATEVIEW.$";
MediaSmart.subArea = "RESIDENTIAL";

//Are ads polite loaded? Default is true
MediaSmart.Utilities.polite = true;
//Above and Below the fold targeting. Default is false
MediaSmart.Utilities.aboveBelowFold = true;
MediaSmart.Utilities.remarketing = true;


MediaSmart.Utilities.initialise = function(){
	if (arguments.length > 0) {
		var zmsJson = arguments[0];
		if (zmsJson.area != "" && typeof(zmsJson.area) == "string") {
			var zmsString = MediaSmart.Utilities.makeURLSafe(zmsJson.area);
				MediaSmart.subArea = zmsString.toUpperCase();
		}
		if (zmsJson.site != ""&& typeof(zmsJson.site) == "string") {
			var zmsString = MediaSmart.Utilities.makeURLSafe(zmsJson.site);
			MediaSmart.site = zmsString.toUpperCase();
		}
				if(zmsJson.tags != null){
						MediaSmart.tags = zmsJson.tags;
				}
	}

		//generate area
		MediaSmart.area = MediaSmart.areaPattern.replace(/\$/g,MediaSmart.subArea);


	MediaSmart.pageId = MediaSmart.Utilities.random();
	if ((document.location + "").indexOf("https://") != -1){
		MediaSmart.domain = "https://media.sensis.com.au";
				MediaSmart.protocol = "https";
				MediaSmart.Utilities.remarketing = false;
		}else{
			MediaSmart.domain = "http://media.sensis.com.au";
				MediaSmart.protocol = "http";
		}

	if ((document.location + "").indexOf("MSTOOTHPASTE") != -1) {
		MediaSmart.keyword = "MSTOOTHPASTE";
	}
	if ((document.location + "").indexOf("zmstestingenvironment375") != -1) {
				MediaSmart.site = "Z_ZORRO";
				MediaSmart.area = "MOB.TEST.DEMO";
	}
	
	if (typeof(MediaSmart.tags['STATE']) != "undefined" && MediaSmart.tags['STATE'] != ""){
			MediaSmart.tags['LOCATION'] = MediaSmart.tags['STATE'];
			MediaSmart.tags['STATE'] = "";
	}
}

MediaSmart.newAd = function(json){
		if(MediaSmart.Utilities.aboveBelowFold == false){
				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.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) || MediaSmart.Utilities.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 islands leaderboard and skys
		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;
	adCallString += "/AAMSZ=" + ad.width + "x" + ad.height;
	if (ad.area != "" && typeof(ad.area) != "undefined")
		adCallString += "/AREA=" + ad.area;
	else {
		if( window.location.href == "http://www.realestateview.com.au/" || window.location.href == "http://www.realestateview.com.au" ) {
			MediaSmart.area = "REALESTATE.REALESTATEVIEW.HOME";
		}
		else if( window.location.href == "http://realestateview.com.au/" || window.location.href == "http://realestateview.com.au" ) {
			MediaSmart.area = "REALESTATE.REALESTATEVIEW.HOME";
		}
		else if( window.location.href == "http://www.realestateview.com.au/Real-Estate/Residential/Rent.html" ) {
			MediaSmart.area = "REALESTATE.REALESTATEVIEW.RESIDENTIAL.RENTHOME";
		}
		else if( window.location.href == "http://realestateview.com.au/Real-Estate/Residential/Rent.html" ) {
			MediaSmart.area = "REALESTATE.REALESTATEVIEW.RESIDENTIAL.RENTHOME";
		}		
		else if( window.location.href == "http://www.realestateview.com.au/Real-Estate/Commercial/Buy.html" ) {
			MediaSmart.area = "REALESTATE.REALESTATEVIEW.COMMERCIAL.BUYHOME";
		}
		else if( window.location.href == "http://realestateview.com.au/Real-Estate/Commercial/Buy.html" ) {
			MediaSmart.area = "REALESTATE.REALESTATEVIEW.COMMERCIAL.BUYHOME";
		}
		else if( window.location.href == "http://www.realestateview.com.au/Real-Estate/Commercial/Rent.html" ) {
			MediaSmart.area = "REALESTATE.REALESTATEVIEW.COMMERCIAL.LEASEHOME";
		}
		else if( window.location.href == "http://realestateview.com.au/Real-Estate/Commercial/Rent.html" ) {
			MediaSmart.area = "REALESTATE.REALESTATEVIEW.COMMERCIAL.LEASEHOME";
		}		
		else if( window.location.href == "http://www.realestateview.com.au/Real-Estate/Rural/Buy.html" ) {
			MediaSmart.area = "REALESTATE.REALESTATEVIEW.RURALVIEW.BUYHOME";
		}
		else if( window.location.href == "http://realestateview.com.au/Real-Estate/Rural/Buy.html" ) {
			MediaSmart.area = "REALESTATE.REALESTATEVIEW.RURALVIEW.BUYHOME";
		}
		else if( window.location.href == "http://www.realestateview.com.au/Real-Estate/Business/Buy.html" ) {
			MediaSmart.area = "REALESTATE.REALESTATEVIEW.BUSINESSESFORSALE.BUYHOME";
		}
		else if( window.location.href == "http://realestateview.com.au/Real-Estate/Business/Buy.html" ) {
			MediaSmart.area = "REALESTATE.REALESTATEVIEW.BUSINESSESFORSALE.BUYHOME";
		}		
		else if( window.location.href == "http://www.realestateview.com.au/holiday-rentals" ) {
			MediaSmart.area = "REALESTATE.REALESTATEVIEW.HOLIDAY.RENTHOME";
		}
		else if( window.location.href == "http://realestateview.com.au/holiday-rentals" ) {
			MediaSmart.area = "REALESTATE.REALESTATEVIEW.HOLIDAY.RENTHOME";
		}
		
		if( MediaSmart.area == "REALESTATE.REALESTATEVIEW.RESIDENTIAL.DETAILS" ) {
			if( MediaSmart.tags['BUYSELLRENT'] != "undefined" ) {
				if( MediaSmart.tags['BUYSELLRENT'] == "BUY" ) {
					MediaSmart.area = "REALESTATE.REALESTATEVIEW.RESIDENTIAL.BUYDETAILS";
				}
				else if( MediaSmart.tags['BUYSELLRENT'] == "RENT" ) {
					MediaSmart.area = "REALESTATE.REALESTATEVIEW.RESIDENTIAL.RENTDETAILS";
				}	
			}
		}
		
		
	/*
		if( window.location.href.indexOf("search")!=-1 && MediaSmart.area.indexOf(".RESULTS")==-1 ) {
			MediaSmart.area = MediaSmart.area + ".RESULTS";
		}
		else if( window.location.href.indexOf("Details")!=-1 && MediaSmart.area.indexOf(".DETAILS")==-1 ) {
			MediaSmart.area = MediaSmart.area + ".DETAILS";
		}
	*/
		adCallString += "/AREA=" + MediaSmart.area;
	}
	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;
	}

	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, "_");
	
	// Fix to prevent Central Equity ads showing on Melcorp pages. Added 08/06/2011
	if( (document.title+"").indexOf( "Melcorp" ) != -1 ) {
		if( adCallString.indexOf( "KEYWORD=" ) == -1 )
			adCallString += "/KEYWORD=MELCORP";
		else
			adCallString += "+MELCORP";
	}

	if( (document.location + "").indexOf("quicksearch=0&bs=&rg=Melbourne&sub=&sur=") != -1 ) {
		MediaSmart.tags.pcode = "3000";
	}	
			
	/*
	if( document.location == "http://www.realestateview.com.au/portal/search?rm=search&P=1&ptr=r&cs=vic&rgr=mel&con=S&portalview=residential&portalsection=buy&green=0&quicksearch=0&bs=&rg=Melbourne&sub=&sur=&prl=0&prh=0&bel=0&beh=0&bal=0&bah=0&kw=&sor=listed&view_alert_email" )
		MediaSmart.tags.pcode = "3000";
	else if( document.location == "http://realestateview.com.au/new-apartments" )
		MediaSmart.tags.pcode = "3000";
	else if( document.location == "http://realestateview.com.au/new-apartments/victoria" )
		MediaSmart.tags.pcode = "3000";
	else if( document.location == "http://realestateview.com.au/portal/search?rm=search&P=1&ptr=r&cs=vic&rgr=mel&con=S&portalview=residential&portalsection=buy&green=0&quicksearch=0&bs=&pt=unt&pt=apt&pt=pen&pt=stu&pt=vil&ybl=2010&rg=Melbourne&sub=&sur=&prl=0&prh=0&bel=0&beh=0&bal=0&bah=0&kw=&sor=listed&view_alert_email" )
		MediaSmart.tags.pcode = "3000";
	*/
			
	for (var key in MediaSmart.tags) {

		if (typeof(ad.tags[key]) == "undefined") {
				if (key == "BUYSELLRENT" ) {
					adCallString += "/" + MediaSmart.Utilities.makeURLSafe("CATEG") + "=" + MediaSmart.Utilities.makeURLSafe(MediaSmart.tags[key].toUpperCase());
				}
				else if( key == "PROPTYP" ) {
					adCallString += "/" + MediaSmart.Utilities.makeURLSafe("SITE_CONTENT") + "=" + MediaSmart.Utilities.makeURLSafe(MediaSmart.tags[key].toUpperCase());
				}
				else if( key == "MINPRICE" ) {
					var minPrice = MediaSmart.tags[key];
					var leadingZeros = 10 - minPrice.length;
					adCallString += "/MINPRICE=";
					var indexCounter = 0;
					for( indexCounter = 0; indexCounter<leadingZeros; indexCounter++ )
						adCallString += "0";
					adCallString += minPrice;
				}
				else if( key == "MAXPRICE" ) {
					var maxPrice = MediaSmart.tags[key];
					var leadingZeros = 10 - maxPrice.length;
					adCallString += "/MAXPRICE=";
					var indexCounter = 0;
					for( indexCounter = 0; indexCounter<leadingZeros; indexCounter++ )
						adCallString += "0";
					adCallString += maxPrice;
				}
				else if( key == "YEARSTART" ) {
					var yearEnd = MediaSmart.tags[key];
					var leadingZeros = 10 - yearEnd.length;
					adCallString += "/YEARSTART=";
					var indexCounter = 0;
					for( indexCounter = 0; indexCounter<leadingZeros; indexCounter++ )
						adCallString += "0";
					adCallString += yearEnd;
				}
				else if( key == "YEAREND" ) {
					var yearEnd = MediaSmart.tags[key];
					var leadingZeros = 10 - yearEnd.length;
					adCallString += "/YEAREND=";
					var indexCounter = 0;
					for( indexCounter = 0; indexCounter<leadingZeros; indexCounter++ )
						adCallString += "0";
					adCallString += yearEnd;
				}
				else if( key == "REGION" ) {
					adCallString += "/TRAVELREGION=";
					adCallString += MediaSmart.tags[key];
				}
				else if (MediaSmart.tags[key] != "")
					adCallString += "/" + MediaSmart.Utilities.makeURLSafe(key.toUpperCase()) + "=" + MediaSmart.Utilities.makeURLSafe(MediaSmart.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";

	MediaSmart.logValues( adCallString );
	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(){
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.Utilities.makeURLSafe = function(text){
	text = text.replace(/ |-|<|>|\/|\?|\+/g, "_");
		text = text.replace(/\&AMP\;/g, "AND");
	text = text.replace(/&/g, "AND");
	text = text.replace(/_+/g, "_");
	text = text.replace(/^_|_$/g, "");
	return text;
}

MediaSmart.doCookieFix = function(ad) {
	if( MediaSmart.Utilities.remarketing == false )
		return false;	
	
	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.width == 120) && ad.height == 600)
		return true;
	else if(ad.width == 468 && ad.height == 60)
		return true;
		
	return false;
}


MediaSmart.logValues = function( adCall ) {
	if( Math.floor(Math.random()*1000) != 0 )
		return;
	
	var sdmLogTags = adCall.match( /src='.*'/ ) + "";
	startTagsIndex = sdmLogTags.indexOf( "acc_random=" );
	if( startTagsIndex == -1 )
		return;
	sdmLogTags = sdmLogTags.substring( startTagsIndex, sdmLogTags.length - 1 );
	sdmLogTags = sdmLogTags.replace( /\//g, "&" );
	sdmLogTags = sdmLogTags.toUpperCase();
	
	// Pass tags to tag logger
	var tagLogger = new Image();
	tagLogger.src = "http://mediasmart-apps.com/taglogger/logtags.php?" + sdmLogTags;
}














