var okToSend = true;
var mindiff = .005;
var order_by = 'lp_Listing_Price';
var up_down = "ASC";

// tile stuff
var resolution;
var container;
var mapContainer;
var ds = 'ms_search_bunbury';
var max_results = "100";
var rows = 256;
var cols = 256;
var max_scale = 9;
var cache = "wi";
var cache_origin_x = "-93.0001";
var cache_origin_y = "48.14167";

function setEnvelope(ix, iy, ax, ay) 
{
	minx = ix;
	miny = iy;
	maxx = ax;
	maxy = ay;

	scale = "";

	getMap("");

}

function getInitialMap(){
	minx = saved_minx;
	miny = saved_miny;
	maxx = saved_maxx;
	maxy = saved_maxy;

	container = document.getElementById('mapArea');
        
        mapContainer = getElmById('mapContainer');

	getMap("");

}

function write_debug(str)
{
	if (document.getElementById('ta_debug') != null)
	{
		document.getElementById('ta_debug').value += str + '\n';
	}
}


function parseLayers(){
  if (http.readyState == 4) {
  
   if (http.status == 200) {

      var result = http.responseText;
      getLayers(result); 
      getInitialMap();
   } else alert("Error retreiving data");
          
  }  
  
 }

function fixedZoomIn(){
        if (parseInt(scale) < max_scale) {
                scale = parseInt(scale) + 1;
                fixedzoom = "true";
                getMap("");
        } else {
                alert("Currently at min zoom : " + scale);
        }
}

function fixedZoomOut(){
        if (parseInt(scale) > 0) {
                scale = parseInt(scale) - 1;
                fixedzoom = "true";
                getMap("");
        } else {
                alert ("Currently at max zoom : " + scale);
        }
}

function zoom(left, bottom, right, top){
     	if (getExtentForZoomIn(left, bottom, right, top)) {
     		getMap("");	
	}
}
     
function dragObjects(dx, dy)
{         
        for (var i = 0; i < container.childNodes.length;i++) {
                var node = container.childNodes[i];
                var cur_left = node.style.left;
                var cur_top = node.style.top;
                var l = parseInt(cur_left.substring(0, cur_left.length));
                var t = parseInt(cur_top.substring(0, cur_top.length));
                
                node.style.left = parseInt(l +  dx) + "px";
                node.style.top = parseInt(t + dy) + "px";
        } 
                
        if (isIE) {
                i = 1;
        } else {
                i = 3;
        }
                
        for (; i < mapContainer.childNodes.length; i++) {
                var node = mapContainer.childNodes[i];
                var cur_left = node.style.left;
                var cur_top = node.style.top; 
                var l = parseInt(cur_left.substring(0, cur_left.length));
                var t = parseInt(cur_top.substring(0, cur_top.length));

                node.style.left = parseInt(l +  dx) + "px";
                node.style.top = parseInt(t + dy) + "px"; 
                
        }

}


function pan(ix, iy){
	var dx = (maxx - minx)/mwidth;
	var mx = dx*ix;
	var my = dx*iy;
	minx += mx;
	maxx += mx;
	miny += my;
	maxy += my;
	var env = '<ENVELOPE minx="' + minx + '" miny="' + miny +'" maxx="' + maxx +'" maxy="' + maxy + '" />';
     	getMap(env);
}
function panDirection(dir){
	shift(dir);
	var env = '<ENVELOPE minx="' + minx + '" miny="' + miny +'" maxx="' + maxx +'" maxy="' + maxy + '" />';
     	getMap(env);
}

function shift(dir){
	var dx = maxx - minx;
	var dy = maxy - miny;
		
	switch(dir) {
		//NORTH
		case "north":
			miny += 0.3*dy;
			maxy += 0.3*dy;
		break;
		//south
		case "south":
			miny -= 0.3*dy;
			maxy -= 0.3*dy;
		break;
		//east
		case "east":
			minx += 0.3*dx;
			maxx += 0.3*dx;
		break;
		//west
		case "west":
			minx -= 0.3*dx;
			maxx -= 0.3*dx;
		break;
		//northeast
		case "ne":
			miny += 0.3*dy;
			maxy += 0.3*dy;
			minx += 0.3*dx;
			maxx += 0.3*dx;
		break;
		//northwest
		case "nw":
			miny += 0.3*dy;
			maxy += 0.3*dy;
			minx -= 0.3*dx;
			maxx -= 0.3*dx;
		break;
		//southeast
		case "se":
			miny -= 0.3*dy;
			maxy -= 0.3*dy;
			minx += 0.3*dx;
			maxx += 0.3*dx;
		break;
		//southwest
		case "sw":
			miny -= 0.3*dy;
			maxy -= 0.3*dy;
			minx -= 0.3*dx;
			maxx -= 0.3*dx;
		break
		
	}
}

function getExtentForZoomIn(left, bottom, right, top){

	var LLPoint = getMapXY(left, bottom);
	var URPoint = getMapXY(right, top);
 
	var xdiff = URPoint[0] - LLPoint[0];
        var ydiff = URPoint[1] - LLPoint[1];

        if (xdiff < mindiff || ydiff < mindiff) {
		alert ("Please select a larger area to zoom.");
        	return false;
	}
	
	minx = LLPoint[0];
	miny = LLPoint[1];
	maxx = URPoint[0];
	maxy = URPoint[1];

	return true;

}

function getExtentForZoomOut(left, bottom, right, top){
		var xDiff= maxx-minx;
		var yDiff= maxy-miny;

		var pwidth = right-left;
		var pheight = top-bottom;
		var xRatio = mwidth / pwidth;
		var yRatio = mheight / pheight;
		var xAdd = xRatio * xDiff / 2;
		var yAdd = yRatio * yDiff / 2;
		minx =  minx - xAdd;
		maxx = maxx + xAdd;
		miny = miny - yAdd;
		maxy = maxy + yAdd;
		

}

function dump_vals()
{	
	alert ('minx / ' + minx + ' miny / ' + miny + ' maxx / ' + maxx + ' maxy / ' + maxy);
}

function getMapXY(xIn,yIn) {

	var newValues = new Array();

	var mouseX = xIn;
	var pixelX = (maxx-minx) / mwidth;

	var newX = (pixelX * mouseX) + minx;

	var mouseY = mheight - yIn;
	var pixelY = (maxy-miny) / mheight;
	var newY = (pixelY * mouseY) + miny;

	newValues[0] = newX;

	newValues[1] = newY;
	return newValues; 
}

function growEnvelope(value){
	var dx = maxx - minx;
	var dy = maxy - miny;

	var cx = (maxx + minx)/2.0;
	var cy = (maxy + miny)/2.0;

	var dx1 = 0.5 * value * dx;
	var dy1 = 0.5 * value * dy;

	minx = cx - dx1;
	miny = cy - dy1;
	maxx = cx + dx1;
	maxy = cy + dy1;
}

function getScaledMap(val, img, sel) {

for (i = 0; i < 10; i++) {
	var elm = getElmById('zoom' + i);
	elm.src='images/zoom/ZoomNav_Level' + (i+1) + '.gif';
}

var elm = getElmById(sel);

elm.src = img;


}

function getMapWithSort(sort) {
if (sort == order_by) {
	if (up_down == "ASC") {
		up_down = "DESC";
	} else {
		up_down = "ASC";
	}
}
order_by = sort;
getMap("");
}

function getMapWithCurrentExtent(){

	getMap("");
}



function getMap(envelope) {

	if (okToSend) {
  	http = getHTTPObject();

  	if ((http != null) ) {
	okToSend = false;
    	var axl = getMapRequest(envelope);
   	 
    	http.open("POST", url, true);
    	http.onreadystatechange = printResponse;
    	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    	showLayer('loading');
    	http.send(axl);
	}
	} else {
		alert ("Waiting for previous response");
	}
}

function createQueryString()
{
	var retVal = "minx=" + minx + "&miny=" + miny + "&maxx=" + maxx + "&maxy=" + maxy;

	var myvars = new Array('lp_low', 'lp_high', 'beds_low', 'beds_high', 'p_type', 'wf');

	for (var i = 0; i < myvars.length; i++) {
		retVal = retVal + "&" + myvars[i] + "=" + getElmById(myvars[i]).value;
	}

        retVal += "&zoom=true&scale=" + scale;

	
	return escape(retVal);
}

function buildPoiWhere() {
        var w = "";
        var cats = "";

        var inputs = document.getElementsByTagName('input');

        for(var i=0; i<inputs.length; i++){
                var type = inputs[i].type;
                var id = inputs[i].id;
                var name = inputs[i].name;
                if (type == 'checkbox' && id.indexOf('POI_') == 0) {
                        if (inputs[i].checked) {
                                if (cats.length == 0) {
                                        cats += "'" + name + "'";
                                } else {
                                        cats += ",'" + name + "'";
                                }
                        }
                }
        }

        if (cats.length > 0) {
                w = " AND tp_category IN (" + cats + ")";
        }

        return w;
}

function getMapRequest(env)
{
	var w_poi = buildPoiWhere();

	var lp_low = document.getElementById('lp_low').value;
	var lp_high = document.getElementById('lp_high').value;
	var beds_low = document.getElementById('beds_low').value;
	var beds_high = document.getElementById('beds_high').value;
	var p_type = document.getElementById('p_type').value;
	var wf = document.getElementById('wf').value;

	var w = ' AND lp_Listing_Price >= ' + lp_low + ' AND lp_Listing_Price <= ' + lp_high + ' AND lp_No_Bedrooms >= ' + beds_low + '  AND lp_No_Bedrooms <= ' + beds_high;

	if (p_type.length > 0) {
		w += " AND lp_tp_Improvement = '" + escape(p_type) + "'";
	}
	if (wf.length > 0) {
		w += " AND lp_fg_WaterAccess = '" + wf + "'";
	}

	w += " ORDER BY " + order_by + " " + up_down;

	var fl = "cd_MLS,lp_MLS_Number,FORMAT(lp_Listing_Price, 0) as lp_Listing_Price_Display,FLOOR(lp_Listing_Price) AS lp_Listing_Price,lp_ListingCity,lp_ListingAddr1,lp_No_Bedrooms,lp_No_Bathrooms,lp_res_Lat,lp_res_Long,lp_Photo_URL,lp_PhotoDesc_1,lp_ind_OpenHouse,lp_ds_OpenHouseTime,cd_Company,lp_fg_ShowOnWeb";
	
        var axl = 'SITE=' + site + '&INC_OFFICES=' + (document.getElementById('INC_OFFICES').checked ? "true" : "") + '&minx=' + minx + '&maxy=' + maxy + '&maxx=' + maxx + '&miny=' + miny + '&cache=' + cache + '&width=' + width + '&height=' + height + '&INC_LISTINGS=y&DS=' + ds + '&MAX_RESULTS=' + max_results + '&FL=' + fl + "&scale=" + scale + '&zoom=' + fixedzoom + "&CACHE_ORIGIN_X=" + cache_origin_x + "&CACHE_ORIGIN_Y=" + cache_origin_y + "&SRCH_WHERE=" + w + "&INC_POI=" + (w_poi.length > 0 ? "true" : "") + "&POI_WHERE=" + w_poi;

	return axl;
}


//need to define loadXML function for non IE browser which do not have loadXML implemented
//we use loadXML (can't use load method) to load ArcXML response because the response from ArcIMS is in plain text format   

if(!isIE && !isSafari){

	Document.prototype.loadXML = function (s) {

	// parse the string to a new doc   
	   	var doc2 = (new DOMParser()).parseFromString(s, "text/xml");
      
	   	// remove all initial children
	   	while (this.hasChildNodes())
	   	   this.removeChild(this.lastChild);
         
	   	// insert and import nodes
	   	for (var i = 0; i < doc2.childNodes.length; i++) {
	   	   this.appendChild(this.importNode(doc2.childNodes[i], true));
	   	}
       };
}



function printResponse(){

if (http.readyState == 4) {
    if (http.status == 200) {

      isWorking = false;
      
	if (!isSafari) 
	{
      	var result = http.responseText;

	var xmlDoc;
	
      	if(document.implementation && document.implementation.createDocument) { 
      		// MOZILLA 
       	 	xmlDoc = document.implementation.createDocument("", "", null);
      	 	xmlDoc.async="false";
         	xmlDoc.loadXML(result);
      	} else if (window.ActiveXObject){
      	 	//IE
      	  	xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	  	xmlDoc.async="false";
	  	xmlDoc.loadXML(result);
       	}
	} else {
		// Safari
		var xmlDoc = http.responseXML;
	}
	
      var tiles = xmlDoc.getElementsByTagName("TILES")[0].getElementsByTagName("TILE");
        scale = xmlDoc.getElementsByTagName("TILES").item(0).getAttribute("scale");

        resolution = xmlDoc.getElementsByTagName("TILES").item(0).getAttribute("resolution");

        var env = xmlDoc.getElementsByTagName("ENVELOPE").item(0);

        minx = parseFloat(env.getAttribute("minx"));
        miny = parseFloat(env.getAttribute("miny"));
        maxx = parseFloat(env.getAttribute("maxx"));
        maxy = parseFloat(env.getAttribute("maxy"));

        clearImageMap();

        placeTiles(tiles);

        fixedzoom = "";

        document.getElementById('propcount').innerHTML = '<span class="red_txt">Property Count: ' + xmlDoc.getElementsByTagName("PROPERTIES").item(0).getAttribute("count") + "</span>";

        var props = xmlDoc.getElementsByTagName("RESPONSE")[0].getElementsByTagName("PROPERTY");
        var offices = xmlDoc.getElementsByTagName("RESPONSE")[0].getElementsByTagName("OFFICE");

	var pois = xmlDoc.getElementsByTagName("RESPONSE")[0].getElementsByTagName("POI");


        processPropertyRecs(props, xmlDoc.getElementsByTagName("PROPERTIES").item(0).getAttribute("count"));

        processOfficeRecs(offices);

        if (props.length > 0) {
                document.getElementById('poi_div').style.visibility = 'visible';
                processPoiRecs(pois);
        } else {
                document.getElementById('poi_div').style.visibility = 'hidden';
                document.getElementById('POI_SCHOOL').checked = false;
        }


	hideLayer('loading');
    }else alert("Error retreiving data");
 
   okToSend = true;
  } 
}

function jumpTo(i) {
        if (okToSend) {
                scale = i;
                fixedzoom = "true";
                getMap("");
        }
}

function clearImageMap ()
{

        if (isIE) {
                while (mapContainer.childNodes.length > 1) {
                        mapContainer.removeChild(mapContainer.lastChild);
                }
        } else {
                while (mapContainer.childNodes.length > 3) {
                        mapContainer.removeChild(mapContainer.lastChild);
                }
        }

        while (container.childNodes.length > 0) {
                container.removeChild(container.lastChild);
        }
}


function outputComma(number) {
    number = '' + number
    if (number.length > 3) {
        var mod = number.length%3;
        var output = (mod > 0 ? (number.substring(0,mod)) : '');
        for (i=0 ; i < Math.floor(number.length/3) ; i++) {
            if ((mod ==0) && (i ==0))
                output+= number.substring(mod+3*i,mod+3*i+3);
            else
                output+= ',' + number.substring(mod+3*i,mod+3*i+3);
        }
        return (output);
    }
    else return number;
}




function cleanNum(num)
{
num = unescape(num);
return num.replace(',', '');
}

function cleanString(str) 
{
	if (str == null) {
		return '';
	}

	return unescape(str.replace(new RegExp(/\+/g), ' '));
}

function getElmById(theID) {
	var rv = null;

	if (isIE) {
		rv = document.all[theID];
	} else {
		rv = document.getElementById(theID);
	}
	return rv;
}

function placeTiles(tiles) {
        
        for (var i = 0; i < tiles.length; i++) {
                var url = tiles[i].getAttribute("url");
                        
                        var x = parseFloat(tiles[i].getAttribute("x"));
                        var y = parseFloat(tiles[i].getAttribute("y"));
                        var row = parseInt(tiles[i].getAttribute("row"));
                        var col = parseInt(tiles[i].getAttribute("col"));
                        var x_pos = parseInt((x - minx)/resolution);
                        var y_pos = parseInt((maxy - y)/resolution);
                        
                        var img = document.createElement('img');
                        
                        img.src = url;
                        img.style.position = 'absolute';
                        img.style.top = y_pos + 'px';
                        img.style.left = x_pos + 'px';
                        img.onload = null;
                        
                        container.appendChild(img);
        }

}

function processPoiRecs(props)
{

        var icon_w = 206;
        var icon_h = 237;

        for (var i = 0; i < props.length; i++) {
                var x = parseInt(props[i].getAttribute('x'));
                var y = parseInt(props[i].getAttribute('y'));

                var x1 = x;
                var y1 = y;

                var icon_type = "";
                if (x > (width / 2)) {
                        if (y > (height / 2)) {
                                icon_type = "BR";
                                x = x - icon_w;
                                y = y - icon_h;                        
			} else {
                                icon_type = "TR";
                                x = x - icon_w + 6;
                                y = y + 4;
                        }                
		} else {
                        if (y > (height / 2)) {
                                icon_type = "BL";
                                y = y - icon_h;
                                x = x + 5;
                        } else {
                                icon_type = "TL";
                                x = x + 5;
                                y = y + 3;
                        }
                }


                var cd_key = cleanString(props[i].getAttribute('cd_key'));

                var myDiv = document.createElement('DIV');
                myDiv.id = "POI_" + cd_key;
                myDiv.style.position='absolute';
                myDiv.style.visibility='hidden';                
		myDiv.style.left =x + 'px';
                myDiv.style.top = y + 'px';
                myDiv.style.zIndex=2;

                var tmp_balloon = '<div class="balloon_' + icon_type + '">';
                tmp_balloon += '<div class="inner_balloon_poi">';

                tmp_balloon += '<div class="close_icon"><a href="#" onClick="hideAll();"><img border="0" src="images/close_icon.gif" style="width:17px;"></a></div><br>';
                if (cleanString(props[i].getAttribute('ds_image')).indexOf('<img') > -1) {
                        tmp_balloon += cleanString(props[i].getAttribute('ds_image'));
                } else if (cleanString(props[i].getAttribute('ds_image')).length > 0) {
                        tmp_balloon += '<img src="http://mapping.redata.com/plots/poi/' + site + '/poi/'  + cleanString(props[i].getAttribute('ds_image')) + '" with="150" height="117"><br>';
                }

                if (cleanString(props[i].getAttribute('ds_display')).length > 0) {
                        tmp_balloon += '<table><tr><td align="left">' + cleanString(props[i].getAttribute('ds_display')) + '</td></tr></table>';
                } else {
                        tmp_balloon += cleanString(props[i].getAttribute('ds_text1')) + '<br>';                        
			tmp_balloon += cleanString(props[i].getAttribute('ds_text2')) + '<br>';
                        tmp_balloon += cleanString(props[i].getAttribute('ds_text3')) + '<br>';
                }

                tmp_balloon += '</div>';

                tmp_balloon += '</div>';

                myDiv.innerHTML = tmp_balloon;

                mapContainer.appendChild(myDiv);

                var area = document.createElement('DIV');
                area.id = cd_key;

		area.innerHTML = '<img src="http://mapping.redata.com/plots/poi/' + site + '/icon/' + cleanString(props[i].getAttribute('ds_icon')) + '" border="0">';

                area.style.left=x1 + 'px';
                area.style.top=y1 + 'px';
                area.style.position='absolute';
                area.style.zIndex=1;
                area.onclick = function() {
                        hideAll();
                        document.getElementById('POI_' + this.id).style.visibility = 'visible';

                }

                mapContainer.appendChild(area);

        }
}


function processOfficeRecs(offices)
{


	var icon_w = 206;
	var icon_h = 237;

        var queryString = createQueryString();

	for (var i = 0; i < offices.length; i++) 
	{
		var x = parseInt(offices[i].getAttribute('x'));
		var y = parseInt(offices[i].getAttribute('y'));
  	
		var x1 = x;
                var y1 = y;

		var icon_type = "";

                if (x > (width / 2)) {
                	if (y > (height / 2)) {
				icon_type = "BR";
				x = x - icon_w;
				y = y - icon_h; 
			} else {
				icon_type = "TR";
				x = x - icon_w + 6;
				y = y + 4;
			}				
		} else {
                	if (y > (height / 2)) {
				icon_type = "BL";
				y = y - icon_h;
				x = x + 5;
			} else {
				icon_type = "TL";
				x = x + 5;
				y = y + 3;
			}
                }

                var myDiv = document.createElement('DIV');
                myDiv.id = "OFFICE_" + i;
                myDiv.style.position='absolute';
                myDiv.style.visibility='hidden';
                myDiv.style.left =x + 'px';
                myDiv.style.top = y + 'px';
                myDiv.style.width = '160px';
                myDiv.style.height= '220px';
                myDiv.style.zIndex=2;

                var photo_string = "";

                if (offices[i].getAttribute('O_DFLT_PHOTO_URL').length > 0) {
                        photo_string = '<a href="http://www.bunburyrealtors.com/vp/AgentServlet?cd_Company=BUNBURY&cd_Office=' + cleanString(offices[i].getAttribute('O_CD_OFFICE')) + '&fg_Active=Y&SITE=BUNBURY&Search=True&ScreenID=AgentsSummary_Public&ScreenID_Cur=AGENT_LINK" target="_parent"><img src="' + cleanString(offices[i].getAttribute('O_DFLT_PHOTO_URL')) + '" width="150" height="117" border="0"></a>';

                } 

		var balloon = '<div class="balloon_' + icon_type + '">';
		balloon += '<div class="inner_balloon">';
		balloon += '<div class="close_icon"><a href="#" onClick="hideAll();"><img border="0" src="images/close_icon.gif" style="width:17px;"></a></div>';
		balloon += photo_string;
		balloon += '<div class="address">' + cleanString(offices[i].getAttribute('O_NM_OFFICE')) + '</div>';
		balloon += cleanString(offices[i].getAttribute('O_ADDRESS_01')) + '<br />';
		balloon += '<a href="http://www.bunburyrealtors.com/vp/AgentServlet?cd_Company=BUNBURY&cd_Office=' + cleanString(offices[i].getAttribute('O_CD_OFFICE')) + '&fg_Active=Y&SITE=BUNBURY&Search=True&ScreenID=AgentsSummary_Public&ScreenID_Cur=AGENT_LINK" target="_parent">More Details</a>';
		balloon += '</div>';
		balloon += '</div>';
		balloon += '</div>';

			myDiv.innerHTML = balloon;

                	mapContainer.appendChild(myDiv);
                        
	                var area = document.createElement('DIV');
        	        area.id = i;
                	area.innerHTML = '<img src="images/office.gif" width="16" height="16" border="0">';
	                area.style.left=x1 + 'px';
        	        area.style.top=y1 + 'px';
                	area.style.position='absolute';
	                area.style.zIndex=1;
        	        area.onmouseover = function() {
				hideAll();
				document.getElementById('OFFICE_' + this.id).style.visibility = 'visible';
                	}
                
                mapContainer.appendChild(area); 
		
                
        }       
}
	
function processPropertyRecs(props, numProps)
{

	var props_list = "";

	var queryString = createQueryString();
	
	var icon_w = 206;
	var icon_h = 237;

	for (var i = 0; i < props.length; i++) {
		var x = parseInt(props[i].getAttribute('x'));
		var y = parseInt(props[i].getAttribute('y'));
	
		if (props_list.length == 0) {
			props_list = props[i].getAttribute("cd_MLS");
		} else {
			props_list = props_list +  "," + props[i].getAttribute("cd_MLS");
		}
	
		var x1 = x - 8;
		var y1 = y - 8;

                var icon_type = "";

                if (x > (width / 2)) {
                        if (y > (height / 2)) {
                                icon_type = "BR";
                                x = x - icon_w;
                                y = y - icon_h;
                        } else {
                                icon_type = "TR";
                                x = x - icon_w + 6;
                                y = y + 4;
                        }
                } else {
                        if (y > (height / 2)) {
                                icon_type = "BL";
                                y = y - icon_h;
                                x = x + 5;
                        } else {
                                icon_type = "TL";
                                x = x + 5;
                                y = y + 3;
                        }
                }

		var myDiv = document.createElement('DIV');
		myDiv.id = "LISTING_" + i;
		myDiv.style.position='absolute';
		myDiv.style.visibility='hidden';
		if (s_layer == props[i].getAttribute('cd_MLS')) {
			myDiv.style.visibility = 'visible';
			s_layer = '';
		}

		myDiv.style.left =x + 'px';
		myDiv.style.top = y + 'px';
		myDiv.style.width = '160px';
		myDiv.style.height= '220px';
		myDiv.style.zIndex=2;

		var photo_string = "";

		if (props[i].getAttribute('lp_Photo_URL').length > 0) {
    			photo_string = '<a href="' + root_url + '/vp/ListingServlet?cd_MLS=' + props[i].getAttribute('cd_MLS') + '&SITE=' + site + '&ScreenID=LISTING_DETAIL_P&mapvars=' + queryString + '%26s_layer%3D' + props[i].getAttribute('cd_MLS') +'" target="_parent"><img src="http://' + cleanString(props[i].getAttribute('lp_Photo_URL')) + props[i].getAttribute('lp_MLS_Number') + 'at.jpg" width="150" height="117" border="0"></a>';
		}	

                var balloon = '<div class="balloon_' + icon_type + '">';
                balloon += '<div class="inner_balloon">';
		if (props[i].getAttribute('lp_ind_OpenHouse') == 'Y') {
			balloon += '<div style="font-size:9px;font-face:Arial"><a href="' + root_url + '/vp/ListingServlet?cd_MLS=' + props[i].getAttribute('cd_MLS') + '&SITE=' + site + '&ScreenID=LISTING_DETAIL_P&mapvars=' + queryString + '%26s_layer%3D' + props[i].getAttribute('cd_MLS')  + '" target="_parent">Open:' + cleanString(props[i].getAttribute('lp_ds_OpenHouseDay')) + ' ' + cleanString(props[i].getAttribute('lp_ds_OpenHouseTime')) + '</a></div>';
		}	

        	balloon += '<div class="close_icon"><a href="#" onClick="hideAll();"><img border="0" src="images/close_icon.gif" style="width:17px;"></a></div>';
 
		balloon += photo_string;
		if (props[i].getAttribute('lp_fg_ShowOnWeb') != 'A') {
                	balloon += '<div class="address">' + cleanString(props[i].getAttribute('lp_ListingAddr1')) + '</div>';
		}
                balloon += '$' + cleanString(props[i].getAttribute('lp_Listing_Price_Display')) + '<br />';
		balloon += props[i].getAttribute('lp_No_Bedrooms') + ' Beds ' + props[i].getAttribute('lp_No_Bathrooms') + ' Baths<br>';
		balloon += '<a href="' + root_url + '/vp/ListingServlet?cd_MLS=' + props[i].getAttribute('cd_MLS') + '&SITE=' + site + '&ScreenID=LISTING_DETAIL_P&mapvars=' + queryString + '%26s_layer%3D' + props[i].getAttribute('cd_MLS') + '" target="_parent">More Details</a>';

                balloon += '</div>';
                balloon += '</div>';
                balloon += '</div>';

                myDiv.innerHTML = balloon;

		mapContainer.appendChild(myDiv);
	
		var area = document.createElement('DIV');
		area.id = i;
	
		area.innerHTML = '<img src="images/ResidentialListing.png" border="0">';

		area.style.left=x1 + 'px';
		area.style.top=y1 + 'px';
		area.style.position='absolute';
		area.style.zIndex=1;
		area.onclick = function() {
			hideAll();
			document.getElementById('LISTING_' + this.id).style.visibility = 'visible'; 
		}

		mapContainer.appendChild(area);
	
	}

	var pca = document.getElementById('propcount_alert');

	if (numProps > parseInt(max_results)) {
		pca.innerHTML = 'Too many to display: Refine your search to fewer than 100 by zooming in closer, or by adjusting the menus at left.';
	} else if (numProps == 0) {
		pca.innerHTML = 'No properties found: Refine your search to more than 0 by zooming out further, or by adjusting the menus above.';
	} else {
		pca.innerHTML = 'Click on a house icon in order to see more information. Click more details to see a detail page.';
	}
			
}

function hideAll()
{
        var els = document.getElementsByTagName('DIV');

        for (var j = 0; j < els.length; j++) {
                var elm = els[j];
                if (elm != null) {
                        if (elm.id.indexOf('LISTING_') > -1 || elm.id.indexOf('OFFICE_') > -1 || elm.id.indexOf('POI_') > -1)
                                elm.style.visibility = 'hidden';
                } else {
                        break;
                }
        }
}

