﻿
    var map;
    var geocoder;
    var gZoom = 13;
    var hCityStateCountry = "";
    var asLocationData;
    var jsonLocationData;
    var sTitle;
    var sZipCode;
    
    function load() {
        hCityStateCountry = document.getElementById("ctl00_ContentPlaceHolder_Body_hCityStateCountry");
    
        if (GBrowserIsCompatible()) {
            map = new GMap2(document.getElementById("map"));

            map.setCenter(new GLatLng(37.09024,-95.712891),9); 

            loadMapPoints();

            geocoder = new GClientGeocoder();
            if(hCityStateCountry.value != ""){
                geocoder.getLatLng(hCityStateCountry.value , setMapToPoint);
            }else{
                setMapToPoint(0);
            }

            map.enableScrollWheelZoom();
            map.addControl(new GSmallMapControl());
             
        }
    }
    
    function setMapToPoint(point){
        if(!point){
            map.setZoom(3);
            map.setCenter(new GLatLng(37.09024,-95.712891));
            //geocoder.getLatLng("United States", setMapToPoint);
            
        }else{
            map.setCenter(point);
        }
    }
    
    
    
var xmlhttp=false;
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
if (!xmlhttp && window.createRequest) {
	try {
		xmlhttp = window.createRequest();
	} catch (e) {
		xmlhttp=false;
	}
}

    function addMapPoint(point, sHtml){
        var marker = new GMarker(point);
        
        GEvent.addListener(marker, "click", function(){
            map.openInfoWindowHtml(point, sHtml);             
        });
        map.addOverlay(marker);    
    }

    function loadMapPoints() {
        xmlhttp.open("GET","/LoadAllListings.axd?rand=" + String(Math.floor(Math.random()*100000)),true);
        xmlhttp.onreadystatechange = function() {
            var asPoint;
            var asDataParts;
            var iId;
            
            if (xmlhttp.readyState == 4) {
                jsonLocationData = eval('(' + xmlhttp.responseText + ')');
                //asLocationData = sLocationData.split('|');

                for (i = 0; i < jsonLocationData.boats.length; i++) {
                    iId = jsonLocationData.boats[i].id;
                    sTitle = jsonLocationData.boats[i].title;
                    sZipCode = jsonLocationData.boats[i].zipcode;
                    var sPoint = jsonLocationData.boats[i].latlng
                    sPoint = sPoint.substring(1, sPoint.length - 1);
                    asPoint = sPoint.split(',');
                    var point = new GLatLng(asPoint[0], asPoint[1]);
                    addMapPoint(point, "<table><tr><td><a href='/boats/used/classifieds/" + iId + "/'><img width=\"70px\" alt=\"Go To Listing\" style=\"cursor:pointer\" src=\"/ShowBoatPhoto.axd?listingid=" + iId + "\"></a></td><td valign=\"top\"><span class=\"fineprint\" style=\"color:black\">" + sTitle + "<br>" + sZipCode + "<br><a href='/boats/used/classifieds/" + iId + "/'> Go To Listing</a></span>");
                }
                /*
                for(i = 0; i < asLocationData.length; i++){
                asDataParts = asLocationData[i].split("~");
                asDataParts[1] = asDataParts[1].substring(1, asDataParts[1].length-1);
                iId = asDataParts[0];
                asPoint = asDataParts[1].split(',');
                var point = new GLatLng(asPoint[0],asPoint[1]);
                addMapPoint(point, "<br><a href='/boats/used/classifieds/" + iId + "/'> Go To Listing</a>");
                }*/
            }
        }
        xmlhttp.setRequestHeader('Accept','message/x-jl-formresult');
        xmlhttp.send();
    }
    
    


