var _progress;
var _infoWindow;
var _markerType = 0;

function GoogleLoad()
{
   ShowSearch(document.getElementById("imgPin"));
   ShowSearch(document.getElementById("imgPin"));
   
   CenterBar();

   if (GBrowserIsCompatible()) 
   {
      map = new GMap2(document.getElementById("map_canvas"));
      map.setCenter(new GLatLng(_lat, _long), 14);

      map.addControl(new GLargeMapControl());
      //map.addControl(new GSmallMapControl());
      map.addControl(new GMapTypeControl());
      map.addControl(new GOverviewMapControl());

      // Create a base icon for all of our markers that specifies the
      // shadow, icon dimensions, etc.
      baseIcon = new GIcon();
      baseIcon.shadow = "http://prooftopress.com/Icons/realestate_shadow.png";
      baseIcon.iconSize = new GSize(20, 34);
      baseIcon.shadowSize = new GSize(37, 34);
      baseIcon.iconAnchor = new GPoint(9, 34);
      baseIcon.infoWindowAnchor = new GPoint(9, 2);
      baseIcon.infoShadowAnchor = new GPoint(18, 25);

      //GEvent.addListener(this.map, "click", function() {
         //MapClick();
      //});

      AddResize();

      GEvent.bind(this.map, "infowindowclose", this, function(){ 
         //alert("InfoWindowClose Click");      
         AddResize();
      });         


      //Show Balloon on Map
      if(parseInt(_markerId)>0)
      {
         MappingResize();
         MarkerShow(_markerId);
      }

      GetResults();
      
   }
}

// Creates a marker whose info window displays the letter corresponding
// to the given index.
function createMarker(point, info, markerType) {
 // Create a lettered icon for this point using our icon class
// var letter = String.fromCharCode("A".charCodeAt(0) + index);
 var letteredIcon = new GIcon(baseIcon);
 //letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";
 
   switch(markerType)
   {
      case 1:
         letteredIcon.image = "http://prooftopress.com/Icons/realestatered.png";
      break;
      case 2:
         letteredIcon.image = "http://prooftopress.com/Icons/realestateblue.png";
      break;
      case 3:
         letteredIcon.image = "http://prooftopress.com/Icons/realestategreen.png";
      break;
      case 4:
         letteredIcon.image = "http://prooftopress.com/Icons/realestateorange.png";
      break;
      default:
         letteredIcon.image = "http://prooftopress.com/Icons/realestate.png";
      break;                        
   }

 //letteredIcon.image = "http://prooftopress.com/Icons/realestate.png";

 // Set up our GMarkerOptions object
 markerOptions = { icon:letteredIcon };
 var marker = new GMarker(point, markerOptions);

 GEvent.addListener(marker, "click", function() {
   //alert("Listener Click");
   RemoveSize();
   marker.openInfoWindowHtml(info);
   //AddResize();
 });

 return marker;
}

function GetResults()
{
   //Lat, Long

   var bounds = map.getBounds();
   var ne = bounds.getNorthEast();
   var sw = bounds.getSouthWest();
   var southWest = sw.lat() + "," + sw.lng();
   var northEast = ne.lat() + "," + ne.lng();
   var areaId = -1;

   //alert("Search Values");
   //Get Search Values

   var propTypes = GetPropertyTypes();
   //areaId = document.getElementById("lstArea")[document.getElementById("lstArea").selectedIndex].value;
   var cityId = document.getElementById("lstCity")[document.getElementById("lstCity").selectedIndex].value;
   var countyId = document.getElementById("lstCounty")[document.getElementById("lstCounty").selectedIndex].value;

   if(areaId>0 || cityId>0 || countyId>0)
   {
      southWest = "";
      northEast = "";
   }

   var bedId = document.getElementById("lstBedrooms")[document.getElementById("lstBedrooms").selectedIndex].value;
   var bathId = document.getElementById("lstBath")[document.getElementById("lstBath").selectedIndex].value;
   var priceLow = document.getElementById("lstLow")[document.getElementById("lstLow").selectedIndex].value;
   var priceHigh = document.getElementById("lstHigh")[document.getElementById("lstHigh").selectedIndex].value;
   var mlsId = document.getElementById("txtMLS").value;
   //End Get Search Values

   //alert("GetResults");
   Snape.Search.MapSearch.GetResults(southWest, northEast, propTypes, areaId, cityId, countyId, bedId, bathId, priceLow, priceHigh, mlsId, BuildMap);
}

function BuildMap(alt)
{
   //alert("BuildMap");
   var bounds = map.getBounds();
   var ne = bounds.getNorthEast();
   var sw = bounds.getSouthWest();
   var markerType = 0;

   document.getElementById("txtNE").value = ne.lat() + "," + ne.lng();
   document.getElementById("txtSW").value = sw.lat() + "," + sw.lng();

   //alert(alt.value.length);
   if(alt!=null)
   {
      var obj = alt.value;
      for(var i=0;i<obj.length;i++)
      {
         var latlng = new GLatLng(obj[i].Latitude.toString(), obj[i].Longitude.toString());
         map.addOverlay(createMarker(latlng, obj[i].Info, obj[i].MarkerType));
       }
   }
   else
      alert("Execute Order 66.");
      
   document.getElementById("divProgress").style.visibility = "hidden";
   GrayOut(false);

}

function MappingResize()
{
   //Lat, Long
   var bounds = map.getBounds();
   var ne = bounds.getNorthEast();
   var sw = bounds.getSouthWest();
   var southWest = sw.lat() + "," + sw.lng();
   var northEast = ne.lat() + "," + ne.lng();
   var areaId = -1;

   //Make sure search is hidden
   document.getElementById("divLst").style.visibility = "hidden";
   var pin = document.getElementById("imgPin");
   var src = pin.src;
   src = src.replace("pin4", "pin3");
   pin.src = src;
   //End Make sure search is hidden

   //Get Search Values
   var propTypes = GetPropertyTypes();
   //var areaId = document.getElementById("lstArea")[document.getElementById("lstArea").selectedIndex].value;
   var cityId = document.getElementById("lstCity")[document.getElementById("lstCity").selectedIndex].value;
   var countyId = document.getElementById("lstCounty")[document.getElementById("lstCounty").selectedIndex].value;
   
   if(areaId>0 || cityId>0 || countyId>0)
   {
      southWest = "";
      northEast = "";
   }
   
   var bedId = document.getElementById("lstBedrooms")[document.getElementById("lstBedrooms").selectedIndex].value;
   var bathId = document.getElementById("lstBath")[document.getElementById("lstBath").selectedIndex].value;
   var priceLow = document.getElementById("lstLow")[document.getElementById("lstLow").selectedIndex].value;
   var priceHigh = document.getElementById("lstHigh")[document.getElementById("lstHigh").selectedIndex].value;
   var mlsId = document.getElementById("txtMLS").value;
   //End Get Search Values

   var info = Snape.Search.MapSearch.BuildGrid(southWest, northEast, propTypes, areaId, cityId, countyId, bedId, bathId, priceLow, priceHigh, mlsId).value;
   document.getElementById("lblCount").innerHTML = "Count: " + info[0];
   document.getElementById("lblGrid").innerHTML = info[1];
   //document.getElementById("lblGrid").innerHTML = Snape.Search.MapSearch.BuildGrid(southWest, northEast, propTypes, areaId, cityId, countyId, bedId, bathId, priceLow, priceHigh, mlsId).value;
   map.clearOverlays();
   
   GetResults();

   clearTimeout(_progress);    
}

function AddResize()
{
   if(myEventListener==null)
   {
      //alert("AddResize");
      myEventListener = GEvent.bind(this.map, "moveend", this, function(){ 
         Processing();
       });
   }

}

function RemoveSize()
{
   if(myEventListener!=null)
   {
      //alert("RemoveSize()");
      GEvent.removeListener(myEventListener);
      myEventListener = null;
   }
}

function Testing()
{
   alert("Testing");
}

function Processing()
{
   //document.getElementById("lstArea").selectedIndex = 0;
   //document.getElementById("lstCity").selectedIndex = 0;
   //document.getElementById("lstCounty").selectedIndex = 0;


   //If Info window open do not move map
   if(map.getInfoWindow().isHidden())
   {
      //alert("Processing");
      CenterBar();
      _progress = window.setTimeout("MappingResize()", 25);
   }
   else
   {
   
      //alert("Skip Processing");
   }
}

function ForceProcessing()
{
   CenterBar();
   _progress = window.setTimeout("MappingResize()", 25);   
}

function CenterBar()
{
   document.getElementById("divProgress").style.visibility = "visible";
   displayFloatingDiv("divProgress", "map_canvas", 50, 50);
   
   GrayOut(true);
}

function displayFloatingDiv(divId, center_over_Id, width, height)
{
   var left, top;
   var parent = document.getElementById(center_over_Id);
   var mapObj = new Pos(null);
   //document.getElementById("divProgress").style.visible = "visible";
   mapObj.SetObject(parent);
   
   //left = Math.max(0, (mapObj.ObjX() + parent.offsetWidth)/2) + width;
   //top = Math.max(0, (mapObj.ObjY() + parent.offsetHeight)/2) - height;

   //left = ((mapObj.ObjX() + parent.offsetWidth)/2) + width;
   //top =  ((mapObj.ObjY() + parent.offsetHeight)/2) - height;
   
   left = ((mapObj.X() + parent.offsetWidth)/2) + width;
   top =  ((mapObj.Y() + parent.offsetHeight)/2) + height;


   document.getElementById("divProgress").style.left=left + "px";
   document.getElementById("divProgress").style.top=top + "px";
   
}

function MarkerClick(id)
{
   var mr = Snape.Search.MapSearch.GetResult(id).value;
   var point = new GLatLng(mr.Latitude.toString(), mr.Longitude.toString());
   var letteredIcon = new GIcon(baseIcon);
   
   switch(mr.MarkerType)
   {
      case 1:
         letteredIcon.image = "http://prooftopress.com/Icons/realestatered.png";
      break;
      case 2:
         letteredIcon.image = "http://prooftopress.com/Icons/realestateblue.png";
      break;
      case 3:
         letteredIcon.image = "http://prooftopress.com/Icons/realestategreen.png";
      break;
      case 4:
         letteredIcon.image = "http://prooftopress.com/Icons/realestateorange.png";
      break;
      default:
         letteredIcon.image = "http://prooftopress.com/Icons/realestate.png";
      break;                        
   }
   
   markerOptions = { icon:letteredIcon };
   
   var marker = new GMarker(point, markerOptions);
   map.addOverlay(marker);
   marker.openInfoWindowHtml(mr.Info);
 
  GEvent.addListener(marker, "click", function() {
   //alert("Listener Click");
   RemoveSize();
   marker.openInfoWindowHtml(mr.Info);
   //AddResize();
 });
  
   //Marker Create
   //createMarker(point, mr.Info, mr.MarkerType);
}

function MarkerShow(id)
{
   var mr = Snape.Search.MapSearch.GetResult(id).value;
   var point = new GLatLng(mr.Latitude.toString(), mr.Longitude.toString());
   var letteredIcon = new GIcon(baseIcon);
  
   switch(mr.MarkerType)
   {
      case 1:
         letteredIcon.image = "http://prooftopress.com/Icons/realestatered.png";
      break;
      case 2:
         letteredIcon.image = "http://prooftopress.com/Icons/realestateblue.png";
      break;
      case 3:
         letteredIcon.image = "http://prooftopress.com/Icons/realestategreen.png";
      break;
      case 4:
         letteredIcon.image = "http://prooftopress.com/Icons/realestateorange.png";
      break;
      default:
         letteredIcon.image = "http://prooftopress.com/Icons/realestate.png";
      break;                        
   }
   
   markerOptions = { icon:letteredIcon };

   var marker = new GMarker(point, markerOptions);
   map.addOverlay(marker);
   marker.openInfoWindowHtml(mr.Info); 

}

function GetPropertyTypes()
{
   //var document.getElementById("chkRes").value;
   //alert(document.getElementById("chkRes").value);
   var a = new Array();
   var actr = 0;
   
   for(var i=1;i<25;i++)
   {
      if(document.getElementById("chk" + i)==null)
         continue;
         
      if(document.getElementById("chk" + i).checked)
      {
         a[actr] = document.getElementById("chk" + i).value;
         actr++;
      }
   }
   
   var v = a.join(',');
   //if(v.length>1)
      //v = v.substr(0, v.length-1);
   if(v=="")
      v=1;

   return v;
}

