// GMap2 object
var map;
// GReverseGeocoder object
var rg;
// text input fields
var lat;
var lng;
// result div
var info;

var exml;

var currentZip;
var icon;
var hbsIcon;
var grayIcon;
var icons = [];

//var bump = 0.001;                   // used to 'bump' lat and lng if no zip found
//var first = false;                  // used to add debug

G_PHYSICAL_MAP.getMinimumResolution = function () { return 5 };
G_NORMAL_MAP.getMinimumResolution = function () { return 5 };
G_SATELLITE_MAP.getMinimumResolution = function () { return 5 };
G_HYBRID_MAP.getMinimumResolution = function () { return 5 };

G_PHYSICAL_MAP.getMaximumResolution = function () { return 16 };
G_NORMAL_MAP.getMaximumResolution = function () { return 16 };
G_SATELLITE_MAP.getMaximumResolution = function () { return 16 };
G_HYBRID_MAP.getMaximumResolution = function () { return 16 };

function clearMarkers() {
   for ( var i=0; i<exml.gmarkers.length; i++ ) {
      map.removeOverlay(exml.gmarkers[i]);
   }

}

/*
function setZip (zip) {
   alert('Setting zip to /admin/kml/zips/'+zip+'.xml');
   exml = new EGeoXml("exml", map, "/admin/kml/zips/"+zip+".xml", {sidebarid:"side_bar",sortbyname:true,iwwidth:300,directions:true});
   exml.parse();
}
*/
/*
function bumpLocation() {

   var tmplat = Number(lat ) + bump;
   var tmplng = Number(lng ) + bump;

   lat = tmplat;
   lng = tmplng;
   map.setCenter( new GLatLng(tmplat,tmplng,7) );

   var point = new GLatLng(tmplat, tmplng);

   rg.reverseGeocode(point);
}
*/
function load( lat , lon , zoom , features) {

   if (GBrowserIsCompatible()) {
      icon = new GIcon(G_DEFAULT_ICON,"http://hotbarspot.com/images/hbsPin.gif");
      icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
      icon.iconSize = new GSize(20, 34);
      icon.shadowSize = new GSize(37, 34);
      icon.iconAnchor = new GPoint(9, 34);
      icon.infoWindowAnchor = new GPoint(9, 2);
      icon.infoShadowAnchor = new GPoint(18, 25);
//      myIcon.transparent = "http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";
//      myIcon.printImage = "http://hotbarspot.com/images/hbsPin.gif";
//      myIcon.mozPrintImage = "http://hotbarspot.com/images/hbsPin.gif";

      hbsIcon = new GIcon();
      hbsIcon.image = "http://hotbarspot.com/images/hbsPin.gif";
      hbsIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
      hbsIcon.iconSize = new GSize(20, 34);
      hbsIcon.shadowSize = new GSize(37, 34);
      hbsIcon.iconAnchor = new GPoint(9, 34);
      hbsIcon.infoWindowAnchor = new GPoint(9, 2);
      hbsIcon.infoShadowAnchor = new GPoint(18, 25);
      hbsIcon.transparent = "http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";
      hbsIcon.printImage = "http://hotbarspot.com/images/hbsPin.gif";
      hbsIcon.mozPrintImage = "http://hotbarspot.com/images/hbsPin.gif";

      grayIcon = new GIcon();
      grayIcon.image = "http://hotbarspot.com/images/grayPin.gif";
      grayIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
      grayIcon.iconSize = new GSize(20, 34);
      grayIcon.shadowSize = new GSize(37, 34);
      grayIcon.iconAnchor = new GPoint(9, 34);
      grayIcon.infoWindowAnchor = new GPoint(9, 2);
      grayIcon.infoShadowAnchor = new GPoint(18, 25);
      grayIcon.transparent = "http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";
      grayIcon.printImage = "http://hotbarspot.com/images/grayPin.gif";
      grayIcon.mozPrintImage = "http://hotbarspot.com/images/grayPin.gif";

      //icons = [];
      icons[0] = hbsIcon;
      icons[1] = grayIcon;

      //lat = document.getElementById("lat");
      //lng = document.getElementById("lng");
      info = document.getElementById("info");

      rg = new GReverseGeocoder(map);

      map = new GMap(document.getElementById("map"));

	 map.setCenter(new GLatLng(lat,lon),12 );
      map.addControl(new GLargeMapControl());
      map.setZoom(zoom);
      var center = map.getCenterLatLng();
      var point = new GLatLng(center.y,center.x);
      lat = center.y;
      lng = center.x;

      // add listners for the results
      GEvent.addListener(rg, "load", goodresult);
      GEvent.addListener(rg, "error", badresult);
      // Clicking on the map fills in the lat and lng fields
      // Just handy
      //GEvent.addListener(map, "click", handleClicks);
      //grab center of map and find zip
      GEvent.addListener(map, "dragend", findCenterZip);
      if ( lat != null && lng != null) {
	 //var xmlLink = "/mobile/admin/2queryLocation.php?zip="+zip;
	 var xmlLink = "/mobile/admin/queryLocation.php?lat="+lat+"&lon="+lng+"&features="+features;
	 //var xmlLink = "/admin/kml/zips/"+zip+".xml";
	 //alert(xmlLink);
	 exml = new EGeoXml("exml", map, xmlLink, {sidebarid:"side_bar",nozoom:true,sortbyname:true,iwwidth:300,directions:true,baseicon:icon});
	 exml.parse();
	 //currentZip = zip;
      } else {
	 exml = new EGeoXml("exml", map, "/mobile/admin/queryLocation.php?zip=43232", {sidebarid:"side_bar",nozoom:true,sortbyname:true,iwwidth:300,directions:true,baseicon:icon});
	 //currentZip = '';
      }

//      alert("Styles: "+exml.styles[0]);

      // simulate dragend on map with specified lat and lng
      /*
      if ( first ) {
	 findCenterZip();
	 first = false;
      }
      */
   }
}

// is called with a placemark if the reverse geocode request was successfull
// sets the result div
function goodresult(placemark) {
   clearMarkers();
   var center = map.getCenterLatLng();
   var point = new GLatLng(center.y,center.x);
   lat = center.y;
   lng = center.x;
   if ( lat != null && lng != null) {
      //var xmlLink = "/mobile/admin/2queryLocation.php?zip="+zip;
      var xmlLink = "/mobile/admin/2queryLocation.php?lat="+lat+"&lon="+lng;
      //var xmlLink = "/admin/kml/zips/"+zip+".xml";
      //alert(xmlLink);
      document.getElementById('side_bar').innerHTML = '';
      exml = new EGeoXml("exml", map, xmlLink, {sidebarid:"side_bar",nozoom:true,sortbyname:true,iwwidth:300,directions:true,baseicon:icon});
      exml.parse();
      //currentZip = zip;
   }
/*
   var html = placemark.address + '<br />' + '<b>Country code:</b> ' + placemark.AddressDetails.Country.CountryNameCode;
   info.innerHTML = html;
   var postalcodenumber = rg.getPlacemarkProperty(placemark,"PostalCodeNumber");
//   alert("postalzip: "+postalcodenumber+" & currentzip: "+currentZip);
   if ( currentZip != postalcodenumber ) {
      clearMarkers();
   }

   if(postalcodenumber != null)
   {
      if ( currentZip != postalcodenumber ) {
         map.removeOverlay(GMarker);
         document.getElementById('side_bar').innerHTML = '';

         exml = new EGeoXml("exml", map, "/admin/kml/zips/"+postalcodenumber+".xml", {sidebarid:"side_bar",sortbyname:true,iwwidth:300,directions:true,nozoom:true,baseicon:icon});
         exml.parse();
	 currentZip = postalcodenumber;
      }

   } else {
      bumpLocation();
   }
*/
}

// is called if the reverse geocode request was unsuccessfull
function badresult() {
   info.innerHTML = "Unable to reverse geocode";
   bumpLocation();
}

// get the input form lat and lng fields and issue a reverse geocode
// request
function reverse(){
   var point = new GLatLng(lat.value,lng.value);
   rg.reverseGeocode(point);
}

function findCenterZip(){
   var center = map.getCenterLatLng();
   var point = new GLatLng(center.y,center.x);
   lat = center.y;
   lng = center.x;
   rg.reverseGeocode(point);
}

// handy method to fill in the lat and lng fields by clicking on the map.
//function handleClicks(marker, point){
//   lat.value=point.lat();
//   lng.value=point.lng();

//}


