﻿

/*
Google maps map controller
*/
function GoogleMapController(varname, listener, mapdiv)
{   
    //Global variable name    
    this.SVarname = varname;
    //Main reference to google maps  
    this.map = null;
    //Currently used zoom level
    //this.ICurrentZoomLevel = 11;
    this.ICurrentZoomLevel = 6;
   
    //Map Containing Div Name
    this.SMapDiv = mapdiv;
    
    //Listener
    this.listener = listener;
             
    
    this._p_addOnZoom = function(listener, thismapctl)
    {
       GEvent.addListener(this.map, "zoomend", 
         function(oldlevel, newlevel)
         {
            thismapctl.ICurrentZoomLevel = newlevel;
            if ( listener && listener.onZoom)     
	        listener.onZoom(newlevel);
         }                            
       );        
    } 


    this._p_addOnMove = function(listener, mapptr)
    {     
       GEvent.addListener(this.map, "moveend",
         function()
         {           
            var coords = mapptr.getCenter();
            listener.onMove(coords.lng(), coords.lat());
         }                            
       );    
    }


    this._p_convertBounds = function(gbounds)
    {
        var ptSW = gbounds.getSouthWest();
        var ptNE = gbounds.getNorthEast();
        var bounds = new MCBounds( 
        		new MCPoint(ptSW.lng(),ptSW.lat()), 
        		new MCPoint(ptNE.lng(),ptNE.lat())
        		);
        return bounds;
    }    

    
    //====== CONSTRUCTOR =====
    this.Constructor = function()
    {	
	if ( ! this.SMapDiv )
	   this.SMapDiv = "map";

        this.map = new GMap2(document.getElementById(this.SMapDiv));
        if ( this.map != null)
        {
           this.map.addControl(new GLargeMapControl());
           this.map.addControl(new GMapTypeControl());
           this.map.addControl(new GScaleControl());

           this._p_addOnZoom(this.listener, this);
           
           if ( this.listener != null)
           {                           
             if ( this.listener.onMove)
                this._p_addOnMove(this.listener, this.map);              
           }                        
        }
    }

    //====== PUBLIC =====  
    
    this.Constructor();
      
    /* Recenter map to the give coordinates position*/
    this.recenter = function(x, y)
    {
        this.map.setCenter(new GLatLng(y, x), this.ICurrentZoomLevel);
    }

    
    this.recenterAndZoom = function(x,y, zoomlevel)
    {
        this.ICurrentZoomLevel = zoomlevel;
        this.recenter(x, y);
    }

             
    /* Place marker on a map in (X, Y) and return reference to a marker object*/
    this.placeMarker = function(imgpath, x, y)
    {   
           //Creating an icon first
    	   var baseIconURL = imgpath;    	  	    
	       var baseIcon = new GIcon();
	       baseIcon.image = baseIconURL;
	       baseIcon.shadow = baseIconURL
	       //baseIcon.mozPrintImage = baseIconURL;
           baseIcon.iconSize = new GSize(25, 25);
           baseIcon.shadowSize = new GSize(25, 25);
           baseIcon.iconAnchor = new GPoint(12, 12); 
           
           //Now that icon is created, creating the marker itself
           var gmarker = new GMarker(new GLatLng(y, x), baseIcon);
           //var gmarker = new GMarker(new GLatLng(y, x));
           
           //Placing it on a map
           this.map.addOverlay(gmarker);
           
           //Creating and return General Marker Object
           var gmarkerAdaptor = new MarkerAdaptor(gmarker);
           return gmarkerAdaptor;
    }
    
    /* remove marker from the map, accepts marker (MarkerAdaptor) as a parameter*/
    this.removeMarker = function(marker)
    {
        this.map.removeOverlay(marker.gmarker);
    }                  


    this.getBounds = function()
    {
 	var gbounds = this.map.getBounds();
 	return this._p_convertBounds(gbounds);
    }
    
}//end class Google Map Controller


/*
Google marker adaptor to marker interface
*/
function MarkerAdaptor(gmarker)
{
    this.gmarker = gmarker;


    /* Function pointer listeners */
    this.addOnMouseOver = function(eventHandler)
    {
        GEvent.addListener(this.gmarker, "mouseover", eventHandler);  
    }

    this.addOnMouseOut = function(eventHandler)
    {
        GEvent.addListener(this.gmarker, "mouseout", eventHandler);  
    }
    
    this.addOnMouseClick = function(eventHandler)
    {
        GEvent.addListener(this.gmarker, "click", eventHandler);  
    }
}
