﻿var map,redIcon,greenIcon;
var lastCenter = new GLatLng();
var jsonData = null;
var start=new Date();
var bWinOpen=false;
function init() {    
    if (GBrowserIsCompatible()) {
      // Display the map, with some controls      
      map = new GMap2(document.getElementById("map"));
      map.addControl(new GLargeMapControl());      
      map.setCenter(new GLatLng(defLat,defLng),defAcc);
      lastCenter = map.getCenter();
      GEvent.addListener(map, "moveend", function() {if((new Date()-start)>400){if(!lastCenter.equals(map.getCenter()))loadMap();else lastCenter=map.getCenter();}start=new Date();});
      GEvent.addListener(map, "zoomend", function() {if(lastCenter.equals(map.getCenter()))loadMap();});
      GEvent.addListener(map, 'infowindowopen', function(){bWinOpen=true;});
      redIcon = new GIcon(G_DEFAULT_ICON);
      redIcon.image = "images/upload_icons_icon4-49.png";
      redIcon.shadow = "";//"images/upload_icons_icon4-49s.png";
      redIcon.iconSize = new GSize(16, 16);
      //redIcon.shadowSize = new GSize(56, 32);
      redIcon.iconAnchor = new GPoint(8, 8);
      redIcon.infoWindowAnchor = new GPoint(16, 0);
            
      greenIcon = new GIcon(G_DEFAULT_ICON);
      greenIcon.image = "images/mm_20_green.png";
      greenIcon.shadow = "images/mm_20_shadow.png";
      greenIcon.iconSize = new GSize(12, 20);
      greenIcon.shadowSize = new GSize(22, 20);
      greenIcon.iconAnchor = new GPoint(6, 20);
      greenIcon.infoWindowAnchor = new GPoint(5, 1);             

      loadMap();
    }
    else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }

}                  
      // A function to create the marker and set up the event window
      function createMarker(point,name,data) {
        var i;
        if(map.getZoom()<11)i=redIcon;else i=greenIcon;
        var marker = new GMarker(point,{title:name,icon:i});        
        GEvent.addListener(marker, "click", function() {
            if(map.getZoom()<8){map.setCenter(point,8);}else if(map.getZoom()<11){map.setCenter(point,11);}
            else openInfoWindow(marker,"" + data);                        		        
                              
        });        
        return marker;
      }      
      function openInfoWindow(marker,data) {
           marker.openInfoWindowHtml("Loading show...");
           fetchDetails(marker,data);
      }
      
      function fetchDetails(marker,id) {
          var req = GXmlHttp.create();
          req.open("GET", "GetShowBubble.aspx?id="+id, true);          
          req.onreadystatechange = function(){if(req.readyState == 4){marker.openInfoWindowHtml(req.responseText)}};
          req.send(null);
      }
        
      process_it = function(doc) {
        // === Parse the JSON document === 
        jsonData = eval('(' + doc + ')');        
	
        // === Plot the markers ===
        for (var i=0; i<jsonData.markers.length; i++) {
          var point = new GLatLng(jsonData.markers[i].lat, jsonData.markers[i].lng);
          var marker = createMarker(point, jsonData.markers[i].l, jsonData.markers[i].d);
          map.addOverlay(marker);
        }
      }                      
  
    function loadMap() {            
        if (!bWinOpen) {
        // remove marker click handlers, if present
        if (jsonData != null){            
            for (var i = 0; i < jsonData.markers.length; i++) {
	            if (jsonData.markers[i].my_clickToken) {
		            GEvent.removeListener(jsonData.markers[i].my_clickToken);
		            delete jsonData.markers[i].my_clickToken;
	            }
            }
        if(map.getInfoWindow()!=null)map.closeInfoWindow();
            map.clearOverlays();
            jsonData.markers=[];	
            jsonData = null;
        }
        var b=map.getBounds()
        GDownloadUrl("GetMarkers.aspx?a="+b.getSouthWest().lat()+"&b="+b.getSouthWest().lng()+"&c="+b.getNorthEast().lat()+"&d="+b.getNorthEast().lng()+"&z="+map.getZoom(), process_it);	    
        }
        bWinOpen=false;
    }

//window.onload = function() { init(); }
window.onunload = function() { GUnload(); } 