// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
var centerLatitude = 48.25; 
var centerLongitude = 16.38; 
var startZoom = 7; 
var map = null; // the google map
var mapData = null; // the data of googel maps stored seperately for furtehr processing
var geocoder = null;

function init() { 
	if (GBrowserIsCompatible()) { 

		if (map == null) {
			map = new GMap2($("#map")[0], {mapTypes:[ G_PHYSICAL_MAP, G_SATELLITE_MAP, G_HYBRID_MAP, G_NORMAL_MAP ]}); 
			map.addControl(new GSmallMapControl()); 
			map.addControl(new GMapTypeControl()); 

			var location = new GLatLng(centerLatitude, centerLongitude); 				
			map.setCenter(location, startZoom); 

			geocoder = new GClientGeocoder();			
		}
	} 
} 

// this function is caled when one single address is entered in to the textfield
// adds a marker, centers it and zooms to the given level
function showAddress(address, zoomLevel) {
	clear();
	geocoder.getLocations(
		address,
		function(locations) {

			if (locations.Status.code != '200') {
				alert(address + " not found");
			} else {
				var placemark = locations.Placemark[0];
				placemark.zoom = zoomLevel;
				var lat = placemark.Point.coordinates[1];
				var lng = placemark.Point.coordinates[0];
				var point = new GLatLng(lat, lng);

				var marker = new GMarker(point);
				map.addOverlay(marker);

				map.setZoom(zoomLevel)
				map.setCenter(point, zoomLevel);

				// add the point to mapData points array
				mapData.push(placemark);
			}
		}
	);
}

// this function is caled when multiple addresses are entered in to the textfields
// adds a marker for each of them and tehn zooms to fit them on the screen
function showMultipleAddresses(addressArray, zoomLevel) {
	clear();
	for (var i = 0; i < addressArray.length; i++) {
		var address = addressArray[i]
		geocoder.getLocations(
			address,
			function(locations) {

				if (locations.Status.code != '200') {
					alert(address + "<%= _('not found'[A]) %>");
				} else {				
					var placemark = locations.Placemark[0];
					placemark.zoom = zoomLevel;
					var lat = placemark.Point.coordinates[1];
					var lng = placemark.Point.coordinates[0];
					var point = new GLatLng(lat, lng);

					// create and add teh marker
					var marker = new GMarker(point);
					map.addOverlay(marker);

					// add the point to mapData points array
					mapData.push(placemark);

					// after adding the last marker: zoom to fit them all
					// plus: compact the point array in mapData		
					//TODO reenable zoomToMarker through the new MapToolkit API
				//	if (i == addressArray.length ) {
				//		map.zoomToMarkers(5);
				//	}
				}
			}
		);	
	}
}

// removes all markers and ste the zoom to default
function resetMap() {
	clear();
	var location = new GLatLng(centerLatitude, centerLongitude); 				
	map.setCenter(location, startZoom);
}

// clears all markers on the map and the related data
function clear() {
	map.clearOverlays();
	mapData = new Array();
}


function harvestGeoData() {
	$('#marker_points')[0].value = $.json.encode(mapData);
}

function getZoomLevel() {
	var zL = null;

	if( $('#geo_zoom_4')[0].checked ) {
		zL = $('#geo_zoom_4')[0].value

	} else if ( $('#geo_zoom_3')[0].checked ) {
		zL = $('#geo_zoom_3')[0].value

	} else if( $('#geo_zoom_2')[0].checked ) {
		zL = $('#geo_zoom_2')[0].value

	} else if( $('#geo_zoom_1')[0].checked ) {
		zL = $('#geo_zoom_1')[0].value

	} else if( $('#geo_zoom_0')[0].checked ) {
		zL = $('#geo_zoom_0')[0].value

	} else {
		zL = '7';
	}

	return parseInt(zL);
}



