// -----------------------------------------------------------------------------------
//	GMap v 0.1 - 2008-06-13
//	by Sindre Wimberger - wimberger@echonet.at
//  dependencies - none
//
//  v 0.1 - bei intialisierung wird ein Container ID übergeben. Wenn diese im HTML gefunden wird, ladet die google maps in dieses Element.
//          wenn ein HTML Element mit der ID "googleplaner" gefunden wird, lädt ein Routenplaner in dieses.
//

var GMap = {
	keys: [
		['golfresort.at','ABQIAAAA1NPrAg5my5Yyo4v80SyAKBSMxePd3rRNtgrO7ovs6lsCX7K8zBSbeBzfvQyh2blrYKFzsvi1RlCP6w']
	],
	height: '400px', // höhe googlemaps container
	width: '100%', // breite googlemaps container
	points: function() { // Marker welche auf der map eingezeichnet werden
		this.markers = [
			[48.993824, 15.050575,'<strong>Golfresort Haugschlag</strong><br />Haugschlag 160<br />A-3874 Haugschlag<br /> Tel: +43/0/2865/8441-0',this.bueroicon]
		];
	},
	icons: function() {
		this.baseicon = new GIcon();
		this.baseicon.image = "/assets/content/icon-googlemaps.png";
		this.baseicon.printImage = "/assets/content/icon-googlemaps-print.gif";
		this.baseicon.mozPrintImage = "/assets/content/icon-googlemaps-print.gif";
	//	icon.shadow = "/grafik/content/icon-googlemaps-shadow.png";
		this.baseicon.iconSize = new GSize(16, 16);
	//	icon.shadowSize = new GSize(36, 36);
		this.baseicon.iconAnchor = new GPoint(8, 8);
		this.baseicon.infoWindowAnchor = new GPoint(8, 8);	
		this.bueroicon = new GIcon();
		this.bueroicon.image = "/assets/content/icon-resort.png";
		this.bueroicon.printImage = "/assets/content/icon-resort-print.gif";
		this.bueroicon.mozPrintImage = "/assets/content/icon-resort-print.gif";
	//	icon.shadow = "/grafik/content/icon-googlemaps-shadow.png";
		this.bueroicon.iconSize = new GSize(20, 20);
	//	icon.shadowSize = new GSize(36, 36);
		this.bueroicon.iconAnchor = new GPoint(8, 8);
		this.bueroicon.infoWindowAnchor = new GPoint(8, 8);	
	},
	center: [48.993824, 15.050575],
	zoomlevel: 13,
	init: false,
	dstat: false,
	initialize: function(container) {
		this.lang = document.getElementsByTagName('HTML')[0].getAttribute('lang');
		if(!document.getElementById(container)) { return false; }
		if(document.getElementById("googleplaner")) {
			this.dstat = true;
			this.dcontainer = document.getElementById("googleplaner");
		}
		GMapFunc = this;
		if (this.keys.length == 0 || window.location.host.indexOf("echonet.at") >= 0) { 
			// Wenn kein key angegeben oder echonet subdomain, wird jener für echonet.at verwendet
			this.key = 'ABQIAAAAvK3RzknTH9eYky0QOpdjChQSrip3x_DT7vffRqHjW0TBeSd_yxR0rnRoTg0yEQDCrE_6V5S4--iQiA';
		} else {
			for (var i = 0; i < this.keys.length; i++) {
				var key = this.keys[i];
				if (window.location.host.indexOf(key[0]) >= 0) { 
					// Wenn kein key angegeben oder echonet subdomain, wird jener für echonet.at verwendet
					this.key = key[1];
				}
			}			
		}
		this.container = document.getElementById(container);
		this.init = true;
		if($('googlemaploader')) {
			  this.create();
		} else {
			  this.load();
		}
	},
	load: function() { 
		var script = document.createElement("script");
		if(this.lang == 'de'){
			script.setAttribute("src", "http://maps.google.com/maps?file=api&v=2&key="+this.key+"&c&async=2&callback=createGoogleMaps&locale=de_AT&hl=de");
		} else if (this.lang == 'cz'){
			script.setAttribute("src", "http://maps.google.com/maps?file=api&v=2&key="+this.key+"&c&async=2&callback=createGoogleMaps&locale=cs_CZ&hl=cs");
		} else {
			script.setAttribute("src", "http://maps.google.com/maps?file=api&v=2&key="+this.key+"&c&async=2&callback=createGoogleMaps&locale=en_US&hl=en");
		}
		script.setAttribute("type", "text/javascript");
		script.setAttribute("id", "googlemaploader");
		document.getElementsByTagName("body").item(0).appendChild(script);
	},
	create: function() { 
		if (!GBrowserIsCompatible()) { return false; }
	//	window.onunload=GMap2.Unload; 
		this.showMap(this.center[0], this.center[1]);
	//    setDirections("Schottenfeldgasse 24, 1070 Wien", "Bräunerstrasse 11a, 1010 Wien");
	},
	showMap: function(x,y) {
		this.container.style.height = this.height;
		this.container.style.width = this.width;
		this.container.style.border = '1px solid #000';
		this.map = new GMap2(this.container);
		this.icons();
		this.points();
		if(this.dstat) {
			this.dclass = GDirection.initialize(this);
			this.gdir = new GDirections(this.map, document.getElementById("GDirection"));
			GEvent.addListener(this.gdir, "load", this.dclass.onGDirectionsLoad);
			GEvent.addListener(this.gdir, "error", this.dclass.GDirectionshandleErrors);
		}
		this.map.addControl(new GLargeMapControl());
		this.map.addControl(new GOverviewMapControl());
//		this.map.addControl(new GScaleControl());
		this.map.setCenter(new GLatLng(x,y), this.zoomlevel);
		this.setupUserMarkers();
//		this.map.addOverlay(this.createMarker(new GLatLng(x,y)));
	},
   	setupUserMarkers: function() {
		var batch = [];
		this.mgr = new GMarkerManager(this.map);
			for (var i = 0; i < this.markers.length; i++) {
				var marker = this.markers[i];
				batch.push(this.createMarker(new GLatLng(marker[0],marker[1]),marker[2],marker[3]));
			}
		this.mgr.addMarkers(batch,3);
		this.mgr.refresh();
    },
	createMarker: function(point,html,icon) {
		if(icon == null) {
			var icon = this.baseicon;	
		}
		var marker = new GMarker(point, icon);
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(html);
		});
		return marker;
	},
	unload: function() {
		if(this.init) { 
			GUnload();
		}
	}
};
function createGoogleMaps() { 
	GMapFunc.create();
}
var GDirection = {
	init: false,
	to: 'Haugschlag 160, A-3874 Haugschlag',
	initialize: function(func) {
		this.lang = document.getElementsByTagName('HTML')[0].getAttribute('lang');		
		this.ParentClass = func;
		if(!$('GDinfo')) {
			if(this.lang == 'de') {
				this.ParentClass.dcontainer.innerHTML = '<div id="GDinfo"><form onsubmit="GDirection.setDirections(this.standpunkt.value); return false"><fieldset><h3>Anfahrtsplan</h3><p>Geben Sie Ihren momentanen Standort ein und erstellen Sie Ihren persönlichen Anfahrtsplan.</p><input id="standpunkt" name="standpunkt" /><button type="submit">Anfahrtsplan erstellen</button></fieldset></form></div><div id="GDirection" class="c"></div>';
			} else if (this.lang == 'cz') {
				this.ParentClass.dcontainer.innerHTML = '<div id="GDinfo"><form onsubmit="GDirection.setDirections(this.standpunkt.value); return false"><fieldset><h3>Pokyny</h3><p>Prosím zašlete vaší aktuální adresu za účelem získání pokynů do našeho hotelu</p><input id="standpunkt" name="standpunkt" /><button type="submit">Žádost pokynů</button></fieldset></form></div><div id="GDirection" class="c"></div>';
			} else {
				this.ParentClass.dcontainer.innerHTML = '<div id="GDinfo"><form onsubmit="GDirection.setDirections(this.standpunkt.value); return false"><fieldset><h3>Directions</h3><p>Please enter your current position/adress in order to get directions to our hotel</p><input id="standpunkt" name="standpunkt" /><button type="submit">Request directions</button></fieldset></form></div><div id="GDirection" class="c"></div>';
			}
		}
		return this;
	},
	setDirections: function(fromAddress) {
		if(this.lang == 'de') {
		  this.ParentClass.gdir.load("from: " + fromAddress + " to: " + this.to,
					{ "locale": "de_AT" });
		} else if (this.lang == 'cz') {
		  this.ParentClass.gdir.load("from: " + fromAddress + " to: " + this.to,
					{ "locale": "cs_CZ" });
		} else {
		  this.ParentClass.gdir.load("from: " + fromAddress + " to: " + this.to,
					{ "locale": "en_US" });
		}
	},
	GDirectionshandleErrors: function (){
		if(GDirection.lang == 'de') {
		   if (GDirection.ParentClass.gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
			 GDirection.error("Die angegebene Adresse konnte nicht gefunden werden. Entweder sie ist relativ neu, oder fehlerhaft.\nFehler Code: " + GDirection.ParentClass.gdir.getStatus().code);
		   else if (GDirection.ParentClass.gdir.getStatus().code == G_GEO_SERVER_ERROR)
			 GDirection.error("Der Anfahrtsplan konnte nicht erstellt werden.\n Fehler Code: " + GDirection.ParentClass.gdir.getStatus().code);
		   
		   else if (GDirection.ParentClass.gdir.getStatus().code == G_GEO_MISSING_QUERY)
			 GDirection.error("The HTTP q parameter was either missing or had no value. For geocoder requests, GDirection means that an empty address was specified as input. For directions requests, GDirection means that no query was specified in the input.\n Fehler Code: " + GDirection.ParentClass.gdir.getStatus().code);
			 
		   else if (GDirection.ParentClass.gdir.getStatus().code == G_GEO_BAD_KEY)
			 GDirection.error("Der Schlüssel passt nicht zur angegeben Domain.\n Error code: " + GDirection.ParentClass.gdir.getStatus().code);
		
		   else if (GDirection.ParentClass.gdir.getStatus().code == G_GEO_BAD_REQUEST)
			 GDirection.error("Der Anfahrtsplan konnte nicht erstellt werden.\n Fehler Code: " + GDirection.ParentClass.gdir.getStatus().code);
			
		   else GDirection.error("Fehler. Bitte überprüfen Sie Ihre Angaben.");
		} else {
			 if (GDirection.ParentClass.gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
			 GDirection.error("No corresponding geographic location could be found for one of the specified addresses. GDirection may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + GDirection.ParentClass.gdir.getStatus().code);
		   else if (GDirection.ParentClass.gdir.getStatus().code == G_GEO_SERVER_ERROR)
			 GDirection.error("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + GDirection.ParentClass.gdir.getStatus().code);
		   
		   else if (GDirection.ParentClass.gdir.getStatus().code == G_GEO_MISSING_QUERY)
			 GDirection.error("The HTTP q parameter was either missing or had no value. For geocoder requests, GDirection means that an empty address was specified as input. For directions requests, GDirection means that no query was specified in the input.\n Error code: " + GDirection.ParentClass.gdir.getStatus().code);
			 
		   else if (GDirection.ParentClass.gdir.getStatus().code == G_GEO_BAD_KEY)
			 GDirection.error("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + GDirection.ParentClass.gdir.getStatus().code);
	
		   else if (GDirection.ParentClass.gdir.getStatus().code == G_GEO_BAD_REQUEST)
			 GDirection.error("A directions request could not be successfully parsed.\n Error code: " + GDirection.ParentClass.gdir.getStatus().code);
			
		   else GDirection.error("An unknown error occurred.");
		}
	},
	onGDirectionsLoad: function (){ 
		document.getElementById('GDirection').className = '';
		document.getElementById('GDirection').innerHTML = '';
  // Use this function to access information about the latest load()
  // results.

  // e.g.
  // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
  // and yada yada yada...
	},
	error: function (msg) {
		document.getElementById('GDirection').className = 'error-msg';
		document.getElementById('GDirection').innerHTML = msg;
	}
}


GMap.initialize('googlemap');