var map = null;
var foto = 0;
var geo = new GClientGeocoder(new GGeocodeCache());

function load() {
	if (GBrowserIsCompatible()) 	{
		map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(48.636538782610494, 19.632568359375), 8);
		map.addControl(new GLargeMapControl()); 
		map.setMapType(G_NORMAL_MAP);

		map.enableDoubleClickZoom();
		map.enableScrollWheelZoom();
		map.enableContinuousZoom();
		GEvent.addDomListener(map.getContainer(), "DOMMouseScroll", wheelevent);
		map.getContainer().onmousewheel = wheelevent;
		
		directionsPanel = document.getElementById("route");
		directions = new GDirections(map, directionsPanel);
		
		active="vyhladavanie"
		document.getElementById("search").focus();
	}
}

function wheelevent(e) {
		if (!e){
			e = window.event
		}
		if (e.preventDefault) {
			e.preventDefault()
		}
		e.returnValue = false;
}

function showAddress() {
	var search = document.getElementById("search").value + ", Slovakia";
	// ====== Perform the Geocoding ======        
	geo.getLatLng(search, function (point)
		{ 
			// ===== If that was successful, plot the point and centre the map ======
			if (point) {
			  var marker = new GMarker(point);
			  map.addOverlay(marker);
			  map.setCenter(point,14);
			}
			// ====== Decode the error status ======
			else {
			  // ==Look to see if the query was cached ==
			  var result=geo.getCache().get(search);
			  if (result) {
				var reason="Code "+result.Status.code;
				if (reasons[result.Status.code]) {
				  reason = reasons[result.Status.code]
				}
			  } else {
				var reason = "";
			  } 
			  alert('Could not find "'+search+ '" ' + reason);
			}
		  }
		);
}

function toggleFoto(){
	geoXml = new GGeoXml("http://www.panoramio.com/kml/");
	if(foto==0) {
		map.addOverlay(geoXml);
		foto = 1;
	} else {
		map.clearOverlays();
		foto = 0;		
	}
}

function findDirections() {
	directions.clear();
	var from = document.getElementById("start").value;
	var to = document.getElementById("finish").value;
	directions.load("from: " + from + " to: " + to, { preserveViewport: true, getSteps: true });
 	//directions.load("from: 500 Memorial Drive, Cambridge, MA to: 4 Yawkey Way, Boston, MA 02215 (Fenway Park)");
	//map.setZoom(direcions.getBounds.getBoundsZoomLevel(bounds));
	
	GEvent.addListener(directions, "load", function() {
			//var distance = directions.getDistance().meters / metersPerMile;
			//var response = directions.getStatus();
			//var miles = Math.round(distance);

			var bounds = directions.getBounds();
			map.setCenter( bounds.getCenter(),
				map.getBoundsZoomLevel(bounds) );		
	});

	
	//alert(directions.getBounds());
	//map.setCenter(new GLatLng( 48.153091,17.129709), 13);
	
	//GDirections.getBounds()
}

function clearDirections() {
	directions.clear();
}
