/**
 * Datei läd Minikarte für frontend CampingDetails.
 * @version $Id$
 * @author s.marletta 
 * $LastChangedDate$
 */
/*window.onerror = errorHandling;
		
function errorHandling(msg, file, line) {
	alert(
		"Fehler in " + file + " Zeile " + 
		line + "\nNachricht: " + msg
	);
}*/

var miniMap = null;
var contentBackup = null;

/**
 * Initialisiert GoogleMap
 */
function setupMap() {
	if(!GBrowserIsCompatible()) {
		alert("Google MAP nicht unterstützt.");
		return;
	}

	miniMap = new google.maps.Map2(document.getElementById("map_content"));

	point = new GLatLng(lat, lng, true);
	miniMap.setCenter(point, 10);
			
    var marker = new GMarker(point);
   	miniMap.addOverlay(marker);	
   	
   	//verhindert einen Überlauf des Copyright-Textes
	miniMap.getContainer().style.overflow = "hidden"; 
}

function initGMaps() {	
	window.unload = GUnload;
	
	window.addOnLoadListener(function() {
		var element = document.getElementById("map_content");
		if(element != null) {
			element.innerHTML = "<img src='http://www.bvcd.de/frontend/include/" +
					"javascript/ajax.gif' />";
		}
	});
}

function startRouting() {
	switchRouting("block", "block");
}

function stopRouting() {
	switchRouting("none", "block");
}

var routing;
var service;

function switchRouting(routingVisible, campDetailsVisible) {
	routing = document.getElementById("routingContainer");
	service = document.getElementById("campDetails");

	if(routing != null && service != null) {
		if(routingVisible == "block") {
			routing.style.zIndex = "100";
			routing.style.position = "absolute";
			routing.style.marginTop = "32px";
			routing.style.marginLeft = "16px";
			
			routing.style.width = "540px";
			routing.style.height = "530px";
			routing.style.border = "1px solid #A0A0A4";
			routing.style.backgroundColor = "#FFFFFF";
			
			Effect.Fade(service,{duration: 0.5, from: 1.0, to:0.2});
			Effect.Appear(routing,{duration: 0.5, from: 0.0, to:1.0});
		}else {
			Effect.Appear(service,{duration: 0.5, from: 0.2, to:1.0});
			Effect.Fade(routing,{duration: 0.5, from: 1.0, to:0.0});
		}
	}else {
		alert("DIV COntainer sind nicht bereit.");
	}
	
	if(routingVisible != "none") {
		loadMap();
	}
}

var map = null;
var overviewMap = null;
var gdir = null;
var marker = null;

function loadMap() {
	if (GBrowserIsCompatible()) {
		map = new google.maps.Map2(
			document.getElementById("mapRoute"),
			{"size": new GSize(520, 460)}
		); 
		
		point = new GLatLng(lat, lng, true);
		
		map.setCenter(point, 10);
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		
		var marker = new GMarker(point);
		marker.value = 1;
		
		map.addOverlay(marker);
		
		GEvent.addListener(map, "addoverlay", function(overlay) {
			showTab("mapRoute");
		});
		document.getElementById("directions").innerHTML = "";
		
		overviewMap = new google.maps.Map2(
			document.getElementById("overviewMap"),
			{"size": new GSize(520, 380)}
		);
		
		overviewMap.setCenter(point, 9);
		overviewMap.addOverlay(marker);
		overviewMap.setMapType(G_HYBRID_MAP);
	}
	
	//verhindert einen Überlauf des Copyright-Textes
	map.getContainer().style.overflow = "hidden";
}  
	
function setDirections() {
	document.getElementById("loadingInfo").style.display = "block";
	
	if(gdir != null)
		gdir.clear();
	gdir = new GDirections(map);
	GEvent.addListener(gdir, "error", function() {
		if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS) {
	        alert("Ihre Startadresse konnte nicht gefunden werden." +
	        		"\nFehlercode: " + 	gdir.getStatus().code
	        );
		}else if (gdir.getStatus().code == G_GEO_SERVER_ERROR) {
	        alert("Serverfehler, bitt versuchen Sie es später noch " +
	        		"einmal.\nFehlercode: " + gdir.getStatus().code
	        );
		}else if (gdir.getStatus().code == G_GEO_MISSING_QUERY) {
	        alert("Die Suchanfrage ist nicht vollständig, bitte wenden " +
	        		"Sie sich an den Support.\nFehlercode: " + 
	        		gdir.getStatus().code
	        );
		}else if (gdir.getStatus().code == G_GEO_BAD_KEY) {
	        alert("Ungültiger key, bitte wenden Sie sich an den Support." +
	        		"\nFehlercode: " + gdir.getStatus().code
	        );
		}else if (gdir.getStatus().code == G_GEO_BAD_REQUEST) {
	        alert("Ihre Anfrage ist ungültig.\nFehlercode: " + gdir.getStatus().code);
		}else {
			alert("An unknown error occurred.\nFehlercode: " + gdir.getStatus().code);
		}
	});
	
	var fromAddress = document.getElementById('routingStreet').value;
	fromAddress += ", " + document.getElementById('routingPostalCode').value;
	fromAddress += ", " + document.getElementById('routingCity').value;

	gdir.load("from: " + fromAddress + " to: " + lat + "," + lng, {"getSteps": true, "locale": locale});
	GEvent.addListener(gdir, "addoverlay", function() {
		routeDescription = "";
		
		for(var i = 0; i < gdir.getNumRoutes(); i++) {
			var route = gdir.getRoute(i);
			routeDescription += "<div class='headlinebox' style='padding: 6px;'>" +
				langTexts.summary + ": " + 
				route.getSummaryHtml() + "</div>";
			
			routeDescription += "<table width='100%' cellpadding='2' cellspacing='0'>"; 
			routeDescription += "	<tr>";
			routeDescription += "		<th class='headlinebox'>";
			routeDescription += langTexts.description;	
			routeDescription += "		</th>";
			routeDescription += "		<th class='headlinebox'>";
			routeDescription += langTexts.duration;	
			routeDescription += "		</th>";
			routeDescription += "		<th class='headlinebox'>";
			routeDescription += langTexts.distance;	
			routeDescription += "		</th>";
			routeDescription += "	</tr>";
			
			for(var j = 0; j < route.getNumSteps(); j++) {
				var step = route.getStep(j);
			
				routeDescription += "	<tr class='wayPoint' onclick='clickWaypoint(" + i + ", " + j + ");'>";
				routeDescription += "		<td class='camplistetext'>";
				routeDescription += step.getDescriptionHtml();	
				routeDescription += "		</td>";	
				routeDescription += "		<td class='camplistetext'>";
				routeDescription += step.getDuration().html;	
				routeDescription += "		</td>";	
				routeDescription += "		<td class='camplistetext'>";
				routeDescription += step.getDistance().html;	
				routeDescription += "		</td>";	
				routeDescription += "	</tr>";
			}
			
			routeDescription += "	<tr>";
			routeDescription += "		<td class='camplistetext' " +
				"style='text-align: right;'><strong>" + langTexts.total + ":</strong></td>";
			routeDescription += "		<td class='camplistetext'>";
			routeDescription += route.getDuration().html;
			routeDescription += "		</td>";
			routeDescription += "		<td class='camplistetext'>";
			routeDescription += route.getDistance().html;
			routeDescription += "		</td>";
			routeDescription += "	</tr>";
			routeDescription += "</table>";
		}		
		
		routeDescription += "<br>";
		routeDescription += "<br>";
		routeDescription += gdir.getCopyrightsHtml();
		document.getElementById("directions").innerHTML = routeDescription;
		document.getElementById("loadingInfo").style.display = "none";
	})
}

function clickWaypoint(routeIndex, stepIndex) {
	if(marker != null)
		map.removeOverlay(marker);
		
	if(gdir != null) {
		var route = gdir.getRoute(routeIndex);
		var step = route.getStep(stepIndex); 
		var point = step.getLatLng();
		marker = new GMarker(point);
		GEvent.addListener(marker, "click", function() {
			marker.showMapBlowup();
		});
		
		map.addOverlay(marker);
	}else {
		alert("No Directions available.");
	}
}

/**
 * Zeigt ein Tab an.
 */
function showTab(tabId) {
	var tabs = new Array("searchFields", "mapRoute", "directions");
	for(var i = 0; i < tabs.length; i++) {
		var element = document.getElementById(tabs[i]);
		var tabButton = document.getElementById("tab_" + tabs[i]);
		if(tabs[i] == tabId) {
			element.style.display = "block";
			tabButton.className = "active";
		}else {
			element.style.display = "none";
			tabButton.className = "tabButton";
		}
	}
}
