/*
    .------------------------------.
    |    GoogleMapClass      |
    *------------------------------*         
    
    Class: Google map widget
        Allows us to play around with the google maps API
                
    Author: 
        Roland Haeusler
    
    Changelog:
        - 2008/09/26, haerolan Initial version

*/
          

/**
 * Initializes the google map
 * 
 * @param { Object } aGMapPackage
 * @param { Object } aMapCanvas
 * 
 * 
 * returns GoogleMap object
 */                                  
 
 
/*
 * The structure of aOverlays has the following attributes:
 * 
 * visible: true/false
 * name: string
 * id: string ( Google ID )
 * 
 */
function GMapClass     ( aGmapPackage, aMapCanvas )
{
	/**
	 * private properties
	 */
	var self = this;
	var gMap = null;
	
	var latitude = null;
	var longitude = null;
	var zoomlevel = null;
	var overlays = null;
	var visibleLayer = null;
	
	/**
	 * private functions
	 */
	
	/**
     * Initializes the GMap2 object and places ties it to the html node aMapCanvasNode
     * @param {Object} aMapCanvasNode
     * 
     * returns:
     *	GMap2 object gMap 
     */
	function initialize ( aMapCanvasNode )
    {
		try {
						
			latitude = aGmapPackage.latitude;
			longitude = aGmapPackage.longitude;
			zoomlevel = aGmapPackage.zoomlevel;
			overlays = aGmapPackage.overlays;
			visibleLayer = aGmapPackage.visiblelayer;
			
			if (GBrowserIsCompatible()) 
			{
				var mapCanvas = document.getElementById ("map_canvas" );

				gMap = new GMap2(mapCanvas);
				
				gMap.addControl(new GLargeMapControl());
				gMap.addControl(new GMapTypeControl());
				
				
				gMap.setCenter(new GLatLng(latitude, longitude), parseInt (zoomlevel));
			
				
				self.updateOverlays (overlays);
				
				console.log ( gMap.getSize ());
				
			}
			else 
			{
				throw "Incompatible browser";
			}
		}
		catch ( error )
		{
			console.error ( "GMap: Initialization failed. " + error );
		}
		
    }
    
	/**
	 * Adds an overlay to the map, based on the GeoXml data derived from google
	 * @param {Object} aOverlay
	 */
	function addOverlayToMap  ( aOverlay )
	{
		var gGeoxml = aOverlay.ggeoxml;
	    
		// If the geoxml data are already set, get them locally
		if ( gGeoxml != null )
		{
			gGeoxml = aOverlay.ggeoxml;
		}
		else
		{
			// Else get them from google
			gGeoxml = new GGeoXml( "http://maps.google.de/maps/ms?ie=UTF8&hl=de&msa=0&msid=" + aOverlay.id + "&output=kml" );
	    	
			// Store the geoxml data to the overlay (needed for later removal)
			aOverlay.ggeoxml = gGeoxml;
		}
	    
		// Add the overlay to the map
		gMap.addOverlay( gGeoxml );
	    	
	}
    
	/**
	 * public functions
	 */

	/**
	 * Toggles the state of a map layer
	 * @param {Object} aName is the name of the layer
	 */
	this.toggleOverlay = function (aName) 
	{
		try
		{
			// find an overlay with this name
			var overlay = null;
			for ( var counter = 0; counter < overlays.length; counter++ )
			{
				if ( overlays [counter].name == aName)
				{
					overlay = overlays[counter];
					break;
				}
			}
	        
			if (overlay)
			{
				if (overlay.visible === true) 
				{
					gMap.removeOverlay ( overlay.ggeoxml );
					overlay.visible = false;
				}
				else 
				{
					addOverlayToMap ( overlay )
					overlay.visible = true;
				}
	        	
				var checkbox = dojoWrapper.getById ( overlay.name + '_checkbox' );
				if ( checkbox != null )
				{
					checkbox.checked = (checkbox.visible === true);
				}
			}
		}
		catch ( error )
		{
			console.error ( "GMap: " + error );
		}
		
		
	}

    
	/**
	 * Updates the map with new coordinates & zoomlevel
	 * @param {Number} aLatitude
	 * @param {Number} aLongitude
	 * @param {Number} aZoomlevel
	 */
	this.updateMap = function ( aLatitude, aLongitude, aZoomlevel )
	{
		try
		{
			gMap.setCenter ( new GLatLng ( aLatitude, aLongitude ), parseInt (aZoomlevel) ); 	
		}
		catch ( error )
		{
			console.error ( "GMap: " + error );
		}
	}
    
    
	/**
	 * Loads new overlays into the object
	 * @param {Object} aOverlays 
	 */
	this.updateOverlays = function ( aOverlays )
	{
		try
		{
			// Clear all overlays
			gMap.clearOverlays ();
			
			// Set new overlays
			overlays = aOverlays;
			
			// Add the visible overlays
			for ( var i = 0; i < overlays.length; i++ )
			{
				// Add overlay to the map, if visible
				if ( !visibleLayer || visibleLayer == overlays [ i ] )
				{
					addOverlayToMap ( overlays [ i ] );
					overlays [i].visible = true;
				}
		   
				// Store the visibility setting from the overlay menu checkbox to 'mapOverlays'
				var checkbox = dojoWrapper.getById ( overlays [ i ].name + '_checkbox' );
	   
				
				if ( checkbox ) 
				{
					checkbox.checked = ( overlays [i].visible === true );
				}
			}
		}
		catch ( error )
		{
			console.error ( "GMap: " + error );
		}
	}
	
	/**
	 * Checks if the canvas size has changed and modifies the map accordingly 
	 */
	this.resize = function ()
	{
		try
		{
			gMap.checkResize ();
			
		}
		catch ( error )
		{
			console.error ( "GMap: " + error );
		}
		
	}

	
	initialize ( aMapCanvas );
 }
