/* static center and zoom */
var centerLatitude = 54.2;
var centerLongitude = 10.1;
var startZoom = 9;
var sGeobytesLatitude = 0;
var sGeobytesLongitude = 0;

/* global map object */
var map;
var searchMarker;
var gmarkers= [];

function addSimpleMarker(latitude,longitude,description) {
  var marker = new GMarker(new GLatLng(latitude, longitude));
  GEvent.addListener(marker, 'click',
		function() {
			marker.openInfoWindowHtml(description);
		}
  );
  map.addOverlay(marker);
}

function addCrosshair(latitude, longitude, image, shadow, description) {

  var icon = new GIcon();
	icon.image = image;
	icon.iconSize = new GSize(41, 41);
	
//	icon.shadow = shadow;
//	icon.shadowSize = new GSize(75, 60);
	
	icon.iconAnchor = new GPoint(21, 21);
	// Anker über dem Icon
	icon.infoWindowAnchor = new GPoint(22, 10);
	// Anker unten links
	icon.infoWindowAnchor = new GPoint(21, 21);

	var marker = new GMarker(new GLatLng(latitude, longitude), icon);
	GEvent.addListener(marker, 'click',
		function() {
			marker.openInfoWindowHtml(description);
		}
	);
	map.addOverlay(marker);
}
function importanceOrder(marker, b) {
    return marker.importance * -1;
}
function addCustomMarker(latitude, longitude, image, shadow, html) {

  var icon = new GIcon();
	icon.image = image;
	icon.shadow = shadow;
	icon.iconSize = new GSize(50, 65);
	icon.shadowSize = new GSize(75, 60);
	icon.iconAnchor = new GPoint(15, 65);
  // Anker über dem Icon
  icon.infoWindowAnchor = new GPoint(22, 10);
  // Anker unten links
	//icon.infoWindowAnchor = new GPoint(0, 65);
	var point = new GLatLng(latitude, longitude);
	var marker = new google.maps.Marker(point, {icon:icon});
	GEvent.addListener(marker, 'click', function() { marker.openInfoWindowHtml(html); });

	
	return marker;

}

function init() {
		var geo = new GClientGeocoder();
		var map = new google.maps.Map2(document.getElementById('map'));
		map.addControl(new GSmallMapControl());
		map.addControl(new GScaleControl());
		map.setCenter(new GLatLng(0,0),1);
		
		
		// dynamically select zoom and center
		var bounds = new GLatLngBounds();
		
		var min_distance = 0;
		var min_distance_2 = 0;
		var min_latitude = 0;
		var min_latitude_2 = 0;
		var min_longitude = 0;
		var min_longitude_2 = 0;
		var min_data = '';
		var distance = 0;
		var alpha = 0;
		var markers_new = [];
		
		//frankentahl
		/*sGeobytesLatitude = '49.543758';
		sGeobytesLongitude = '8.361111';
		*/
		
		if (sGeobytesLatitude && sGeobytesLongitude){
			var queryUrl =	"http://www.mein-profi.de/?" + jQuery.param({
				eID: "json",
				setNewMarker: sGeobytesLatitude+','+sGeobytesLongitude
			});
			jQuery.getJSON(
				queryUrl,
				function(data) {
					for (j=0;j<data.jsreturn.length; j++){
						//alert("markers_new 1: "+data.jsreturn[j]);
						markers_new.push({
						'latitude': data.jsreturn[j].latitude, 
						'longitude': data.jsreturn[j].longitude,
						'name': data.jsreturn[j].name,
						'image': data.jsreturn[j].image,
						'shadow': data.jsreturn[j].shadow
						});
					}
					for(id in markers_new) {
						//addMarker(markers[id].latitude, markers[id].longitude, markers[id].name);
						var marker = addCustomMarker(markers_new[id].latitude, markers_new[id].longitude, markers_new[id].image, markers_new[id].shadow, markers_new[id].name);
						marker.importance = 2;
						map.addOverlay(marker);
						bounds.extend(new GLatLng(markers_new[id].latitude, markers_new[id].longitude));
						
						if (markers_new[id].latitude && markers_new[id].longitude){
							
							alpha = Math.acos(Math.sin(deg2rad(markers_new[id].latitude))*Math.sin(deg2rad(sGeobytesLatitude))+Math.cos(deg2rad(markers_new[id].latitude))*Math.cos(deg2rad(sGeobytesLatitude))*Math.cos(deg2rad(markers_new[id].longitude) - deg2rad(sGeobytesLongitude)));
							distance=alpha * 6371;
							//alert('min_distance: '+min_distance+', min_distance_2: '+min_distance_2);
							if (min_distance_2==0 || min_distance_2 > distance){
								min_distance_2 = distance;
								min_latitude_2 = markers_new[id].latitude;
								min_longitude_2 = markers_new[id].longitude;
							}
						}
					}
					if (min_distance_2 && min_distance && min_distance_2 < min_distance){
						var showCenterLocation_2 = new GLatLng(min_latitude_2, min_longitude_2);
						map.setCenter(showCenterLocation_2, 12);
					}
				}
			);
		}

		for(id in markers) {
			
			//addMarker(markers[id].latitude, markers[id].longitude, markers[id].name);
			
			var marker = addCustomMarker(markers[id].latitude, markers[id].longitude, markers[id].image, markers[id].shadow, markers[id].name);
			marker.importance = 2;
			map.addOverlay(marker);
			bounds.extend(new GLatLng(markers[id].latitude, markers[id].longitude));
			
			if (sGeobytesLatitude && sGeobytesLongitude){
				
				alpha = Math.acos(Math.sin(deg2rad(markers[id].latitude))*Math.sin(deg2rad(sGeobytesLatitude))+Math.cos(deg2rad(markers[id].latitude))*Math.cos(deg2rad(sGeobytesLatitude))*Math.cos(deg2rad(markers[id].longitude) - deg2rad(sGeobytesLongitude)));
				distance=alpha * 6371;
				if (min_distance==0 || min_distance > distance){
					min_distance = distance;
					min_latitude = markers[id].latitude;
					min_longitude = markers[id].longitude;
				}
			}
		}
		
		
		
		if (markers.length == 1) {
			if (markers[0].zoom) {
				map.setZoom(markers[0].zoom);
			} else {
				map.setZoom(12);
			}
		} else {
			map.setZoom(map.getBoundsZoomLevel(bounds));
			
			
		}
		
		if (sGeobytesLatitude && sGeobytesLongitude){
			
			if (min_latitude){
				var showCenterLocation = new GLatLng(min_latitude, min_longitude);
			}else{
				var showCenterLocation = new GLatLng(sGeobytesLatitude, sGeobytesLongitude);
			}
			map.setCenter(showCenterLocation, 12);
		}else{
			map.setCenter(bounds.getCenter());
		}
	
	
}

function deg2rad(deg)
  {
conv_factor = (2.0 * Math.PI)/360.0;
return(deg * conv_factor);
  }

