var centerLatitude = 52.4786;
var centerLongitude = 13.3538;
var startZoom = 10;
var map;
var thermelogo = new GIcon();
thermelogo.image = 'http://www.therme-sauna-freizeitbad.de/logotherme.png';
thermelogo.shadow = 'http://www.therme-sauna-freizeitbad.de/logothermeschadow.png';
thermelogo.iconSize = new GSize(26, 25);
thermelogo.shadowSize = new GSize(50, 22);
thermelogo.iconAnchor = new GPoint(13, 25);
thermelogo.infoWindowAnchor = new GPoint(13, 1);
thermelogo.infoShadowAnchor = new GPoint(26, 13);

var deselectCurrent = function() {};


var PromoControl = function(url) {
	this.url_ = 'http://www.therme-sauna-freizeitbad.de/grossraum-berlin.php';
};

PromoControl.prototype = new GControl(true);

PromoControl.prototype.initialize = function(map) {
	var container = document.createElement("div");
	container.innerHTML =
	
		
		

		
		
		'<a href="http://www1.belboon.de/adtracking/0284b307da7a0065ae000648.html" target="_blank">'
		+ '<img src="http://www1.belboon.de/adtracking/0284b307da7a0065ae000648.img" border="0" width="120" '
		+ ' height="90" alt="" /></a>';
		



	
	
		
	container.style.width='120px';
	container.style.height='90px';
	
	url = this.url_;
	
	GEvent.addDomListener(container, "click", function() {
		document.location = url;
	});
	
	map.getContainer().appendChild(container);
	
	return container;
};

PromoControl.prototype.getDefaultPosition = function() {
	return new GControlPosition(
		G_ANCHOR_TOP_LEFT, new GSize(70, 10));
};



function initializePoint(pointData) {
	var point = new GLatLng(pointData.latitude,
		pointData.longitude);
	var marker = new GMarker(point, {title:pointData.state, icon:thermelogo});
	var listItem = document.createElement('li');
	var listItemLink =
		listItem.appendChild(document.createElement('a'));
	var visible = false;

	listItemLink.href = "#";
	listItemLink.innerHTML = '<strong>' + pointData.city
		+ ' </strong><span>' 
		+ pointData.state + '</span>';
	
	var focusPoint = function() {
		deselectCurrent();
		// Abschnitt current muss in der CSS-Datei
		// entsprechend ergänzt werden
		listItem.className = 'current';
		deselectCurrent = function() {
			listItem.className = ''; }
		//marker.openInfoWindowHtml(pointData.address);
		var infoTabs = [	new GInfoWindowTab("Bad", pointData.address) ];
		marker.openInfoWindowTabsHtml(infoTabs);
		
		map.panTo(point);
		return false;
	}

	GEvent.addListener(marker, 'click', focusPoint);	
	listItemLink.onclick = focusPoint;

	pointData.show = function() {
		if (!visible) {
			document.getElementById(
				'sidebarmap-list').appendChild(listItem);
			map.addOverlay(marker);
			visible = true;
		}
	}
	pointData.hide = function() {
		if (visible) {
			document.getElementById(
				'sidebarmap-list').removeChild(listItem);
			map.removeOverlay(marker);
			visible = false;
		}
	}

	pointData.show();
}

function initializeSortTab(type) {
	var listItem = document.createElement('li');
	var listItemLink = listItem.appendChild(
		document.createElement('a'));

	listItemLink.href = "#";
	listItemLink.innerHTML = type;
	listItemLink.onclick = function() {
		changeBodyClass('standby', 'loading');

		for(id in markers) {
			if (markers[id].type == type || 'Alle' == type)
				markers[id].show();
			else
				markers[id].hide();	
		}

		changeBodyClass('loading', 'standby');

		return false;
	}

	document.getElementById('filters').appendChild(listItem);
}

function windowHeight() {
	// Standardbrowser (Mozilla, Safari usw.)
	if (self.innerHeight)
		return self.innerHeight;
	// IE 6
	if (document.documentElement
		&& document.documentElement.clientHeight)
		return document.documentElement.clientHeight;
	// IE 5
	if (document.body)
		return document.body.clientHeight;
	// Nur für den Fall...
	return 0;
}

function handleResize() {
	var height = windowHeight()
		- document.getElementById('toolbar').offsetHeight - 30;
		
		
	document.getElementById('map').style.height
		= 500 + 'px';
	document.getElementById('sidebarmap').style.height
		= 500 + 'px';
}

function changeBodyClass(from, to) {
	document.body.className =
		document.body.className.replace(from, to);
	return false;
}


function init() {
	var type;
	var allTypes = { 'Alle':[] };
	
	document.getElementById('button-sidebarmap-hide').onclick =
		function() {
			return changeBodyClass('sidebarmap-right', 'nosidebarmap');
		};
	document.getElementById('button-sidebarmap-show').onclick =
		function() {
			return changeBodyClass('nosidebarmap', 'sidebarmap-right');
		};
	handleResize();
	
	map = new GMap2(document.getElementById("map"));
	map.addControl(new GSmallMapControl());
	
	
	
	map.setCenter(new GLatLng(centerLatitude,
		      centerLongitude), startZoom);

	for(id in markers) {
		initializePoint(markers[id]);
		allTypes[markers[id].type] = true;
	}

	for(type in allTypes) {
		initializeSortTab(type);
	}

	changeBodyClass('loading', 'standby');
}

window.onresize = handleResize;
window.onload = init;
window.onunload = GUnload;
