new Namespace("at.kitzbuehel.map");
at.kitzbuehel.map = Class.create({

	zoom: false,
	clusterMarkers: {},
	map: null,
	clusterer: null,
	tourLine: null,
	tourBounds: null,

	initialize: function(container) {
		if (GBrowserIsCompatible()) {
			this.map = new GMap2(container);
			this.map.addMapType(G_PHYSICAL_MAP);

			if ($('portal')) {
				this.map.setCenter(new GLatLng(47.31555190588103, 12.407684326171875), 10);
				/*this.map.addMapType(new TextualZoomControl());*/
			}
			else {
				this.map.setCenter(new GLatLng(47.42555190588103, 12.407684326171875), 10);
				this.map.addMapType(G_SATELLITE_3D_MAP);
			}
			this.map.setMapType(G_PHYSICAL_MAP);


			var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10, 30));
			var bottomRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(70, 20));
			this.map.addControl(new GMapTypeControl(), bottomRight);
			this.map.addControl(new GLargeMapControl(), topRight);
			GEvent.addListener(this.map, "maptypechanged", function () {
				status = map.getCurrentMapType().getName();
				try {
					switch (status) {
						case "Earth":
							$('console').hide();
							$('menumap').hide();
							break;
						default:
							$('console').show();
							$('menumap').show();
					}
				}
				catch (e) {
				}
			});
			window.map = this.map;


		}

	},

	getMap : function () {
		return this.map;
	},

	getPois : function (type) {


		new Ajax.Request("/" + language + "/poi/" + type, {
			onComplete: this.getPoisComplete.bind(this, type),
			method: "get"
		});
	},

	getPoisComplete : function (type, transport) {
		var marker = null;
		var data = transport.responseText.evalJSON();
		this.clusterMarkers[type] = [];

		var icon = new GIcon();
		icon.iconSize = new GSize(33, 42);
		//icon.iconAnchor = new GPoint(7, 4);
		icon.iconAnchor = new GPoint(3, 36);
		icon.image = "/static/img/marker/" + type + ".png";
		icon.infoWindowAnchor = new GPoint(42, 0);


		for (var i = 0; i < data.length; i++) {
			if (data[i].point.latitude != null && data[i].point.longitude != null) {
				marker = new GMarker(new GLatLng(data[i].point.latitude, data[i].point.longitude), {icon: icon});
				marker.data = data[i];
				GEvent.addListener(marker, "click", this.markerClick.bind(this, marker, type));
				this.clusterMarkers[type].push(marker);
			}
		}

		this.redrawClusteredMarkers();
	},

	setMarkersForKey : function (key, markers) {
		this.clusterMarkers[key] = markers;
		this.redrawClusteredMarkers();
	},

	markerClick : function (marker, type) {

		var data = marker.data;

		if (type == "tour") {

			text = '<div class="standard_window">' +
					'<div class="content tour" onclick="location.href=\'' + data.link + '\';">' +
					'<img class="type" src="/static/img/icons/' + data.typeSport + '_s.gif" />' +
					'<div class="headline"><span class="name">' + data.name + '</span><span class="difficulty">' + data.difficulty + '</span></div>' +
					'<div class="description">' + data.description + '</div>' +
					'<hr />' +
					'<div class="keyvalue">' +
					'<div class="key">Länge</div><div class="value">' + data.length + '</div>';

			if ((data.altitudeup.length != "-") && (data.altitudedown.lenght != "-")) {
				text = text + '<div class="key">Höhenmeter ▲</div><div class="value">' + data.altitudeup + '</div>' +
						'<div class="key">Höhenmeter ▼</div><div class="value">' + data.altitudedown + '</div>';
			}

			else if (data.verticalheight.length != "-") {
				text = text + '<div class="key">Höhendifferenz</div><div class="value">' + data.verticalheight + '</div>';
			}

			text = text + '<div class="key">Tour-Rank</div><div class="value"><img src="/static/img/layout/rank_0' + Math.round(data.rank) + '.gif" /></div>' +
					'</div>' +
					'</div><div class="background"></div></div><div class="value"></div>';

			marker.openExtInfoWindow(
					this.map, "map_window", text, {beakOffset: 0}
					);
		}

		if (type == "guesthouse" || type == "farm" || type == "private" || type == "apartments" || type == "pension" ||
				type == "hotel2" || type == "hotel3" || type == "hotel4" || type == "hotel5" || type == "rad") {
			marker.openExtInfoWindow(
					this.map,
					"map_window",
					'<div class="standard_window">' +
							'<div class="content hotel" onclick="window.open(\'' + data.link + '\');">' +
							'<div class="headline">' + data.name + '</div>' +
							'<div class="description">' + data.description + '</div>' +
							'<hr />' +
							'<div class="lineentry">' + data.address + '</div>' +
							'<div class="lineentry">' + data.zip + ' ' + data.city + '</div>' +
							'<div class="lineentry">' + data.phone + '</div>' +
							'<div class="lineentry">' + data.homepage + '</div>' +
							'<div class="lineentry">' + data.email + '</div>' +
							'</div><div class="background"></div></div><div class="value"></div>',
			{beakOffset: 0}
					);
		}

		if (type == "huts" || type == "restaurants" || type == "skirental" || type == "bars" || type == "parking" || type == "playgrounds" || type == "bikerental" || type == "sportshop") {
			marker.openExtInfoWindow(
					this.map,
					"map_window",
					'<div class="standard_window">' +
							'<div class="content feratel" onclick="window.open(\'' + data.link + '\');">' +
							'<div class="headline">' + data.name + '</div>' +
							'<div class="description">' + data.description + '</div>' +
							'<hr />' +
							'<div class="lineentry">' + data.address + '</div>' +
							'<div class="lineentry">' + data.zip + ' ' + data.city + '</div>' +
							'<div class="lineentry">' + data.phone + '</div>' +
							'<div class="lineentry">' + data.homepage + '</div>' +
							'<div class="lineentry">' + data.email + '</div>' +
							'</div><div class="background"></div></div><div class="value"></div>',
			{beakOffset: 0}
					);
		}

		if (type == "lonelyhuts" || type == "viewpoints") {
			marker.openExtInfoWindow(
					this.map,
					"map_window",
					'<div class="standard_window">' +
							'<div class="content webedition">' +
							'<div class="headline">' + data.name + '</div>' +
							'<div class="description">' + data.description + '</div>' +
							'<hr />' +
							'<div class="lineentry">' + data.address + '</div>' +
							'<div class="lineentry">' + data.zip + ' ' + data.city + '</div>' +
							'<div class="lineentry">' + data.phone + '</div>' +
							'<div class="lineentry">' + data.homepage + '</div>' +
							'<div class="lineentry">' + data.email + '</div>' +
							'</div><div class="background"></div></div><div class="value"></div>',
			{beakOffset: 0}
					);
		}
	},

	isCategoryActive : function (key) {
		if (this.clusterMarkers[key]) {
			return true;
		}
		return false;
	},

	toogleCategory : function (key, button) {
		if (this.isCategoryActive(key)) {
			button.parentNode.removeClassName("active");
			this.removeCategory(key);
		}
		else {
			button.parentNode.addClassName("active");
			this.getPois(key);
		}
		return false;
	},

	toogleCategorybyListelement : function (key, listelement) {
		if (this.isCategoryActive(key)) {
			listelement.removeClassName("active");
			this.removeCategory(key);
		}
		else {
			listelement.addClassName("active");
			this.getPois(key);
		}
		return false;
	},


	removeCategory : function (key) {
		delete this.clusterMarkers[key];
		this.redrawClusteredMarkers();
	},

	redrawClusteredMarkers : function () {
		var markers = [];

		var categoryKeys = Object.keys(this.clusterMarkers);
		for (var i = 0; i < categoryKeys.length; i++) {
			for (var u = 0; u < this.clusterMarkers[categoryKeys[i]].length; u++) {
				markers.push(this.clusterMarkers[categoryKeys[i]][u]);
			}
		}

		// remove old marker manager
		if (this.clusterer) {
			this.clusterer.clearMarkers();
		}

		this.clusterer = new MarkerClusterer(this.map, markers, {styles: [
			{
				url: '/static/img/marker/cluster.png',
				height: 54,
				width: 44,
				zIndex: 1,
				opt_anchor: [8, 8],
				opt_textColor: '#FFFFFF'
			}
		],gridSize: 40 });
	},

	setLogo : function() {
		// alert ("change");
	},
	zoomMap : function() {
		try {
			console.log(this.zoom);
			if (this.zoom == false) {
				$('map').addClassName("zoom");
				$('mapzoom').addClassName("zoom");
				$('mapzoom').update($('txtZoomIn').innerHTML);
				this.zoom = true;
			}
			else {
				$('map').removeClassName("zoom");
				$('mapzoom').removeClassName("zoom");
				$('mapzoom').update($('txtZoomOut').innerHTML);
				this.zoom = false;
			}
			console.log(this);
			//this.map.clearOverlays();
			this.map.checkResize();

			//this.map.addOverlay(this.tourLine);
			this.map.setCenter(this.tourBounds.getCenter());
			this.map.setZoom(this.map.getBoundsZoomLevel(this.tourBounds));
		}
		catch (e) {
		}
	},
	setTourline : function (line) {
		this.tourLine = line;
	},
	setTourBounds : function (bounds) {
		this.tourBounds = bounds;
	}

});


var tourKeys = ["climbing","fixedrope","cycle","racer","snowshow","mountainbike","crosscountry","running","skitour","nordicwalking","hiking"];
var tourTypes = ["pro","standard"]
var tourIcons = {};
var combinedKey = "";

for (var u = 0; u < tourTypes.length; u++) {
	for (var z = 0; z < tourKeys.length; z++) {
		combinedKey = tourKeys[z] + "_" + tourTypes[u];
		tourIcons[combinedKey] = new GIcon();
		tourIcons[combinedKey].iconSize = new GSize(33, 42);
		tourIcons[combinedKey].iconAnchor = new GPoint(7, 4);
		tourIcons[combinedKey].image = "/static/img/marker/" + combinedKey + ".png";
		tourIcons[combinedKey].infoWindowAnchor = new GPoint(42, 0);
	}
}


function substr(f_string, f_start, f_length) {
	// Returns part of a string
	//
	// version: 810.1317
	// discuss at: http://phpjs.org/functions/substr
	// +     original by: Martijn Wieringa
	// +     bugfixed by: T.Wild
	// +      tweaked by: Onno Marsman
	// *       example 1: substr('abcdef', 0, -1);
	// *       returns 1: 'abcde'
	// *       example 2: substr(2, 0, -6);
	// *       returns 2: ''
	f_string += '';

	if (f_start < 0) {
		f_start += f_string.length;
	}

	if (f_length == undefined) {
		f_length = f_string.length;
	} else if (f_length < 0) {
		f_length += f_string.length;
	} else {
		f_length += f_start;
	}

	if (f_length < f_start) {
		f_length = f_start;
	}

	return f_string.substring(f_start, f_length);
}


var panoramioActive = false;
var panoramio;
var MarkersArray = [];
function switchPanoramio(menu) {

	Counter = 1;
	if (panoramioActive) {
		panoramioActive = false;
		if ($(menu)) {
			$(menu).removeClassName("active");
		}
		map.removeOverlay(panoramio);
	}
	else {
		panoramio = new GLayer("com.panoramio.all");
		panoramioActive = true;
		if ($(menu)) {
			$(menu).addClassName("active");
		}
		map.addOverlay(panoramio);
	}
}




