var map = null;
var bounds = null;
var route = null;
var cpicon = null;

var counter = 0;
var points = [];
var markers = [];
var marker_objects = [];
var marker_html = [];

$(document).ready(function() {
	if(GBrowserIsCompatible()){
		mapsetup();
	} else {
		alert("Sorry, the Google Maps API is not compatible with this browser.");
	}
});

function mapsetup(){
	cpicon = new GIcon();
	cpicon.image = "fileadmin/mapsetup/img/carpark.png";
	cpicon.iconSize = new GSize(20,20);
	cpicon.iconAnchor = new GPoint(10,10);
	cpicon.infoWindowAnchor = new GPoint(20,0);
	
	map = new GMap2(document.getElementById('map'));
	bounds = new GLatLngBounds(new GLatLng(51.2207816, 6.7759946), new GLatLng(51.2264994, 6.7841984));
	route = new GDirections(map, document.getElementById('dir'));
	
	map.setCenter(new GLatLng(51.223641, 6.780097), 16, G_NORMAL_MAP);
	map.setZoom(map.getBoundsZoomLevel(bounds));
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.addControl(new GScaleControl());
	
	$.getJSON('fileadmin/mapsetup/php/getmarkers.php', function(data) {
		if(isArray(data)){
			for(i=0; i<data.length; i++){
				if(isObject(data[i])){
					createMarker(data[i]);
				}
			}
		}
    });	
}

function createMarker(markerObj) {
	var a = markerObj;
	
	var point = new GLatLng(a.lat,a.lon);
	points[counter] = point;
	
	var params = {
		title: (a.type === 'carpark') ? 'Parkhaus "'+a.name+'", freie Plätze: '+a.counter : a.name,
		icon: (a.type === 'carpark') ? cpicon : ''
	};
	
	var marker = new GMarker(point,params);
	markers[counter] = marker;
	marker_objects[counter] = a;
	
	if(a.type === 'home'){
		var description = '<div class="marker"><p><strong>'+a.name+'</strong></p><p>'+a.address+'</p><p><a href="#" onclick="setRouteData('+counter+'); return false;">Route hierhin berechnen</a></p></div>';
		$('#endinput').val(a.address);
		showInfoWindow(0, description);
	} else if(a.type === 'carpark'){
		var description = '<div class="marker"><p><strong>Parkhaus "'+a.name+'"</strong></p><p>Einfahrt: '+a.address+'</p><p>Freie Pl&auml;tze: '+a.counter+'</p><p><a href="#" onclick="setRouteData('+counter+'); return false;">Route hierhin berechnen</a></p></div>';
	}
	
	marker_html[counter] = description;
					
	map.addOverlay(marker);
	GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(description); });
	
	counter++;
}

function showInfoWindow(i, description) {
	markers[i].openInfoWindowHtml(description);
}

function setRouteData(i){
	var a = marker_objects[i];
	$('#startinput').val('');
	$('#endinput').val(a.address);
	map.closeInfoWindow();
	$('#form').show(500, function() {
    	$('#startinput').animate({backgroundColor:'#FBDEBE'}).animate({backgroundColor:'#FFFFFF'}).val('').focus();
	});
}

function router(from, to){
	if(from===''){
		$('#error').html('Bitte geben Sie eine Startadresse ein.').show(500, function(){
			$('#startinput').animate({backgroundColor:'#FBDEBE'}).animate({backgroundColor:'#FFFFFF'}).val('').focus();
			setTimeout(function(){$('#error').hide(500);},3000);
		});
	} else {
		$('#loading').show();
		map.closeInfoWindow();
		route.load("from: " + from + " to: " + to, { "locale": "de" });
		setTimeout(function(){
			if(route.getStatus().code === 200){
				$('#loading').hide(0, function(){
					$('#form').hide(500, function(){
						$('#dir').show(500);
					});
				});
			} else {
				$('#loading').hide(0, function(){
					$('#form').show(500, function(){
						$('#error').html('Die angegebene Adresse "'+from+'" konnte nicht gefunden werden.<br />Bitte überprüfen Sie die Eingabe und versuchen es noch einmal.').show(500, function(){
							$('#startinput').animate({backgroundColor:'#FBDEBE'}).animate({backgroundColor:'#FFFFFF'}).val('').focus();
							setTimeout(function(){$('#error').hide(500);},5000);
						});
					});
				});
			}
		},2000);
	}
}
function showForm(){
	route.clear();
	map.setCenter(new GLatLng(51.223641, 6.780097), 16, G_NORMAL_MAP);
	map.setZoom(map.getBoundsZoomLevel(bounds));
	showInfoWindow(0, marker_html[0]);
	$('#dir').hide(500, function(){
		$('#form').show(500, function(){
			$('#startinput').animate({backgroundColor:'#FBDEBE'}).animate({backgroundColor:'#FFFFFF'}).val('').focus();
		});
	});
	
}

function isArray(a){
	return isObject(a) && a.constructor == Array;
}

function isObject(a){
	return (a && typeof a == 'object') || isFunction(a);
}
