/**
 * Creates a google map
 *
 * Uses Google Maps API (msu include in before using this function)
 * Creates a map, centers it, zooms and places an optional marker
 * There must be an element with "gmap_map" id on a page
 * If interactive parameter is true, there also must be inputs with "gmap_lat" and "gmap_lng" ids for latitude and longtitude saving
 * If interactive parameter is true, there also must be an input with "gmap_zoom" id for zoom settings saving
 * 
 * @param boolean interactive If true, allows a user to place markers on the map with later saving possibility
 * @param integer zoom Map zoom
 * @param float center_lat Latitude of the map center
 * @param float center_lng Longtitude of the map center
 * @param float marker_lat If not empty - latitude of a marker
 * @param float marker_lng If not empty - longtitude of a marker
 * @return GMap2 A created map
 */
function create_google_map(interactive, zoom, center_lng, center_lat, marker_lng, marker_lat) {
    if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("gmap_map"));
        //map.addType(G_SATELLITE_MAP);
        map.addControl(new GMapTypeControl());
        map.addControl(new GScaleControl());
        //controls
        map.addControl(new GSmallMapControl());
        map.setCenter(new GLatLng(center_lat, center_lng), zoom);
        //place a marker
        if (marker_lng != 0 || marker_lat != 0) {
            //custom icon
            var custom_icon = new GIcon(G_DEFAULT_ICON);
            custom_icon.image = "/schools/frontend/img/gmap_marker.png";
            custom_icon.shadow = '';
            custom_icon.iconSize = new GSize(35, 71);
            custom_icon.iconAnchor = new GPoint(0, 71);
            if (interactive)
            	marker_options = { icon:custom_icon, draggable:true };
            else
            	marker_options = { icon:custom_icon };
            var marker = new GMarker(new GLatLng(marker_lat, marker_lng), marker_options);
            map.addOverlay(marker);
        }
        if (interactive) {
        	if (marker) {
	        	GEvent.addListener(marker, "dragend", function() {
	                //retrieve the longitude and lattitude of the click point
	                //set them into form fields
	                document.getElementById('gmap_lat').value = marker.getPoint().lat();
	                document.getElementById('gmap_lng').value = marker.getPoint().lng();
	                return true;
	        	});
	        }
        	
	        GEvent.addListener(map, "click", function(overlay, latlng) {
	            if (latlng) { //we need this check because the event fires on marker click too
	                //remove all markers
	                map.clearOverlays();
	                //add new marker
	                //custom icon
	                var custom_icon = new GIcon(G_DEFAULT_ICON);
	                custom_icon.image = "/schools/frontend/img/gmap_marker.png";
	                custom_icon.shadow = '';
	                custom_icon.iconSize = new GSize(35, 71);
	                custom_icon.iconAnchor = new GPoint(0, 71);
	                marker_options = { icon:custom_icon, draggable:true };
	                
	                marker = new GMarker(latlng, marker_options);
		        	GEvent.addListener(marker, "dragend", function(overlay, latlng) {
		                //retrieve the longitude and lattitude of the click point
		                //set them into form fields
		                document.getElementById('gmap_lat').value = marker.getPoint().lat();
		                document.getElementById('gmap_lng').value = marker.getPoint().lng();
		                return true;
		        	});
	                map.addOverlay(marker);
	                
	                //retrieve the longitude and lattitude of the click point
	                //set them into form fields
	                document.getElementById('gmap_lat').value = latlng.lat();
	                document.getElementById('gmap_lng').value = latlng.lng();
	            }
	            else { //a marker was clicked
	                //remove the marker
	                map.removeOverlay(overlay);
	                document.getElementById('gmap_lat').value = 0;
	                document.getElementById('gmap_lng').value = 0;
	            }
	        });
	        
	        
	    }
    }
    return map;
}

/**
 * Save current map center coordinates and zoom
 */
function save_google_map_center(current_map) {
    if (current_map) {
        latlng = current_map.getCenter();
        document.getElementById("gmap_center_lat").value = latlng.lat();
        document.getElementById("gmap_center_lng").value = latlng.lng();
        document.getElementById("gmap_zoom").value = current_map.getZoom();
    }
    return true;
}