// Configuration
// -------------
// debug:	"boolean", default:"0"
// id: "string", default:"map"
// height: "string", default:"350px"
// zoom: "integer",	default:"13"
// coordinate: "number x,	number y"
// address:	"string"
// title:	"html"
// dragging: "enabled|disabled", default:"enabled"
// doubleclick:	"enabled|disabled",	default:"disabled"
// scrollwheel:	"enabled|disabled",	default:"disabled"
// scale:	"enabled|disabled",	default:"disabled"
// overviewmap:	"enabled|disabled",	default:"disabled"
// keyboard:	"enabled|disabled",	default:"disabled"
// routing:	"enabled|disabled",	default:"disabled"
// control:	"none|small|large|zoom", default:"none"
// maps: "normal,satellite,hybrid",	default:"normal,satellite,hybrid"


function ggMaps_Engine() {
	this.is_loaded = false;
	this.map = null;
	this.directions = null;
};

ggMaps_Engine.prototype	=	{

	init:function(settings)	{
		this.settings	=	settings;
		this.config("debug", parseInt(this.settings["debug"]));
		if (!this.settings["id"])
			this.config("id",	"map");
		if (!this.settings["height"])
			this.config("height",	"350px");
		if (!this.settings["zoom"] &&	!parseInt(this.settings["zoom"]))
			this.config("zoom",	"13");
		this.config("zoom",	parseInt(this.settings["zoom"]));
		if (this.settings["coordinate"])
			this.config("coordinate",	this.settings["coordinate"].split(",", 2));
		if (this.settings["dragging"]	!= "disable")
			this.config("dragging",	"enable");
		if (this.settings["doubleclick"] !=	"enable")
			this.config("doubleclick", "disable");
		if (this.settings["scrollwheel"] !=	"enable")
			this.config("scrollwheel", "disable");
		if (this.settings["scale"] !=	"enable")
			this.config("scale", "disable");
		if (this.settings["overviewmap"] !=	"enable")
			this.config("overviewmap", "disable");
		if (this.settings["keyboard"] !=	"enable")
			this.config("keyboard", "disable");
		if (this.settings["routing"] !=	"enable")
			this.config("routing", "disable");
		if (!this.settings["maps"])
			this.config("maps",	"normal,satellite,hybrid");
		this.config("maps",	this.settings["maps"].split(",", 3));
		Event_Engine.prototype.add(window,	"load",	ggMaps_Engine.prototype.load);
		Event_Engine.prototype.add(window,	"unload",	ggMaps_Engine.prototype.unload);
	},

	load:function()	{
		if (!ggMaps.is_loaded) {
			ggMaps.is_loaded = true;
			Event_Engine.prototype.remove(window, "load",	ggMaps_Engine.prototype.load);
			ggMaps.run();
		}
	},

	unload:function()	{
		if (ggMaps.is_loaded)	{
			try	{
				GUnload();
			}	catch	(exception)	{
			}
			Event_Engine.prototype.remove(window, "unload",	ggMaps_Engine.prototype.unload);
		}
	},

	config:function(property,	value) {
		this.settings[property]	=	value;
	},

	run:function()	{
		if (!document.getElementById)	{
			this.error("ERROR	(1): No	element	with id	\""+this.settings["id"]+"\"	found	in document.");
			return;
		}
		if (!document.getElementById(this.settings["id"])) {
			this.error("ERROR	(1): No	element	with id	\""+this.settings["id"]+"\"	found	in document.");
			return;
		}
		this.config("error", document.getElementById(this.settings["id"]).innerHTML);
		try	{
			if (GBrowserIsCompatible())	{
				with (this)	{
					if (settings["coordinate"])
						show_map(new GLatLng(settings["coordinate"][0],	settings["coordinate"][1]));
					else if	(settings["address"])
						show_address(settings["address"]);
					else
						error("ERROR (3):	No coordinates or	address	configured.");
				}
			}	else {
				this.error("ERROR	(2): Used	browser	is not compatible.");
			}
		}	catch(exception) {
			this.error("ERROR	(0): Could not load	script from	Google.");
		}
	},

	error:function(str)	{
		this.unload();
		if (document.getElementById(this.settings["id"]))	{
			document.getElementById(this.settings["id"]).innerHTML = this.settings["error"];
		}
		if (this.settings["debug"])
			alert(str);
	},

	set_map_types:function() {
		var	maps = {normal:0,satellite:0,hybrid:0};
		var	const_maps = {normal:G_NORMAL_MAP,satellite:G_SATELLITE_MAP,hybrid:G_HYBRID_MAP};
		for(var	i=0;i<this.settings["maps"].length;i++)	{
			maps[this.settings["maps"][i]] = 1;
		}
		for	(var type	in maps) {
			if (!maps[type])
				this.map.removeMapType(const_maps[type]);
		}
		this.map.setMapType(const_maps[this.settings["maps"][0]]);
	},

	show_address:function(address) {
		var	geocoder = new GClientGeocoder();
		if (geocoder)	{
			geocoder.getLatLng(
				address, 
				function(point)	{
					ggMaps.show_map(point);
				}
			);
		}	else {
			this.error("ERROR	(4): Could not initialize	Geocoder.");
		}
	},

	show_map:function(point, title)	{
		if (!point)	{
			this.error("ERROR	(5): No	valid	coordinates	or address found.");
			return;
		}
		if (this.settings["routing"] == "enable" && !this.settings["address"]) {
			this.error("ERROR	(6): No	address for routing found.");
			return;
		}
		this.point = point;
		if (!this.map) {
			document.getElementById(this.settings["id"]).style.height	=	this.settings["height"];
			this.map = new GMap2(document.getElementById(this.settings["id"]));
			with(this) {
				switch (settings["control"]) {
					case "small":
						map.addControl(new GSmallMapControl());
						break;
					case "large":
						map.addControl(new GLargeMapControl());
						break;
					case "zoom":
						map.addControl(new GSmallZoomControl());
						break;
				}
				if (settings["scale"]	== "enable")
					map.addControl(new GScaleControl());
				if (this.settings["maps"].length > 1)
					map.addControl(new GMapTypeControl());
				if (settings["overviewmap"] == "enable")
					map.addControl(new GOverviewMapControl());
				if (settings["keyboard"] == "enable")
					new GKeyboardHandler(map);
				if (settings["dragging"] == "enable")
					//map.enableDragging();
					void(0);
				else
					map.disableDragging();
				if (settings["doubleclick"] == "enable")
					map.enableDoubleClickZoom();
				else
					map.disableDoubleClickZoom();
				if (settings["scrollwheel"] == "enable") {
					GEvent.addListener(map, "mouseover", function() {
						Event_Engine.prototype.add(window.document, "DOMMouseScroll", Event_Engine.prototype.disable);
						Event_Engine.prototype.add(window.document, "mousewheel", Event_Engine.prototype.disable);
					});
					GEvent.addListener(map, "mouseout", function() {
						Event_Engine.prototype.remove(window.document, "DOMMouseScroll", Event_Engine.prototype.disable);
						Event_Engine.prototype.remove(window.document, "mousewheel", Event_Engine.prototype.disable);
					});
					map.enableScrollWheelZoom();
				} else
					map.disableScrollWheelZoom();
				map.enableContinuousZoom();
			}
		}
		with (this)	{
			map.setCenter(point, settings["zoom"]);
			set_map_types();
			var	marker = new GMarker(point);
			map.addOverlay(marker);
			if (settings["title"]	!= "") {
				marker.openInfoWindowHtml(settings["title"]);
				GEvent.addListener(marker, "click",	function() {
					marker.openInfoWindowHtml(settings["title"]);
				});
			}
			if (settings["routing"] == "enable") {
				directions = new ggDirections_Engine();
				directions.init();
			}
		}
	}

};


function ggDirections_Engine() {
	this.is_loaded = false;
	this.gdir = null;
	this.status = null;
	this.errors = null;
};

ggDirections_Engine.prototype	=	{

	init:function() {
		GDownloadUrl("routenplanung.php", function(data, responseCode) { 
			if (responseCode == 200) {
				var node = document.createElement("div");
				node.innerHTML = data;
				var settings = node.getElementsByTagName("div");
				for (var i=0;i<settings.length;i++) {
				  switch(settings[i].getAttribute("id")) {
				  	case "status":
				  		ggMaps.directions.status = String(settings[i].innerHTML);
				  		break;
				  	case "directions":
				  		eval("ggMaps.directions.errors = "+String(settings[i].innerHTML).replace(/\\="">/g, "\\>")+";");
				  		break;
				  }
				  settings[i].innerHTML = "";
				}
				document.getElementById(ggMaps.settings["id"]).parentNode.appendChild(node);
			}
		});
	},

	set:function(locale) {
		with(this) {
			document.getElementById("calculate").disabled = true;
			is_loaded = false;
			ggMaps.map.clearOverlays();
			document.getElementById("directions").innerHTML = "";
			if (!gdir) {
				gdir =	new	GDirections(ggMaps.map,	document.getElementById("directions"));
				GEvent.addListener(gdir, "load", function() { ggMaps.directions.load(); });
				GEvent.addListener(gdir, "error",	function() { ggMaps.directions.error(); });
			}
			gdir.clear();
			show_loading(this.status);
			gdir.load("from: "+document.getElementById("directions_from").value+" to:	"	+	ggMaps.settings["address"],	{	"locale":	locale });
		}
	},
	
	show_loading:function(text)	{
		if (!this.is_loaded) {
			document.getElementById("status").innerHTML	= document.getElementById("status").innerHTML+text+".";
			window.setTimeout("ggMaps.directions.show_loading('');", 250);
		} else {
			document.getElementById("status").innerHTML = "";
		}
	},
	
	load:function() {
 		this.is_loaded = true;
		document.getElementById("calculate").disabled = false;
	},

	error:function() {
		this.load();
		switch(this.gdir.getStatus().code) {
			case G_GEO_UNKNOWN_ADDRESS:
				document.getElementById("directions").innerHTML = this.errors["G_GEO_UNKNOWN_ADDRESS"];
				break;
			default:
				document.getElementById("directions").innerHTML = this.errors["default"];
		}
	}

}

var	ggMaps = new ggMaps_Engine();
