if (GBrowserIsCompatible()) {
	var map = new GMap2(document.getElementById("map"));				
	
	var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,30));
	var bottomRight = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,20));
	map.addControl(new GMapTypeControl(), bottomRight);
	map.addControl(new GLargeMapControl(), topRight);
	map.addMapType(G_PHYSICAL_MAP);
	
	myPoint = new GLatLng(47.435,13.9);
	map.setCenter(myPoint, 11, G_PHYSICAL_MAP);
	
	icons = new Array('ausflugsziele','bank','bauernhof','bergbahnen','bergfuehrer','ferienwohnung','gastronomie','hotel5','hotel','huetten','nightlife','pension','schutzhuette','shopping','skihuetten','verleih');
	
	var icon = new Array();
	
	for(t=0;t<icons.length;t++)
	{
		v = icons[t];
		
		icon[v] = new GIcon();
		icon[v].iconSize = new GSize(25, 26);
		icon[v].iconAnchor = new GPoint(8, 8);
		icon[v].image = "/static/img/map/icon_" + v + ".png";	
		icon[v].infoWindowAnchor = new GPoint(25, 0);
	}
	
	var startPoint = new Array();
	var endPoint = new Array();
	var startPointType = null;
	var endPointType = null;
	var startPointText = null;
	var endPointText = null;
	
	var allPois = new Array();
	
	var poiMarkers = new Object();
	var lastKey = "";
	var lastIcon = "";
	
	clusterMarkers = new Array();
			
	var i18n = new Array();
	i18n['de'] = new Array();
	i18n['en'] = new Array();
	
	i18n['de']['start'] = "als Start";
	i18n['de']['ziel'] = "als Ziel";
	i18n['de']['routenplaner'] = "Routenplaner";
	i18n['de']['details'] = "Details";
	i18n['de']['error1'] = "Bitte wählen Sie erst einen Start und Endpunkt aus!";
	i18n['de']['error2'] = "Einer der angegebenen Orte konnte nicht gefunden werden, bitte vergewissern Sie sich das die Adresse richtig geschrieben ist.";
	i18n['de']['error3'] = "Bitte geben Sie eine gültige Adresse ein";
	i18n['de']['error4'] = "Es trat ein unbekannter Fehler bei der internen Kommunikation auf, sollte dieser Fehler wiederholt auftreten wenden Sie sich bitte an info@schladming-dachstein.at.";
	
	i18n['en']['start'] = "as start";
	i18n['en']['ziel'] = "as destination";
	i18n['en']['routenplaner'] = "routeplanner";
	i18n['en']['details'] = "details";
	i18n['en']['error1'] = "Please select one start and destination point.";
	i18n['en']['error2'] = "One of the entered addresses cannot be found, please be sure that the address is valid.";
	i18n['en']['error3'] = "Please enter a valid address.";
	i18n['en']['error4'] = "An unknown error occured. If the error appears several times, pleate contact info@schladming-dachstein.com.";
	
	gdir = new GDirections(map, document.getElementById("mapRoute"));
	GEvent.addListener(gdir, "load", onGDirectionsLoad);
	GEvent.addListener(gdir, "error", handleErrors);
}


function setDirections()  {
 	if(startPointType != null && endPointType != null) {
		if(startPointType == "text") {
			from = startPointText;
		}
		else {
			from = startPoint['lat'] + "," + startPoint['lng'];
		}
		
		if(endPointType == "text") {
			to = endPointText;
		}
		else {
			to = endPoint['lat'] + "," + endPoint['lng'];
		}
		
		gdir.load("from: " + from + " to: " + to ,{ "locale": lang });
	}
	else {
		alert(i18n[lang]['error1']);
	}
}

function handleErrors(){
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS) {
		alert(i18n[lang]['error2']);
	}
	
	else if (gdir.getStatus().code == G_GEO_MISSING_QUERY) {
		alert(i18n[lang]['error3']);
	}
	
	else {
		alert(i18n[lang]['error4']);
	}	
}

function onGDirectionsLoad() { 
	$('categoriesBox').style.display = 'none';
	$('showCategoriesContents').style.display = 'block';
	$('mapRoute').style.display = 'block';
}

function showInputBlock (prefix) {
	$(prefix + 'InputBlock').style.display = 'block';
	$(prefix + 'AdressBlock').style.display = 'none';
}

function setAsStartPoint (lat,lng,text) {
	startPointType = 'coordinates';
	
	startPoint['lat'] = lat;
	startPoint['lng'] = lng;
	$('startAdressText').innerHTML = text;
	$('startInputBlock').style.display = 'none';
	$('startAdressBlock').style.display = 'block';
}

function setAsEndPoint (lat,lng,text) {
	endPointType = 'coordinates';
	endPoint['lat'] = lat;
	endPoint['lng'] = lng;
	$('endAdressText').innerHTML = text;
	$('endInputBlock').style.display = 'none';
	$('endAdressBlock').style.display = 'block';
}

function setStartPointInput () {
	startPointType = 'text';
	if ($('startStreet').value == 'Strasse / Nr.' || $('startStreet').value == ''){
		$('startStreet').value = '';
	} else {
		$('startStreet').value += ','; 	
	}
	if ($('startZip').value == 'PLZ' || $('startZip').value == ''){
		$('startZip').value = '';
	} else {
		$('startZip').value += ','; 	
	}
	if ($('startCity').value == 'Ort' || $('startCity').value == ''){
		$('startCity').value = '';
	} else {
		$('startCity').value += ','; 	
	}
	if ($('startCountry').value == 'Land' || $('startCountry').value == ''){
		$('startCountry').value = '';
	} else {
		$('startCountry').value += ','; 	
	}
	startPointText = $('startStreet').value +" "+ $('startZip').value +" "+ $('startCity').value +" "+ $('startCountry').value;
	//startPointText = $('startStreet').value + "," + $('startZip').value + ',' + $('startCity').value + ',' + $('startCountry').value;
	$('startAdressText').innerHTML = $('startStreet').value + "<br />" + $('startZip').value + ' ' + $('startCity').value + '<br />' + $('startCountry').value;
	$('startInputBlock').style.display = 'none';
	$('startAdressBlock').style.display = 'block';
}

function setEndPointInput () {
	endPointType = 'text';
	if ($('endStreet').value == 'Strasse / Nr.' || $('endStreet').value == '') {
		$('endStreet').value = '';
	} 
	else {
		$('endStreet').value += ','; 	
	}
	if ($('endZip').value == 'PLZ' || $('endZip').value == '') {
		$('endZip').value = '';
	} 
	else {
		$('endZip').value += ','; 	
	}
	if ($('endCity').value == 'Ort' || $('endCity').value == '') {
		$('endCity').value = '';
	} 
	else {
		$('endCity').value += ','; 	
	}
	if ($('endCountry').value == 'Land' || $('endCountry').value == '') {
		$('endCountry').value = '';
	} 
	else {
		$('endCountry').value += ','; 	
	}
	endPointText = $('endStreet').value + "," + $('endZip').value + ',' + $('endCity').value + ',' + $('endCountry').value;
	$('endAdressText').innerHTML = $('endStreet').value + "<br />" + $('endZip').value + ' ' + $('endCity').value + '<br />' + $('endCountry').value;
	$('endInputBlock').style.display = 'none';
	$('endAdressBlock').style.display = 'block';
}

function ucFirst (value) {
	return value.substr(0,1).toUpperCase() + value.substr(1,value.length);
}

function showCategoriesContents (){
	$('categoriesBox').style.display = 'block';
	$('showCategoriesContents').style.display = 'none';
}

var MarkerManager = Class.create({
	initialize: function(map) {
		
		this.poiMarkers = new Object();
		this.lastKey = "";
		this.lastIcon = "";
		this.map = map;
		
		this.initClusterer();
			
	},
	
	toogleAcco : function (category,checked,icon)
	{
		this.lastKey = category;
		this.lastIcon = icon;
			
		if(checked == true)
		{
			this.enableDisableCheckboxes();
			
			new Ajax.Request('/tools/dta/proxy.php',{ method: 'get', parameters: 'lang=' + lang + '&category=' + category, onComplete: this.createAccoPois.bind(this) });
		}
		else
		{
			this.rewritePois(true);
		}
	},
	
	createAccoPois : function (transport)
	{
		this.enableDisableCheckboxes();
		
		var accos = transport.responseText.evalJSON();
		
		this.poiMarkers[this.lastIcon] = new Array();
		for (j = 0; j < accos.accommodation.length; j++)
		{
			this.poiMarkers[this.lastIcon][j] = new GMarker(new GLatLng(accos.accommodation[j].geoposition.latitude,accos.accommodation[j].geoposition.longitude),{icon: icon[this.lastIcon]});
					
			this.poiMarkers[this.lastIcon][j].myHTML = ('<div class="infoWindowContent"><b>' + accos.accommodation[j].name + '</b><br />' + accos.accommodation[j]['address-contact'].street + '<br />' + accos.accommodation[j]['address-contact'].zip + ' ' + accos.accommodation[j]['address-contact'].city +
			'<br /><br /><a class="details" href="'+ accos.accommodation[j].links.link['@attributes'].url + '">' + i18n[lang]['details'] + '</a><br /><br /><div class="routeBox"><span class="label">' + i18n[lang]['routenplaner'] + '</span>' + 
			'<span class="start" onclick="setAsStartPoint(' + accos.accommodation[j].geoposition.latitude + ',' + accos.accommodation[j].geoposition.longitude + ',\'<b>' + accos.accommodation[j].name + '</b><br />' + accos.accommodation[j]['address-contact'].street + '<br />' + accos.accommodation[j]['address-contact'].zip + ' ' + accos.accommodation[j]['address-contact'].city + '\');">' + i18n[lang]['start'] +  '</span>' +
			'' +
			'<span class="end" onclick="setAsEndPoint(' + accos.accommodation[j].geoposition.latitude + ',' + accos.accommodation[j].geoposition.longitude + ',\'<b>' + accos.accommodation[j].name + '</b><br />' + accos.accommodation[j]['address-contact'].street + '<br />' + accos.accommodation[j]['address-contact'].zip + ' ' + accos.accommodation[j]['address-contact'].city +  '\');">' + i18n[lang]['ziel'] + '</span></div></div>');

			GEvent.addListener(this.poiMarkers[this.lastIcon][j], "click", function() { this.openInfoWindow(this.myHTML); });	
		}
		this.rewritePois();
	},
	
	toogleAz : function (category,checked,icon)
	{
		this.lastKey = category;
		this.lastIcon = icon;
		
		if(checked == true)
		{
			this.enableDisableCheckboxes();

			new Ajax.Request('/tools/geoExport/json/a-z.php',{ method: 'get', parameters: 'lang=' + lang + '&category=' + category, onComplete: this.createAzPois.bind(this) });
		}
		else
		{
			this.rewritePois(true);
		}
	},
	
	createAzPois : function (transport)
	{
		this.enableDisableCheckboxes();
		
		var pois = transport.responseText.evalJSON();
		
		this.poiMarkers[this.lastIcon] = new Array();
		for (i = 0; i < pois.length; i++)
		{
			if (pois[i])
			{
				this.poiMarkers[this.lastIcon][i] = new GMarker(new GLatLng(pois[i].float_geoLat,pois[i].float_geoLng),{icon: icon[this.lastIcon]});
				
				if(parseInt(pois[i].zip) < 1) {
					zip = "";
				}
				else {
					zip = pois[i].zip;
				}
				if (pois[i].input_address < 1) {
					address = "";
				}
				else {
					address = pois[i].input_address;
				}
				
				this.poiMarkers[this.lastIcon][i].myHTML = ('<div class="infoWindowContent"><b>' + pois[i].input_name + '</b><br />' +  address + '<br />' + pois[i].zip + ' ' + pois[i].city +
				'<br /><br /><a class="details" href="/' + lang + '/service/a-z/?we_objectID=' + pois[i].OF_ID + '">' + i18n[lang]['details'] + '</a><br /><br />' +
				'<div class="routeBox"><span class="label">' + i18n[lang]['routenplaner'] + '</span>' + 
				'<span class="start" onclick="setAsStartPoint(' + pois[i].float_geoLat + ',' + pois[i].float_geoLng + ',\'<b>' + pois[i].input_name + '</b><br />' + address + '<br />' + pois[i].zip + ' ' + pois[i].city + '\');">' + i18n[lang]['start'] + '</span>' +
				'' +
				'<span class="end" onclick="setAsEndPoint(' + pois[i].float_geoLat + ',' + pois[i].float_geoLng + ',\'<b>' + pois[i].input_name + '</b><br />' + address + '<br />' + pois[i].zip + ' ' + pois[i].city + '\');">' + i18n[lang]['ziel'] + '</span></div></div>');
				
				if(pois[i].input_name.length > 2 || pois[i]['text_description_' + ucFirst(lang)].length > 2) {
					GEvent.addListener(this.poiMarkers[this.lastIcon][i], "click", function() { this.openInfoWindow(this.myHTML); });
				}
			}
		}
		this.rewritePois();
	},
	
	enableDisableCheckboxes : function ()
	{
		var checks = $('categoriesBox').getElementsByTagName("input");
		
		for (e=0;e<checks.length;e++)
		{
			if(checks[e].disabled == true)
			{
				checks[e].disabled = false;
			}
			else
			{
				checks[e].disabled = true;
			}
		}
	},
	
	initClusterer : function () {
		this.clusterer = new Clusterer(this.map);
		this.clusterer.SetMinMarkersPerCluster(1);
		this.clusterer.SetMaxVisibleMarkers(30);
	},
	
	rewritePois : function  (deleteItem)
	{
		// remove all markers from clusterer
		if(deleteItem == true) {
			var name = this.lastIcon;
			if (this.poiMarkers[name].length > 0)
			{
				for (var b = 0; b < this.poiMarkers[name].length; b++)
				{
					this.clusterer.RemoveMarker(this.poiMarkers[name][b]);
				}
			}
			delete this.poiMarkers[this.lastIcon];	
		}
		else {
			// add all markers from pool to the clusterer
			var name = this.lastIcon;
			if (this.poiMarkers[name].length > 0)
			{
				for (var d = 0; d < this.poiMarkers[name].length; d++)
				{
					this.clusterer.AddMarker(this.poiMarkers[name][d],"");
				}
			}
			
		}
		
		// hack for cluserer.js 
		this.map.setZoom(this.map.getZoom()+1);
		this.map.setZoom(this.map.getZoom()-1);
	}
	
});

var markerManager = new MarkerManager (map);

