
var trs_ad_xss = new function() {
	var BASE_URL = 'http://www.thatrentalsite.com';
	var CONTENT_URL = BASE_URL + '/rentalAdsRequest.js';
	var MIN_AD_WIDTH = 120;
	var MAX_AD_COUNT = 10;
	var SWITCH_WIDTH = 300;
	
	var isIE6 = ( navigator.appName=="Microsoft Internet Explorer" && navigator.appVersion.indexOf("MSIE 6") != -1 )?true:false;
	var isIE7 = ( navigator.appName=="Microsoft Internet Explorer" && navigator.appVersion.indexOf("MSIE 7") != -1 )?true:false;
	
	this.init = function(divId){
		var ROOT = divId ? divId : "trs_advertisement" + Math.round( Math.random()*1000 );

		//Setup ad state
		if( typeof(trs_show_alerts) == "undefined" || trs_show_alerts == null ) trs_show_alerts = false;

		if( typeof trs_ad_client == "undefined" || !isSet(trs_ad_client) ){
			if( trs_show_alerts == true ) alert("To use thatRentalSite.com PropSense, you must set `trs_ad_client`.");
			return;
		}

		if( ( typeof(trs_city) == "undefined" || !isSet(trs_city) ) || ( typeof(trs_state) == "undefined" || !isSet(trs_state) ) ){
			if( trs_show_alerts == true ) alert("trs_city and trs_state must be set in order to use PropSense.");
			return;
		}
		

		if( typeof(trs_color_border) == "undefined" || !isSet(trs_color_border) ) 	trs_color_border = "black";
		if( typeof(trs_color_bg) == "undefined" 	|| !isSet(trs_color_bg) ) 		trs_color_bg = "white";
		if( typeof(trs_color_font) == "undefined" 	|| !isSet(trs_color_font) )  	trs_color_font = "black";
		if( typeof(trs_color_hover) == "undefined" 	|| !isSet(trs_color_hover) ) 	trs_color_hover = "#DDDDFF";
		
		if( typeof(trs_ad_count) == "undefined" || !isSet(trs_ad_count) )  	trs_ad_count = "5";		
		if( typeof(trs_ad_set) == "undefined" 	|| trs_ad_set == null )		trs_ad_set = "";

		if( typeof(trs_show_image) == "undefined" 	|| trs_show_image == null )	trs_show_image = true;

		if( typeof(trs_search) == "undefined" 				|| trs_search == null ) 				trs_search = false;
		if( typeof(trs_search_property) == "undefined" 		|| trs_search_property == null ) 		trs_search_property = "";
		if( typeof(trs_search_lat) == "undefined" 			|| trs_search_lat == null ) 			trs_search_lat = -1;
		if( typeof(trs_search_lon) == "undefined" 			|| trs_search_lon == null ) 			trs_search_lon = -1;
		if( typeof(trs_search_radius) == "undefined" 		|| trs_search_radius == null ) 			trs_search_radius = 25;
		if( typeof(trs_search_rent_max) == "undefined" 		|| trs_search_rent_max == null ) 		trs_search_rent_max = -1;
		if( typeof(trs_search_beds_min) == "undefined" 		|| trs_search_beds_min == null ) 		trs_search_beds_min = -1;
		if( typeof(trs_search_leaseTerm_max) == "undefined" || trs_search_leaseTerm_max == null ) 	trs_search_leaseTerm_max = -1;
		if( typeof(trs_search_movein_date) == "undefined" 	|| trs_search_movein_date == null ) 	trs_search_movein_date = -1;
		if( typeof(trs_search_pets) == "undefined" 			|| trs_search_pets == null ) 			trs_search_pets = false;
		if( typeof(trs_search_smoking) == "undefined" 		|| trs_search_smoking == null ) 		trs_search_smoking = false;
		
		if( typeof(trs_autofill_name) == "undefined" 		|| trs_autofill_name == null ) 		trs_autofill_name = "";
		if( typeof(trs_autofill_email) == "undefined" 		|| trs_autofill_email == null ) 	trs_autofill_email = "";
		if( typeof(trs_autofill_phone) == "undefined" 		|| trs_autofill_phone == null ) 	trs_autofill_phone = "";
		if( typeof(trs_autofill_tm_tenant) == "undefined" 	|| trs_autofill_tm_tenant == null ) trs_autofill_tm_tenant = false;


		if( typeof(trs_ad_width) == "undefined" || !isSet(trs_ad_width) )  	trs_ad_width = MIN_AD_WIDTH + "px";
		if( parseInt( trs_ad_width ) < MIN_AD_WIDTH ){
			if( trs_show_alerts == true ) alert("trs_ad_width must be >= " + MIN_AD_WIDTH);
			trs_ad_width = MIN_AD_WIDTH + "px";
		}

		if( trs_ad_count > MAX_AD_COUNT ){
			if( trs_show_alerts == true ) alert("trs_ad_count must be <= " + MAX_AD_COUNT);
			trs_ad_count = MAX_AD_COUNT;
		}
		if( trs_ad_count <= 0 ) return;

		if(!divId)document.write("<div id='" + ROOT + "' style='display: none;'>&nbsp;</div>");

		//save state
		rootNode = document.getElementById(ROOT);
		rootNode.trs_city = trs_city;
		rootNode.trs_state = trs_state;
		
		rootNode.trs_ad_client = trs_ad_client;
		rootNode.trs_ad_width = trs_ad_width;
		rootNode.trs_ad_count = trs_ad_count;
		rootNode.trs_ad_set = trs_ad_set;
		
		rootNode.trs_color_border = trs_color_border;
		rootNode.trs_color_bg = trs_color_bg;
		rootNode.trs_color_font = trs_color_font;
		rootNode.trs_color_hover = trs_color_hover;
		
		rootNode.trs_show_alerts = trs_show_alerts;
		rootNode.trs_show_image = trs_show_image;

		rootNode.trs_search = trs_search;
		rootNode.trs_search_property = trs_search_property;
		rootNode.trs_search_lat = trs_search_lat;
		rootNode.trs_search_lon = trs_search_lon;
		rootNode.trs_search_radius = trs_search_radius;
		rootNode.trs_search_rent_max = trs_search_rent_max;
		rootNode.trs_search_beds_min = trs_search_beds_min;
		rootNode.trs_search_leaseTerm_max = trs_search_leaseTerm_max;
		rootNode.trs_search_movein_date = trs_search_movein_date;
		rootNode.trs_search_pets = trs_search_pets;
		rootNode.trs_search_smoking = trs_search_smoking;
		
		rootNode.trs_autofill_name = trs_autofill_name;
		rootNode.trs_autofill_email = trs_autofill_email;
		rootNode.trs_autofill_phone = trs_autofill_phone;
		rootNode.trs_autofill_tm_tenant = trs_autofill_tm_tenant;

		requestContent(ROOT);
	}
	
	function requestContent( rootId ) {
		var script = document.createElement('script');
		script.src = CONTENT_URL + '?client_id=' + trs_ad_client 
			+ '&city=' + trs_city + '&state=' + trs_state 
			+ '&search=' + trs_search
			+ '&search_property=' + trs_search_property
			+ '&search_lat=' + trs_search_lat
			+ '&search_lon=' + trs_search_lon
			+ '&search_radius=' + trs_search_radius
			+ '&search_rent_max=' + trs_search_rent_max
			+ '&search_beds_min=' + trs_search_beds_min
			+ '&search_leaseterm_max=' + trs_search_leaseTerm_max
			+ '&search_movein_date=' + trs_search_movein_date
			+ '&search_pets=' + trs_search_pets
			+ '&search_smoking=' + trs_search_smoking
			+ '&autofill_name=' + trs_autofill_name
			+ '&autofill_email=' + trs_autofill_email
			+ '&autofill_phone=' + trs_autofill_phone
			+ '&autofill_tm_tenant=' + trs_autofill_tm_tenant
			+ "&ad_count=" + trs_ad_count + "&rootId=" + rootId 
			+ '&url=' + escape(location.href) + "&ad_set=" + trs_ad_set + "&json";
		document.getElementsByTagName('head')[0].appendChild(script);
	}

	this.serverResponse = function( dataArray ) {
		if (!dataArray) alert("PropSense data retrieval is null");
		
		if( dataArray.length <= 2 ) return; //no ad results were restrieved
		
		var rootNode = document.getElementById( dataArray[0] );
		var impressionId = dataArray[1];
		
		if( impressionId == "" || rootNode == "" ) alert("Error retrieving critical PropSense data");
		
		//load state
		trs_city = rootNode.trs_city;
		trs_state = rootNode.trs_state;
		trs_show_image = rootNode.trs_show_image;
		trs_color_border = rootNode.trs_color_border;
		trs_color_bg = rootNode.trs_color_bg;
		trs_color_font = rootNode.trs_color_font;
		trs_color_hover = rootNode.trs_color_hover;
		trs_ad_width = rootNode.trs_ad_width;
		trs_ad_count = rootNode.trs_ad_count;
		trs_ad_set = rootNode.trs_ad_set;
		trs_ad_client = rootNode.trs_ad_client;
		trs_show_alerts = rootNode.trs_show_alerts;
		
		var bSwitch = (parseInt(trs_ad_width) >= SWITCH_WIDTH);
		
		if(trs_show_image) rootNode.style.background = "white url('" + BASE_URL + "/aptsrentals/i/sign-for_rent.png') no-repeat top left";
		
		rootNode.style.padding = "0 15px";
		rootNode.style.width = parseInt(trs_ad_width);
		rootNode.style.font = "8pt tahoma";
		//rootNode.style.border = "1px solid black";
		
		adHeaderElmnt = document.createElement("div");
		
		adHeaderElmnt.style.margin = "22px 0 0 -10px";
		adHeaderElmnt.align = "left";
		if(bSwitch){
			adHeaderElmnt.style.height = parseInt( Math.round( (dataArray.length-1)/parseInt((parseInt(trs_ad_width)-53)/120.0) ) * 116 - 15 ) + "px";
			if( !isIE6 ) adHeaderElmnt.style.width = "53px";
			else adHeaderElmnt.style.width = "70px";
			adHeaderElmnt.style.cssFloat = "left";
			adHeaderElmnt.style.styleFloat = "left";
		}
		else{
			adHeaderElmnt.style.height = "48px";
		}
		//adHeaderElmnt.style.border = "1px solid black";
		
		signTextElmnt = document.createElement("div");
		signTextElmnt.style.fontWeight = "bold";
		signTextElmnt.style.lineHeight = "10px";
		if( isIE6 && bSwitch ) signTextElmnt.style.paddingLeft = "13px";
		else signTextElmnt.style.paddingLeft = "1px";
		signTextElmnt.style.textAlign = "center";
		signTextElmnt.style.width = "43px";
		
		if(bSwitch) signTextElmnt.style.height = "60px";
		else signTextElmnt.style.height = "30px";
		
		if( trs_city.indexOf(" ") != -1 ){
			var line1 = trs_city.substring( 0, trs_city.lastIndexOf(" ") );
			var line2 = trs_city.substring( trs_city.lastIndexOf(" ")+1, trs_city.length );
			signTextElmnt.innerHTML = line1 + "<br/>" + line2;
		}
		else signTextElmnt.appendChild( document.createTextNode(trs_city) );
		
		adHeaderElmnt.appendChild(signTextElmnt);
		
		websiteLinkElmnt = document.createElement("div");
		websiteLinkElmnt.style.fontSize = "8pt";
		if(bSwitch){
			websiteLinkElmnt.style.paddingLeft = "46px";
			//websiteLinkElmnt.style.width = "";
		}
		else{
			websiteLinkElmnt.style.paddingLeft = "3px";
			websiteLinkElmnt.style.width = "133px";
		}
		websiteLinkElmnt.align = "left";
		
		anchorElmnt = document.createElement("a");
		anchorElmnt.href = BASE_URL + "/adClick?impression_id=" + impressionId + "&position=0" + "&type=ad" + "&url=" + escape(BASE_URL);
		
		anchorElmnt.target = "_blank";
		anchorElmnt.appendChild( document.createTextNode("ThatRentalSite.com") );
		
		if(bSwitch){
			websiteLinkElmnt.appendChild( anchorElmnt );
			var boldElmnt = document.createElement("b");
			boldElmnt.appendChild( document.createTextNode(" Rentals Available Now") );
			websiteLinkElmnt.appendChild( boldElmnt );

			rootNode.appendChild(adHeaderElmnt);
			rootNode.appendChild(websiteLinkElmnt);
		} else{
			websiteLinkElmnt.appendChild( document.createTextNode("from ") );
			websiteLinkElmnt.appendChild( anchorElmnt );

			adHeaderElmnt.appendChild( websiteLinkElmnt );
			rootNode.appendChild(adHeaderElmnt);
		}

		for(var i=2;i<dataArray.length;i++ ){
			var dataMap = dataArray[i];
			
			containerElmnt = document.createElement("div");
			containerElmnt.style.display = "block";
			containerElmnt.style.cssFloat = "left";
			containerElmnt.style.styleFloat = "left";
			//containerElmnt.style.margin = "5px 5px 10px 5px";
			containerElmnt.style.width = "110px";
			containerElmnt.style.padding = "5px";
			containerElmnt.style.cursor = "pointer";
			containerElmnt.hoverColor = trs_color_hover;
			containerElmnt.onmouseover = function(){ this.style.backgroundColor = this.hoverColor; };
			containerElmnt.onmouseout = function(){ this.style.backgroundColor = 'transparent'; };

			centerElmnt = document.createElement("center");
			containerElmnt.appendChild( centerElmnt );

			imgContainerElmnt = document.createElement("div");
			imgContainerElmnt.style.border = "1px solid " + trs_color_border;
			//imgContainerElmnt.style.padding = "2px";
			imgContainerElmnt.style.backgroundColor = trs_color_bg;
			imgContainerElmnt.style.cursor = "pointer";
			imgContainerElmnt.style.width = "104px";
			imgContainerElmnt.style.height = "104px";

			imageElmnt = document.createElement("div");
			//imageElmnt.style.border = "1px solid black";
			imageElmnt.style.align = "center";
			imageElmnt.style.margin = "2px";
			imageElmnt.style.background = trs_color_bg + " url('" + BASE_URL + dataMap["image"] + "') no-repeat";
			imageElmnt.style.width = "100px";
			imageElmnt.style.height = "100px";
			//imageElmnt.style.paddingTop = "75px";
	
			imgContainerElmnt.appendChild( imageElmnt );
	
			spacerLinkElmnt = document.createElement("div");
			spacerLinkElmnt.style.width = "100px";
			spacerLinkElmnt.style.height = "75px";
			if(dataMap["link"].substring(0,7) == "http://") spacerLinkElmnt.linkage = dataMap["link"];
			else spacerLinkElmnt.linkage = BASE_URL + dataMap["link"];
			spacerLinkElmnt.onclick = function(){ window.open( this.linkage ); };

			imageElmnt.appendChild(spacerLinkElmnt);
	
			anchorElmnt = document.createElement("a");
			if(dataMap["link"].substring(0,7) == "http://") anchorElmnt.href = dataMap["link"];
			else anchorElmnt.href = BASE_URL + dataMap["link"];
			anchorElmnt.target = "_blank";
			anchorElmnt.style.display = "block";
			anchorElmnt.style.color = trs_color_font;
			anchorElmnt.style.textDecoration = 'none';
			anchorElmnt.style.backgroundColor = trs_color_bg;
			anchorElmnt.style.width = "100px";
			anchorElmnt.style.lineHeight = "10px";
			anchorElmnt.style.padding = "2px 0 3px 0";

			anchorElmnt.appendChild( document.createTextNode(dataMap["type"]) );
			anchorElmnt.appendChild( document.createElement("br") );
			anchorElmnt.appendChild( document.createTextNode(dataMap["city"]) );
	
			imageElmnt.appendChild( anchorElmnt );
	
			centerElmnt.appendChild( imgContainerElmnt );				
			rootNode.appendChild( containerElmnt );
		
		}
		
		var clearDiv = document.createElement("div");
		clearDiv.style.clear = "both";
		rootNode.appendChild( clearDiv );
		
		anchorElmnt = document.createElement("a");
		anchorElmnt.href = BASE_URL + "/adClick?impression_id=" + impressionId + "&position=1" + "&type=ad" + "&url=" + escape(BASE_URL + "/For-Rent/" + escape(trs_city.replace(" ", "-")) + "-" + trs_state + ".html");
		anchorElmnt.target = "_blank";
		anchorElmnt.style.display = "block";
		
		if( ( isIE6 || isIE7 ) ){ 
			if(!bSwitch) anchorElmnt.style.margin = "5px -10px 5px 20px";
			else anchorElmnt.style.margin = "5px -10px 5px 0px";
		}
		else anchorElmnt.style.margin = "5px 0 5px 0";
		anchorElmnt.style.textAlign = "center";
		anchorElmnt.style.width = "100%";
		anchorElmnt.appendChild( document.createTextNode("View all") );

		if(bSwitch) adHeaderElmnt.appendChild( anchorElmnt );
		else rootNode.appendChild( anchorElmnt );

		anchorElmnt = document.createElement("a");
		anchorElmnt.href = BASE_URL + "/adClick?impression_id=" + impressionId + "&position=2" + "&type=ad" + "&url=" + escape(BASE_URL  + "/post?city=" + escape(trs_city) + "&state=" + trs_state);
		anchorElmnt.target = "_blank";
		anchorElmnt.style.display = "block";
		if( ( isIE6 || isIE7 ) ){
			if( !bSwitch ) anchorElmnt.style.margin = "5px -10px 5px 20px";
			else anchorElmnt.style.margin = "5px -10px 5px 0px";
		}
		else anchorElmnt.style.margin = "5px 0 5px 0";
		anchorElmnt.style.textAlign = "center";
		anchorElmnt.style.width = "100%";
		if( isIE6 || isIE7 ) anchorElmnt.appendChild( document.createElement("br") );
		anchorElmnt.appendChild( document.createTextNode("Advertise here") );

		if(bSwitch){
			var spacerDiv = document.createElement("div");
			var spacerHeight = parseInt( adHeaderElmnt.style.height ) - 99 - 15;
			if( spacerHeight < 0 ){
				if( isIE6 || isIE7 ) anchorElmnt.style.marginTop = "-15px";
				spacerHeight = 0;
			}
			spacerDiv.style.height = spacerHeight + "px";
			adHeaderElmnt.appendChild( spacerDiv );
			adHeaderElmnt.appendChild( anchorElmnt );
		}
		else rootNode.appendChild( anchorElmnt );
		
		rootNode.style.display = 'block';
	}

	function isSet( variable ){ return !( variable == null || variable == "" ); }
}

