// gmap.js

var GMAP = GMAP || {};

var geocoder = null;
var markers = new Array();

GEvent.addDomListener(window, 'load', initialize);

function initialize() {
	if (GBrowserIsCompatible()) {
		try {
            geocoder = new GClientGeocoder();
	 		map.addControl(new GOverviewMapControl());
			
			GEvent.addDomListener(window, 'unload', GUnload);

		} catch (e) {
			window.setTimeout(initialize, 1000);
		}
		
	}	
}

function wheelZoom(e) { 
		
	// Verhindern, dass beim Zoomen der Map die Seite gescrollt wird
	// IE
	if(window.event) {  
		event.returnValue = false; 
	}  
	
	// DOM-Standard
	if(e.cancelable) { 
		e.preventDefault(); 
	}  

	// Event-Objekt für IE
	if (!e) { 
		e = window.event; 
	}  

	// Firefox
	if (e.detail) {  
		if (e.detail < 0) { 
			map.zoomIn(null, null, true); 
		} 
		else { 
			map.zoomOut( null, true); 
		} 
	} 
	// IE
	else if (e.wheelDelta) {  
		if (e.wheelDelta > 0) { 
			map.zoomIn(null, null, true); 
		} 
		else { 
			map.zoomOut( null, true); 
		} 
	} 
} 


function clearMarkers() {
	for (var i = 0; i < markers.length; i++) {
		marker = markers[i];
		map.removeOverlay(marker);
	}
	markers.splice(0,markers.length);
}

function toggleMarker(id, order, street, houseNumber, zipcode, city, country, title, image, show, pan) {
    if (!isMarker(id)) {
        addMarker(id, order, street, houseNumber, zipcode, city, country, title, image, show, pan);
        return;
    }
	if (show == "true" || show == true) {
		selectMarker(id, pan);
	} else {
		unselectMarker(id);
	}
}
function viewMarkers(view) {
	for (var i = 0; i < markers.length; i++) {
		var marker = markers[i];
		if (view) {
            selectMarker(marker.id, false);
        } else {
            unselectMarker(marker.id);
        }

	}
}
function addMarker(id, order, street, houseNumber, zipcode, city, country, title, image, show, pan) {
	//alert(order + "\n\r" + street + "  " + houseNumber + "\n\r" + zipcode + "  " + city + "\n\r" + country + "\n\r" + title + "\n\r" + image);
	
	if (!street.length > 0) {
		return null;
	}
	
	var address = street + " " + houseNumber + ",";
	if (!city.length > 0) {
		return null;
	}
	
	address += city + ",";
	address += country + ",";
	address += zipcode;
	var adressWithoutStreet=city + ",";
	adressWithoutStreet += country + ",";
	adressWithoutStreet += zipcode;

	geocoder.getLatLng(	address,
		function(point) {
			if (!point) {
				//alert(address +" nicht gefunden");
				//return null;
				geocoder.getLatLng( adressWithoutStreet,
					function(point) {
						//if (!point) {
						//	alert(adressWithoutStreet +" nicht gefunden"); 
						//}
						var marker = new GMarker(new GLatLng(point.lat(), point.lng()), {title:title,icon:getNumberIcon(order, show),draggable:false});
						marker.id = id;
						marker.order = order;
						marker.zipcode = zipcode;
						marker.city = city;
						marker.country = country;
						marker.title = title;
						marker.image = image;
						marker.point = point;

						function onClickHandler() {
							showInfo2(this);
						}
						
						GEvent.addListener(marker, "click", onClickHandler);
						markers[markers.length] = marker;
						map.addOverlay(marker);
						if ( pan ) {
							map.panTo(point);
						}
					}
				);
			} else {
				var marker = new GMarker(new GLatLng(point.lat(), point.lng()), {title:title,icon:getNumberIcon(order, show),draggable:false});
				marker.id = id;
				marker.order = order;
				marker.street = street;
				marker.houseNumber = houseNumber;
				marker.zipcode = zipcode;
				marker.city = city;
				marker.country = country;
				marker.title = title;
				marker.image = image;
				marker.point = point;

				function onClickHandler() {
					showInfo(this);
				}
				
				GEvent.addListener(marker, "click", onClickHandler);

				markers[markers.length] = marker;
				map.addOverlay(marker);
				if ( pan ) {
					map.panTo(point);
				}
			}
		}
	);
	
}

function showInfo(marker) {
	var content;
	content =  '<div style="text-align:center;overflow:hidden;width:340px;height:160px;padding:0px;zIndex:10000">';                                                                                         
	content += '    <div style="border:0px solid #e0e0e0;text-align:center;margin:auto">';                                    
	if (marker.title.length > 0) {
	content += '    	<span style="max-width:90%;margin:0px;font:bold 1.0em/1em arial,Times New Roman;">' + marker.title + '</span>';
	}                                   
	content += '    </div>';                                    
	content += '    <div style="overflow:hidden;height:106px;text-align:left;border:1px solid #e0e0e0;margin-top:4px;padding:4px;">';                                                                                 
	if ((marker.image.length > 0)) {
	content += '        <div style="overflow:hidden;float:left;max-width:120px;height:100%;border:0px solid red;margin-right:4px;vertical-align:middle">';                                                                                 
	content += '            <img id="image" style="max-width:60px;height:60px;margin:auto;border:1px solid #666;padding:2px;vertical-align:middle" src=" '+"/user"+ marker.image + '" alt="Image"/>';
	content += '        </div>';
	}                                                                                                                                                                     
	content += '        <div style="width:%;height:100%;margin:0px 0px 0px 0px;font:normal 1.1em/1.3em arial;border:0px solid #e0e0e0;">';                                                                                          
	content += '            <span style="">' + marker.street + ' ' + marker.houseNumber + '</span>';                                                                                       
	content += '            <br/>';
	content += '            <span style="">' + marker.zipcode + ' ' + marker.city + '</span>';                                                                                       
	content += '            <br/>';
	content += '            <span style="">' + marker.country + '</span>';                                                                                       
	content += '            <br/>';
	content += '            <br/>';
	content += '        </div>';                                                                                                                                                                     
	content += '    </div>';                                                                                                                                                                     
	content += '    <div style="clear:both;height:15px;vertical-align:middle;text-align:right;border:1px solid #e0e0e0;border-bottom:4px solid #e0e0e0;margin-top:6px;padding:0 4px;">';                                                                                                                                                                         
//	content += '    	<a style="float:left;margin-right:5px;" target="_blank" href="' + marker.url + '">Webseite</a>';
//	content += '    	<a style="float:right;margin-right:5px;" href="javascript:viewFacility(' + marker.id + ')">Details...</a>';    
	content += '    </div>';                                                                                                                                                                         
	content += '</div>';

	marker.openInfoWindowHtml(content);
}

function showInfo2(marker) {
	var content;
	content =  '<div style="text-align:center;overflow:hidden;width:340px;height:160px;padding:0px;zIndex:10000">';                                                                                         
	content += '    <div style="border:0px solid #e0e0e0;text-align:center;margin:auto">';                                    
	if (marker.title.length > 0) {
	content += '    	<span style="max-width:90%;margin:0px;font:bold 1.0em/1em arial,Times New Roman;">' + marker.title + '</span>';
	}                                   
	content += '    </div>';                                    
	content += '    <div style="overflow:hidden;height:106px;text-align:left;border:1px solid #e0e0e0;margin-top:4px;padding:4px;">';                                                                                 
	if ((marker.image.length > 0)) {
	content += '        <div style="overflow:hidden;float:left;max-width:120px;height:100%;border:0px solid red;margin-right:4px;vertical-align:middle">';                                                                                 
	content += '            <img id="image" style="max-width:60px;height:60px;margin:auto;border:1px solid #666;padding:2px;vertical-align:middle" src="' + marker.image + '" alt="Image"/>';
	content += '        </div>';
	}                                                                                                                                                                     
	content += '        <div style="width:%;height:100%;margin:0px 0px 0px 0px;font:normal 1.1em/1.3em arial;border:0px solid #e0e0e0;">';                                                                                          
	content += '            <span style="">' + marker.zipcode + ' ' + marker.city + '</span>';                                                                                       
	content += '            <br/>';
	content += '            <span style="">' + marker.country + '</span>';                                                                                       
	content += '            <br/>';
	content += '            <br/>';
	content += '        </div>';                                                                                                                                                                     
	content += '    </div>';                                                                                                                                                                     
	content += '    <div style="clear:both;height:15px;vertical-align:middle;text-align:right;border:1px solid #e0e0e0;border-bottom:4px solid #e0e0e0;margin-top:6px;padding:0 4px;">';                                                                                                                                                                         
//	content += '    	<a style="float:left;margin-right:5px;" target="_blank" href="' + marker.url + '">Webseite</a>';
//	content += '    	<a style="float:right;margin-right:5px;" href="javascript:viewFacility(' + marker.id + ')">Details...</a>';    
	content += '    </div>';                                                                                                                                                                         
	content += '</div>';

	marker.openInfoWindowHtml(content);
}

function viewFacility (id) {
	alert (" Hier soll dann die Detail-Ansicht kommen ! ");
}

function isMarker(id) {
	for (var i = 0; i < markers.length; i++) {
		var marker = markers[i];
		if ((marker) && (marker.id == id)) {
			return true;
		}
	}
	return false;
}

function selectMarker(id, pan) {
	for (var i = 0; i < markers.length; i++) {
		var marker = markers[i];
		if ((marker) && (marker.id == id)) {
            marker.setImage("/user/resources/marker/teardrop/marker" + id + "s.png");
			if ( pan ) {
				map.panTo(marker.point);
			}
			//return;
		}
	}	
}

function unselectMarker(id) {
	for (var i = 0; i < markers.length; i++) {
		var marker = markers[i];
		if ((marker) && (marker.id == id)) {
            marker.setImage("/user/resources/marker/teardrop/marker" + id + ".png");
			//return;
		}
	}	
}

function getNumberIcon(number, show) {
    var icon = new GIcon();
    if (show == "true" || show == true) {
        icon.image = "/user/resources/marker/teardrop/marker" + number + "s.png";
    } else {
        icon.image = "/user/resources/marker/teardrop/marker" + number + ".png";
    }
	icon.shadow = "/user/resources/marker/teardrop/shadow.png";

    icon.iconSize = new GSize(20.0, 34.0);
    icon.shadowSize = new GSize(38.0, 34.0);
    icon.iconAnchor = new GPoint(10.0, 17.0);
    icon.infoWindowAnchor = new GPoint(10.0, 17.0);
	return icon;
}

function getMarkerIcon(image, shadow) {
    var icon = new GIcon();

	if (!image) {
		icon.image = "/user/resources/marker/arrow.png";
		icon.shadow = "/user/resources/marker/arrow-shadow.png";
	} else {
		icon.image = "/user/resources/marker/" + image + ".png";
		if (shadow) {
			icon.shadow = "/user/resources/marker/" + shadow + ".png";
		}
	}
	
	icon.iconSize = new GSize(39, 34);
	icon.shadowSize = new GSize(39, 34);
	icon.iconAnchor = new GPoint(12, 12);
	icon.infoWindowAnchor = new GPoint(12, 12);
	return icon;
}

function setGMapMouseListener(elementID){
	GEvent.addDomListener(document.getElementById(elementID), "DOMMouseScroll", wheelZoom); // Firefox 
	GEvent.addDomListener(document.getElementById(elementID), "mousewheel", wheelZoom); // IE 
}

// Verzoegerte Ausfuehrung der Anfragen an google
var toggleOnLoadFunctions = new Array();

/**
 * Initializes the map.
 */
GMAP.init = function() {
    toggleOnLoadFunctions = new Array();
};

function addToggleOnLoad( list ){
	toggleOnLoadFunctions[toggleOnLoadFunctions.length] = list
}
function clearToggleOnLoad(){
    toggleOnLoadFunctions.splice(0,toggleOnLoadFunctions.length);
}

function executeToggleOnLoad(i){
	if (!i) {
		i = 0;
	}
	if (i >= toggleOnLoadFunctions.length)  {
//		clearToggleOnLoad();
		return;
	}
	toggleMarker(
			toggleOnLoadFunctions[i][0],
			toggleOnLoadFunctions[i][1],
			toggleOnLoadFunctions[i][2],
			toggleOnLoadFunctions[i][3],
			toggleOnLoadFunctions[i][4],
			toggleOnLoadFunctions[i][5],
			toggleOnLoadFunctions[i][6],
			toggleOnLoadFunctions[i][7],
			toggleOnLoadFunctions[i][8],
			toggleOnLoadFunctions[i][9], 
			false
		);
	setTimeout("executeToggleOnLoad("+(i+1)+")", 500);
}




