﻿var map = null;
var geocoder = null;
var locations = new Array();
var bounds = null;
    
    function LabeledMarker(latlng, options){
        this.latlng = latlng;
        this.labelText = options.labelText || "";
        this.labelClass = options.labelClass || "markerLabel";
        this.labelOffset = options.labelOffset || new GSize(0, 0);
        GMarker.apply(this, arguments);   
    }
    
    LabeledMarker.prototype = new GMarker(new GLatLng(0, 0));
    
    LabeledMarker.prototype.initialize = function(map) {
	       GMarker.prototype.initialize.call(this, map);

	        var div = document.createElement("div");
	        div.className = this.labelClass;
	        div.innerHTML = this.labelText;
	        
	        div.style.position = "absolute";
	        div.style.color = "white";
	        div.style.fontSize = "10pt";
	        div.style.fontWeight = "Bolder";
	        
	        map.getPane(G_MAP_MARKER_PANE).appendChild(div);

	        this.map = map;
	        this.div = div;
        }
    
    LabeledMarker.prototype.redraw = function(force) {
	        GMarker.prototype.redraw.call(this, map);

	        if (!force) return;

	        var p = this.map.fromLatLngToDivPixel(this.latlng);
	        var z = GOverlay.getZIndex(this.latlng.lat());

	        this.div.style.left = (p.x + this.labelOffset.width) + "px";
	        this.div.style.top = (p.y + this.labelOffset.height) + "px";
	        this.div.style.zIndex = z + 1; 
	        
	        LabeledMarker.prototype.remove = function() {
                this.div.parentNode.removeChild(this.div);
                this.div = null;
                GMarker.prototype.remove.call(this);
            }
        }
    
    LabeledMarker.prototype.redraw = function(force) {
	        GMarker.prototype.redraw.call(this, map);

	        if (!force) return;

	        var p = this.map.fromLatLngToDivPixel(this.latlng);
	        var z = GOverlay.getZIndex(this.latlng.lat());

	        this.div.style.left = (p.x + this.labelOffset.width) + "px";
	        this.div.style.top = (p.y + this.labelOffset.height) + "px";
	        this.div.style.zIndex = z + 1;
	        
	        LabeledMarker.prototype.remove = function() {
                this.div.parentNode.removeChild(this.div);
                this.div = null;
                GMarker.prototype.remove.call(this);
            }
    }
            
    function load() {
      if (GBrowserIsCompatible()) {
        if(!map){
            map = new GMap2(document.getElementById("map"));
            geocoder = new GClientGeocoder();
            map.addControl(new GLargeMapControl());
            map.addControl(new GMapTypeControl());
            map.enableScrollWheelZoom();
        }           
      }
    }
    
    function highlightMarker(index, address, info){
        try{
            index--;
            locations[index].openInfoWindowHtml("<font size='3pt'><b>" + info + "</b>" + "<BR>" + address + "</font>");
        }
        catch(err){
            
        }
    }
    
    function clearMap(){
        map.clearOverlays();
        map.setZoom(13);
    }
    
    function adjustZoomLevel(){
        
    }
            
    function putMarker(address, info, number) {
      //alert('putMarker called');
      if (geocoder) {
        geocoder.getLatLng(address,
          function(point) 
          {
            if (!point) 
            {
               //alert(address + " not found *put marker*");               
            } 
            else 
            {
              try
              {                              
                bounds = map.getBounds();
                bounds.extend(point);
                map.setZoom(map.getBoundsZoomLevel(bounds));          
                        
                var icon = new GIcon();
                icon.image = _appPath + "/Images/maptag.png";
                icon.iconSize = new GSize(32, 32);
                icon.iconAnchor = new GPoint(16, 16);
                var osx = -2 + (number.length * - 2.5);
                var options = {icon: icon, title: info, clickable: true, draggable: false, labelText: number, labelOffset: new GSize(osx, -15)};
                                             
                var marker = new LabeledMarker(point, options); 
                //alert(market);
                map.addOverlay(marker);                	        
                
                //GEvent.addListener(marker, "click", function(){
                //    marker.openInfoWindowHtml("<font size='3pt'><b>" + info + "</b>" + "<BR>" + address + "</font>");
                //});
               }
               catch(err)
               { 
                //alert('error: ' + err.description);              
               }                     
            }
          }
        );
      }
    }
    
    function closeInfoWindow(marker){
    
    }

    function showAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
               //alert(address + " not found *show address*");
            } else {
                try{
                map.setCenter(point, 13);
                //var marker = new GMarker(point);
                //map.addOverlay(marker, G_DEFAULT_ICON, false);
                //marker.openInfoWindowHtml(address);
                //window.setTimeout(function(){marker.closeInfoWindow();map.removeOverlay(marker);}, 4000);
                /*GEvent.addListener(marker, "click", function(){
                    marker.openInfoWindowHtml("<font size='3pt'><b>" + address + "</b></font>");
                    window.setTimeout(function(){marker.closeInfoWindow();}, 2000);
                });*/
                }
                catch(err){
                    alert(err);
                }              
            }
          }
        );
      }
    }

