var contentStatus = "closed";
var map = false;
var mPos = true;

function addContactLinks(elString){
	$("#contentContainer a.contactLink").unbind('click');
	$(elString).bind('click', function(){
		$('#topNaviList a').removeClass("active");
		$('#topNaviList a[href="/kontakt"]').addClass("active");
		$.post($(this).attr("href"), {
			"ajax": "1"
		}, function(html){
			$("#contentContainer").fadeTo("fast", 0.0, function(){
				$("#contentContainer").html(html);
			});
			
			showContent();
		});
		return false;
	});
	scrambleMail();
}

function scrambleMail(){

	ML = "t<m@rafE\".dn :>be-lhoM/i=";
	MI = "7AE5GB=<15<C4@6H825GB0D=G;6D3B0A?5:@;9:@8>G;6D3B0A?5:@;9:@1F5>";
	OT = "";
	for (j = 0; j < MI.length; j++) {
		OT += ML.charAt(MI.charCodeAt(j) - 48);
	}
	
	$(".contactMail").html(OT);
	
	
}


function initializeMap(){

	setMapSize();
	
	var blueIcon = new GIcon(G_DEFAULT_ICON);
	blueIcon.image = "img/map_marker.png";
	
	map = new GMap2(document.getElementById("map"));
	map.addControl(new GLargeMapControl(), new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10, 10)));
	map.addControl(new GMapTypeControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10, 10)));
	
	GEvent.addListener(map, "load", function(){
		$('#map div.gmnoprint').eq(0).css({
			bottom: '48px',
			right: '30px'
		}).end().eq(1).css({
			bottom: '45px',
			left: '10px'
		});
	});
	
	map.setCenter(new GLatLng(48.0911, 7.9850), 9);
	
//	var geocoder = new GClientGeocoder();
//	function showAddress(address){
//		geocoder.getLatLng(address, function(point){
//			if (!point) {
//				alert(address + " not found");
//			} else {
//				console.log(point);
//			}
//		});
//	}
//	showAddress('Dorfstraße, Mundingen 79312 Emmendingen, Germany');
	
	$("#topContent").hide();
	$("#locationBox").show();
	hideContent();
	
	$('#topNaviList a,.imprint a').bind('click', function(){
		$('#topNaviList a,.imprint a').removeClass("active");
		$(this).addClass("active");
		
		$.post($(this).attr("href"), {
			"ajax": "1"
		}, function(html){
			$("#contentContainer").fadeTo("fast", 0.0, function(){
				$("#contentContainer").html(html);
				$("a[@rel*=fancyvideo]").fancybox({
					overlayShow: true,
					frameWidth:640,
					frameHeight:360
				});
			});
			
			showContent();
		});
		return false;
	});
	
	
	$('#locationList a').each(function(){
	
		var self = this;
		
		var latlng = $(this).attr("name").split("/");
		var lat = latlng[0];
		var lng = latlng[1];
		
		var marker = addMarker(new GLatLng(lat, lng), $(this).attr("href"));
		
		map.addOverlay(marker);
		
		GEvent.addListener(marker, "click", function(){
			addToolTip(map, self, lat, lng);
		});
		
		$(this).bind('click', function(){
			addToolTip(map, self, lat, lng);
			return false;
		});
	});
	
}

function addMarker(point, link){
	var icon;
	var blueIcon = new GIcon(G_DEFAULT_ICON);
	blueIcon.image = "img/map_marker.png";
	if (link.search(/none/) == -1) {
		icon = blueIcon;
	} else {
		icon = new GIcon(G_DEFAULT_ICON);
	}
	return new GMarker(point, {
		icon: icon
	});
}


function addToolTip(map, el, lat, lng){

	var self = el;
	
	$.post($(el).attr("href"), {
		"ajax": "1",
		"name": $(el).attr("type")
	}, function(html){
		map.openInfoWindowHtml(new GLatLng(lat, lng), html, {
			onOpenFn: function(){
				addContactLinks("#toolTip a.contactLink");
				$(".lightboxPic").lightbox();
				$(".lbLink").setPNGTransparenz4IE6();
				$(".lbLink").mouseover(function(){
					$(this).attr("src", $(this).attr("alt") + "_hover.png");
				}).mouseout(function(){
					$(this).attr("src", $(this).attr("alt") + ".png");
				});
			}
		});
		
	}, "html");
	
}


function showContent(){

	var normal = map.fromContainerPixelToLatLng(new GPoint(0, 0));
	var move = map.fromContainerPixelToLatLng(new GPoint(0, 71));
	
	var latDifference = normal.lat() - move.lat();
	
	var coords = map.getCenter();
	if (contentStatus == "closed") 
		map.setCenter(new GLatLng(coords.lat() + latDifference, coords.lng()));
	
	$("#locationBox").fadeTo("slow", 0.0);
	
	contentStatus = "open";
	
	$("div.gmnoprint").each(function(){
		if ($(this).css("top") == "10px") 
			$(this).css("top", "80px");
	});
	
	setMapSize();
	$("#gMapWrapper").css("padding", "0px 0px 0px 0px");
	$("#topContent").slideDown(1500, function(){
	
		$("#slideBt").attr("class", "slideUp");
		
		$("#slideBt, #logo").unbind("click", showContent);
		$("#slideBt, #logo").bind("click", hideContent);
		
		//$("#logo").unbind("click",showContent);
		//$("#logo").bind("click",hideContent);
		
		$("#contentContainer").fadeTo("slow", 1.0, function(){
			addContactLinks("#contentContainer a.contactLink");
		});
	});
	
	
}

function hideContent(){

	$("#contentContainer").fadeTo("slow", 0.0);
	
	$("#topContent").slideUp(1500, function(){
		$("#slideBt").attr("class", "slideDown");
		
		$("#slideBt, #logo").unbind("click", hideContent);
		$("#slideBt, #logo").bind("click", showContent);
		$("#gMapWrapper").css("padding", "70px 0px 0px 0px");
		
		
		var normal = map.fromContainerPixelToLatLng(new GPoint(0, 0));
		var move = map.fromContainerPixelToLatLng(new GPoint(0, 71));
		
		var latDifference = normal.lat() - move.lat();
		
		var coords = map.getCenter();
		
		if (contentStatus == "open") 
			map.setCenter(new GLatLng(coords.lat() - latDifference, coords.lng()));
		$("#locationBox").fadeTo("slow", 1.0);
		contentStatus = "closed";
		setMapSize();
		
		$("div.gmnoprint").each(function(){
			if ($(this).css("top") == "80px") 
				$(this).css("top", "10px");
		});
		
	});
	
}


function setMapSize(){
	var mapPadding = (contentStatus == "closed") ? 70 : 0;
	var hHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		//Non IE Clients
		hHeight = window.innerHeight;
	} else if (document.documentElement &&
	(document.documentElement.clientWidth ||
	document.documentElement.clientHeight)) {
		//IE 6+
		hHeight = document.documentElement.clientHeight;
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		//only for IE 4
		hHeight = document.body.clientHeight;
	}
	// set new height...
	var nHeight = hHeight - (80 + mapPadding);
	
	
	document.getElementById('gMapWrapper').style.height = nHeight + "px";
	document.getElementById('map').style.height = nHeight + "px";
	if (map != false) 
		map.checkResize();
}


window.onresize = function(){
	setMapSize();
}


jQuery.fn.setPNGTransparenz4IE6 = function(){
	return this.each(function(e){
		if (!$.browser.msie) {
			return this;
		} else if (parseInt($.browser.version) != 6) {
			return this;
		}
		var div = $("<div />");
		for (var n in this.attributes) {
			if (this[n] != "" && this[n] != null) {
				if (n.substr(0, 2) == "on") {
					div.bind(n.substr(2), this[n]);
				} else if (n != "src") {
					div.attr(n, this[n]);
				}
			}
		}
		div.attr("style", this.style.cssText);
		div.css({
			width: this.width + "px",
			height: this.height + "px",
			filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.src + "', sizingMethod='scale')"
		});
		if (this.align == "left") {
			div.css("float", "left");
		}
		if (this.align == "right") {
			div.css("float", "right");
		}
		if (this.parentNode.href) {
			div.css("cursor", "hand");
		}
		this.parentNode.replaceChild(div[0], this);
	});
}


$(document).ready(function(){
	initializeMap();
	$(".pageWidth[id!=pageWrapper]").mouseover(function(){
		mPos = false;
	}).mouseout(function(){
		mPos = true;
	});
	$(document).click(function(){
		if (mPos == true && contentStatus == "open") {
			hideContent();
		}
	});
	$("#slideBt").hover(function(){
		$(this).addClass("hover");
	}, function(){
		$(this).removeClass("hover");
	});
	$("a[@rel*=fancyvideo]").fancybox({
		overlayShow: true,
		frameWidth:640,
		frameHeight:360
	});
	$('a[rel=external]').bind('click', function() {
		window.open(this.href);
		return false;
	});

	$('#selectReferenz').bind('change', function(){
		var selected = $(this).val();
		var $li = $('#locationList li.'+selected+' a');
		
		var latlng = $li.attr("name").split("/");
		var lat = latlng[0];
		var lng = latlng[1];

		addToolTip(map, $li, lat, lng);
		return false;
	});
});





