var ds = 'ms_search_restaino';
var max_results = "100";
var open_div = "";

function getMap(envelope) {

        if (okToSend) {

                http = getHTTPObject();

                if ((http != null) ) {
                        okToSend = false;
                        var axl = "";
                        axl = getTiledMapRequest("");
                        http.open("POST", cache_url, true);
                        http.onreadystatechange = printTiledResponse;
                        http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                        showLayer('loading');
                        http.send(axl);
                }
        }
}

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 getTiledMapRequest(env) 
{

	var w_poi = buildPoiWhere();

	if (w_poi.length > 0) {                
		w_poi += " ORDER BY RAND() LIMIT 250 ";
        }

	var inc_off = '';

	if (document.getElementById('INC_OFFICES')) {
		if (document.getElementById('INC_OFFICES').checked) {
			inc_off = "true";
		}
	}

  	var axl = 'INC_OFFICES=' + inc_off  + '&INTERNAL_PLOT=true&SITE=' + site + '&minx=' + minx + '&miny=' + miny + '&maxx=' + maxx + '&maxy=' + maxy + '&width=' + mwidth + '&height=' + mheight + '&cache=' + cache + '&scale=' + scale + '&zoom=' + fixedzoom + '&CACHE_ORIGIN_X=' + cache_origin_x + '&CACHE_ORIGIN_Y=' + cache_origin_y + '&US_TILES=true' + "&INC_POI=" + (w_poi.length > 0 ? "true" : "") + "&POI_WHERE=" + w_poi + '&DS=' + ds;


        for (var i = 0; i < req_vars.length; i++) {
                axl += '&' + req_vars[i] + '=' + req_vals[i];
        }

	var ta_poi_w = buildTAPoiWhere();

        if (ta_poi_w.length > 0) {
                axl += "&INC_TA_POI=true&TA_POI_WHERE=" + ta_poi_w;
        }

        return axl;
}


function printTiledResponse() {

if (http.readyState == 4) {
    if (http.status == 200) {

      isWorking = false;

        var result = http.responseText;

        var xmlDoc = getXMLDoc();
        
        var tiles = xmlDoc.getElementsByTagName("TILES")[0].getElementsByTagName("TILE");
        resolution = xmlDoc.getElementsByTagName("TILES").item(0).getAttribute("resolution");

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

    	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"));

 	positionMileSlider();
                
        clearImageMap();

       	placeTiles(tiles);

        fixedzoom = "";
  
        var props = xmlDoc.getElementsByTagName("PROPERTY");
        var offices = xmlDoc.getElementsByTagName("OFFICE");
       	var pois = xmlDoc.getElementsByTagName("POI");
	var ta_pois = xmlDoc.getElementsByTagName("TA_POI");

	var ta_pois_count = xmlDoc.getElementsByTagName("TA_POIS");

        if (ta_pois.length > 0 || pois.length > 0) {
        	document.getElementById('ta_poi_count').innerHTML = 'Number of Points of Interest: ' + (parseInt(ta_pois.length) + parseInt(pois.length));
        } else {
                document.getElementById('ta_poi_count').innerHTML = 'Points Of Interest.';
        }

 
        processPropertyRecs(props);
        processOfficeRecs(offices);
       	processPoiRecs(pois);

	processTAPois(ta_pois);

	if (ta_pois.length > 0 || pois.length > 0) {
	        createTA_POISummary(ta_pois, pois, props);
 	} else {
		clearTA_POISummary();
	}

        hideLayer('loading');
    }
   
   	okToSend = true;
	if (schools.length > 0) {
		schools = "";
		document.getElementById('POI_SCHOOL').checked = true;
		fixedZoomOut();
	}
  
  }
 
}
    
function processPoiRecs(props)
{
        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_" + i;
                myDiv.style.position='absolute';
                myDiv.style.visibility='hidden';
                myDiv.style.left =x + 'px';
                myDiv.style.top = y + 'px';
		myDiv.style.width = icon_w + 'px';
                myDiv.style.height= icon_h + '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"><br><a href="javascript:hideAll();"><img border="0" src="images/close_icon.gif" style="width:17px;"></a></div><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>';
                }

                tmp_balloon += '</div>';

                tmp_balloon += '</div>';

                myDiv.innerHTML = tmp_balloon;


                mapContainer.appendChild(myDiv);

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

                area.innerHTML = '<img src="images/' + 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.onmouseover = function() {
                        hideAll();
                        document.getElementById('POI_' + this.id).style.visibility = 'visible';
			open_div = document.getElementById('POI_' + this.id);
                        document.onmousemove = checkPosition;
                }

                mapContainer.appendChild(area);

        }
}

function processOfficeRecs(offices)
{


        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 = icon_w + 'px';
                myDiv.style.height= icon_h + 'px';
                myDiv.style.zIndex=2;

                var photo_string = "";

 		if (offices[i].getAttribute('O_PHOTO_URL').length > 0) {   
                     photo_string = '<img src="' + cleanString(offices[i].getAttribute('O_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="document.getElementById(\'OFFICE_' + i + '\').style.visibility=\'hidden\';"><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 += cleanString(offices[i].getAttribute('O_TELEPHONE1')) + '<br />';
                balloon += '<font size="-2"><a href="mailto:' + cleanString(offices[i].getAttribute('O_EMAIL')) + '">' + cleanString(offices[i].getAttribute('O_EMAIL')) + '</a></font><br />';
                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/logo_small.gif" border="0" height="16">';
                        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';
				open_div = document.getElementById('OFFICE_' + this.id);
	                        document.onmousemove = checkPosition;
                        }

                mapContainer.appendChild(area);

        }
}


function processPropertyRecs(props)
{

        var props_list = "";

        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;
                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 = "LISTING_" + i;
                myDiv.style.position='absolute';
                myDiv.style.visibility='hidden';
                myDiv.style.left =x + 'px';
                myDiv.style.top = y + 'px';
		 myDiv.style.width = icon_w + 'px';
                myDiv.style.height= icon_h + 'px';
                myDiv.style.zIndex=2;

                var photo_string = "";

                if (props[i].getAttribute('LP_PHOTO_URL').length > 0) {
                        photo_string = '<img src="http://' + cleanString(props[i].getAttribute('LP_PHOTO_URL')) + props[i].getAttribute('L_MLS_NUMBER') + 'at.jpg" width="150" height="117" border="0">';
                }

   		var balloon = '<div class="balloon_' + icon_type + '">';
                balloon += '<div class="inner_balloon">';
                balloon += '<div class="close_icon"><a href="javascript:hideAll();"><img border="0" src="images/close_icon.gif" style="width:17px;"></a></div>';
		balloon += photo_string; 
                balloon += '<div class="address">' + cleanString(props[i].getAttribute('L_LISTINGADDR1')) + '</div>';
		var price = props[i].getAttribute('L_LISTING_PRICE');
                balloon += '$' + cleanString(formatPrice(price)) + '<br />';
                balloon += props[i].getAttribute('LS_NO_BEDROOMS') + ' Beds ' + cleanString(props[i].getAttribute('LS_NO_BATHROOMS')) + ' Baths<br>';
                balloon += '</div>';
                balloon += '</div>';
                balloon += '</div>';

                myDiv.innerHTML = balloon;

                mapContainer.appendChild(myDiv);
        
                var area = document.createElement('DIV');
 		area.id = i;

 		area.innerHTML = '<img src="http://mapping.redata.com/plots/arcims/markers/home_yellow.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('LISTING_' + this.id).style.visibility = 'visible';
			open_div = document.getElementById('LISTING_' + this.id);
                        document.onmousemove = checkPosition;

                }

                mapContainer.appendChild(area);

        }

  	if (props.length > 0) {
                buildListingRouteTable(props);
        }

}

