//  CREATE OBJECT ------------------------------------------------------------------------------- #    
    var Gmaps = new Object();
    
//  MAP ID -------------------------------------------------------------------------------------- #
    Gmaps.nMapId = 0;   // this value should be initialised with map id
    
//  STORE INFORMATIONS -------------------------------------------------------------------------- #
    Gmaps.lastClickPosition = null;
    Gmaps.aShapeCoordinates = new Array();
    Gmaps.oPolygon = null;

//  PHP FILE FOR SERVER OPERATIONS -------------------------------------------------------------- #
    Gmaps.sServerFileUrl = '../GMAPS/gmaps.php';
    
//  MAIN PHP FILE ------------------------------------------------------------------------------- #
    Gmaps.sMainInterfaceFile = 'show_map.php';

//  MAIN AJAX TARGET ---------------------------------------------------------------------------- #
    Gmaps.sMainAjaxTarget = '#gmaps';
    Gmaps.sMainMarkersList = '#markers_list';
    
//  SAVE MAP SIZE ------------------------------------------------------------------------------- #
    //  aParams is an array of values that will be passed to the server
    Gmaps.saveMapInfo = function(aParams)
    {
        $(Gmaps.sMainAjaxTarget).load(Gmaps.sServerFileUrl, aParams, function(){
            window.location.href = 'show_map.php';   
        })
    }  
            
//  UPDATE MAP TYPE ----------------------------------------------------------------------------- #
    Gmaps.updateMapType = function(oMap)
    {
         $("#map_type option[value='" + oMap.mapTypeId + "']").attr({selected : 'selected'});
    }
    

//  UPDATE BOUNDS ------------------------------------------------------------------------------- #
    Gmaps.updatePosition = function(oMap)
    {
        $("#bounds_ne").val(oMap.getBounds().getNorthEast());
        $("#bounds_sw").val(oMap.getBounds().getSouthWest());
        $("#map_center_lat").val(oMap.getBounds().getCenter().lat());
        $("#map_center_lng").val(oMap.getBounds().getCenter().lng());
    }
    
//  POLYGON ------------------------------------------------------------------------------------- #
    Gmaps.drawPolygon = function(oMap, oCoordinates, color, color2,fill_opacity)
    {
    	// set fillOpacity
    	temp = 0;
    	if (fill_opacity == true) {
			temp = 1;
    	}
        polygon = new google.maps.Polygon({
            paths: oCoordinates,
            strokeColor: color2,
            strokeOpacity: 0.9,
            strokeWeight: 1,
            fillColor: color,
            fillOpacity: temp
        });
        
        //polygon.setMap(oMap);
        
        return polygon;
    }
    
    Gmaps.coordinatesPolygon = function (sCoordinates) 
    {
    	// recovers coordinates from string
		var aCoordinates = sCoordinates.split("|");
		var aNewCoordinates =  new Array();
		for(var oLatLng in aCoordinates) {
			temp = aCoordinates[oLatLng].split(",");
			aNewCoordinates.push(new google.maps.LatLng(temp[0],temp[1]));
		} 
		return aNewCoordinates;
    }
    
    Gmaps.saveZone = function(nMapId, nZoneName, oCoordinates, sColor)
    {
        // build a string of coordinates separated by a pipe 
        var aCoordinates = new Array();
        for(var oLatLng in oCoordinates)
        {
            aCoordinates.push(oCoordinates[oLatLng].lat() + ',' + oCoordinates[oLatLng].lng()); 
        }
        var sCoordinates = aCoordinates.join('|');
        
        // save
        var aParams = { sAction: 'save_zone',
                        nMapId: nMapId,
                        nZoneName: nZoneName,
                        sColor: sColor,
                        sCoordinates: sCoordinates            
        }
        $('#val_zone_id').load(Gmaps.sServerFileUrl, aParams);
    }
    // SAVE MAP STYLE NAME -------------------------------------------------------------------------- #
	Gmaps.saveMapNameStyle = function(aParams)
    {
         $(Gmaps.sMainAjaxTarget).load(Gmaps.sServerFileUrl, aParams, function(){
            window.location.href = Gmaps.sMainInterfaceFile;   
        })        	
    }   
    
    Gmaps.deleteZone = function(nElemId)
    {
    	var aParams = { sAction: 'delete_zone',
                        nElemId: nElemId            
        }
    	$(Gmaps.sMainAjaxTarget).load(Gmaps.sServerFileUrl, aParams, function(){
            window.location.href = Gmaps.sMainInterfaceFile;   
        })        	
	}
	Gmaps.addZoneClick = function(nElemId, nVal)
	{
		var aParams = { sAction: 'clickable',
		                nElemId: nElemId,
		                nVal: nVal
		}
		$(Gmaps.sMainAjaxTarget).load(Gmaps.sServerFileUrl, aParams, function(){
            window.location.href = Gmaps.sMainInterfaceFile;   
        })        		
	}
	
	Gmaps.deleteMapZone = function(nMapId)
    {
    	var aParams = { sAction: 'delete_map_zone',
                        nMapId: nMapId            
        }
    	$(Gmaps.sMainAjaxTarget).load(Gmaps.sServerFileUrl, aParams, function(){
            window.location.href = Gmaps.sMainInterfaceFile;   
        })        	
	}
	
	Gmaps.showZone = function(nZoneId,nDisplay)
    {
    	var aParams = { sAction: 'show_zone',
                        nZoneId: nZoneId,
                        nDisplay: nDisplay            
        }
        //  alert(nZoneId);           
    	$(Gmaps.sMainAjaxTarget).load(Gmaps.sServerFileUrl, aParams, function(){
            window.location.href = Gmaps.sMainInterfaceFile;   
        })        		
	}
                
//  EVENTS -------------------------------------------------------------------------------------- #
    //  ZOOM CHANGE 
        //  if the zoom is changed in the map, it will update the zoom select in the form
        Gmaps.changeZoomFromMapEvent = function(nLevel)
        {
            $("#zoom_level option[value='" + nLevel + "']").attr({selected : 'selected'});
        }   
    
        //  if the zoom is changed from the form, update the map 
        //  oMap must be a Google Maps V3 object
        Gmaps.changeZoomFromSelect = function(oMap, nLevel)
        {
            oMap.setZoom(parseInt(nLevel));
        }  
        
        
    //  MAP TYPE CHANGE
        Gmaps.updateMapTypeFromSelect = function(oMap)
        {
            oMap.setMapTypeId($("#map_type option:selected").val());
        }
        
    //  GET MOUSE POSITION ON THE MAP
        Gmaps.getMousePosition = function(event)
        {
            //  save position
            Gmaps.lastClickPosition = event.latLng;
            //  show position
            $("#last_lat").val(event.latLng.lat().toFixed(6));
            $("#last_lng").val(event.latLng.lng().toFixed(6));
            
            return event.latLng;
        }
        
//  MARKERS ------------------------------------------------------------------------------------- #
    //  CREATE A MARKER
        Gmaps.createMarkerFromClick = function(oMap, oLatLng, sTitle)
        {
            //  create the marker on the map
            var marker = new google.maps.Marker({
                position: oLatLng, 
                map: oMap,
                title: sTitle
            }); 
            
            //  update html markers list
            Gmaps.createMarkerInDom(oLatLng, sTitle);
        }
        
        Gmaps.createMarkerInDom = function (oLatLng, sTitle)
        {
            //  save the marker into the DB
            var aParams = {
                            sAction: 'save_marker',
                            sName: sTitle,
                            fLat: oLatLng.lat(),
                            fLng: oLatLng.lng(),
                            nMapId: Gmaps.nMapId
                        }
            Gmaps.saveNewMarker(aParams);     
        } 
        
        Gmaps.saveNewMarker = function(aParams)
        {
            $(Gmaps.sMainAjaxTarget).load(Gmaps.sServerFileUrl, aParams, function(){
                window.location.href = Gmaps.sMainInterfaceFile;   
            })     
        }
        
        Gmaps.updateMarker = function(aParams)
        {
            //  needs : nMarkerId, sName, fLat, fLng
            $(Gmaps.sMainAjaxTarget).load(Gmaps.sServerFileUrl, aParams, function(){
                window.location.href = Gmaps.sMainInterfaceFile; 
            })   
        }
        
// ADD TO MAP ------------------------------------------------------------------------------------ #
		// Shows any overlays currently in the array
	    Gmaps.showOverlays = function (oMap, aParams) 
	    {
	    	// removes overlays form map
	    	if (aParams) {
			    for (i in aParams) {
			      aParams[i].setMap(oMap);
			    }
			  }
	    }
	    Gmaps.showOverlay = function (oMap, oParam) 
	    {
	    	// removes overlays form map
	    	if (oParam) {
			      oParam.setMap(oMap);
			  }
	    }
        
// CLEAR MAP ------------------------------------------------------------------------------------ #
		// Removes the overlays from the map, but keeps them in the array  
	    Gmaps.clearOverlays = function (aParams) 
	    {
	    	//  removes previous version of the polygon if exists    
	    	if (Gmaps.oPolygon != null) {
	    		Gmaps.aShapeCoordinates = new Array(); 
	    		Gmaps.oPolygon = null;
			}
			// removes overlays form map
	    	if (aParams) {
			    for (i in aParams) {
			      aParams[i].setMap(null);
			    }
			  }
	    }
	    Gmaps.clearOverlay = function (oParam) 
	    {
			// removes overlays form map
	    	if (oParam) {
			      oParam.setMap(null);
			  }
	    }  
             
		
