google.load("maps", "2");

// Global variables
var mode = 0; //0 = view; 1 = Edit
var mapCenterLat = 50.079176;
var mapCenterLong = 14.412346;
var mapZoom = 7;
var icon_url = "/images/gmap/"; //"http://labs.google.com/ridefinder/images/";

var map;
var mapdiv = document.getElementById("ele_gmap_map");
var poly;
var point;
var marker;

function gMapSetVariable(variable, value)
{
	if (/^icon_url$/.test(variable)) //fillColor
		eval(variable + ' = "' + value + '";');
	else
		eval(variable + ' = ' + value + ';');
}

//SAVE FUNCTIONS
function gMapGetCoordField()
{
	if (point)
		return map.getCenter() + "/" + map.getZoom() + "/" + point;
	return "";
}
//

//RESTORE FUNCTIONS
function gMapImportConfiguration(configStr)
{
	var tempArray = configStr.split("/");
	if (tempArray.length > 1)
	{
		//Keep coords
		configStr = tempArray[tempArray.length-1];

		//Set Center
		var center = tempArray[0];
		var centerArray = center.split(",");
		mapCenterLat = parseFloat(centerArray[0]);
		mapCenterLong = parseFloat(centerArray[1]);
		
		//Set zoom
		if (tempArray.length > 2)
		{
			var zoom = tempArray[1];
			mapZoom = parseFloat(zoom);
		}
	}
	
	var coordArray = configStr.split(",");
	point = coordArray;
}

function loadPoint()
{
	if (point)
		leftClick(null, new GLatLng(parseFloat(point[0]), parseFloat(point[1])));
}
//


function addIcon(icon) { // Add icon attributes
	icon.shadow = icon_url + "mm_20_shadow.png";
 	icon.shadowSize = new GSize(22, 20);
  
	icon.iconSize = new GSize(12, 20); 
	icon.iconAnchor = new GPoint(6, 20);
	icon.infoWindowAnchor = new GPoint(5, 1);
}

function buildMap() {

 map = new GMap2(mapdiv, {draggableCursor:"auto", draggingCursor:"move"});

 // Load initial map and a bunch of controls
 map.setCenter(new GLatLng(mapCenterLat, mapCenterLong), mapZoom);
 map.addControl(new GLargeMapControl()); // Zoom control
 map.addMapType(G_PHYSICAL_MAP);
 // Create a hierarchical map type control
 var hierarchy = new GHierarchicalMapTypeControl();
 // make Hybrid the Satellite default
 hierarchy.addRelationship(G_SATELLITE_MAP, G_HYBRID_MAP, "Labels", true);
 // add the control to the map
 map.addControl(hierarchy);

 map.addControl(new GScaleControl()); // Scale bar
 map.disableDoubleClickZoom();

 // Add click listener
 if (mode == 1)
 	GEvent.addListener(map, "click", leftClick);
 
 loadPoint();
}

function leftClick(overlay, pt) 
{
	if(!pt)
		return;
		
	clearMap(); 

	// Light blue marker icons
	var icon = new GIcon();
	icon.image = icon_url + "mm_20_lightblue.png";
	addIcon(icon);

	// Make markers draggable
	marker = new GMarker(pt, {icon:icon, draggable:mode, bouncy:false, dragCrossMove:true});
	map.addOverlay(marker);
	point = pt;

	if (mode == 1)
	{
		// Drag listener
		GEvent.addListener(marker, "drag", function() 
		{
			point = marker.getLatLng();
		});
	
		// Second click listener
		GEvent.addListener(marker, "dblclick", function() 
		{
			// Find out which marker to remove
			map.removeOverlay(marker);
			marker = null;
		});
	}
}

function clearMap() 
{
	// Clear current map and reset arrays
	map.clearOverlays();
	point = null;
 	marker = null;
}

google.setOnLoadCallback(buildMap);
