$(document).ready(function() {
	initLayer();
	// calculate the position of the navigation 
	// according to the menu button
	resizeNavigation();
});


var useAjax = true;
//layer visible?
var showingLayer = false; 

// basic width
var basicWidth = 0;

function initLayer()
{
	
	// calculate imprint position
	calculateImprintAndBgPosition();
	$(window).resize( function() {
		calculateImprintAndBgPosition();
	});
	
	// IE --> AJAX test
	if($.browser.msie)
	{
		var ajaxTest = new ActiveXObject("Msxml2.XMLHTTP") 
		if(typeof ajaxTest != 'object' )
		{
			useAjax = false;
		}
		
		// max width IE < 7
		if(browserIeVersion() < 7)
		{
			basicWidth = $("div.karriere-left-position").css("width");
			calculateIeMaxWidth();
			$(window).resize(function()
			{
				calculateIeMaxWidth();
			});
		}
		
	}
	
	// ajax ok!
	if(useAjax)
	{
	
		//deactive layer links
		$("a.layer_link").click(function(){
	
			var url = this.href + "";
			loadLayerContent(url);
			return false;		
		});
	
		// bookmarked ? 
		var uri = window.location + ''; //making a string
		uri = uri.replace("%23","#"); // replacing encoding
		if(uri.indexOf("##") != -1)
		{
			uri = uri.split("#");
			// avaliable?
			loadLayerContent(uri[2]);
		}
	}
}



function loadLayerContent(pUrl)
{
		$.ajax({
				type: "GET",
			url: pUrl,
			async: false, // blocks showing the layer
			success: function(html)
			{
				// "imagetoolbar" craches IE 6
				html = html.replace("imagetoolbar", "");
				
				// parsing xhtml				
				var layerContent = $(html).filter("div#karriere-layer").html();

				if(typeof $("#technologie")[0] != "undefined")
				{// stop flash
					$("#technologie")[0].SetVariable("stopAnimation", 1);
				}
				
				// insert the new content
				$("div#karriere-layer").html(layerContent);
				
				// show layer
				openLayer(pUrl);
				}
			});
}

function openLayer(pUrl)
{
	
	// deactive close link
	$("a#case-study-layer-karriere-close").click(function()
	{
		
		// disable bookmark-anchor
		var uri = window.location + '';//making a string
		uri = uri.split("#");
		window.location = uri[0] + '#';
		
	
		$("div#karriere-layer").fadeOut(300, function(){
			
			if(typeof $("#technologie")[0] != "undefined")
			{// play flash again
				$("#technologie")[0].SetVariable("stopAnimation", 0);
			}
		});
		
		showingLayer = false;
		
		// calculate imprint position
		calculateImprintAndBgPosition();
		
					
		
		return false;		
	});

	// making bookmarkable
	var uri = window.location + '';//making a string
	uri = uri.split("#");
	window.location = uri[0] + "##" + pUrl;

	// hide navigation
	//hideNavigation();

	showingLayer = true;
	
	// show layer	
	$("div#karriere-layer").show().fadeIn(300, function(){
			// calculate layer height and imprint position
			calculateImprintAndBgPosition();
	});
}


function calculateImprintAndBgPosition()
{
	var imprintHeight = 0;
	var bottomOfPage  = 0;
/*
	if($.browser.mozilla)
	{
		//imprintHeight = $("div#karriere-imprint")[0].offsetHeight;
		
		imprintHeight = $("div#imprint")[0].offsetHeight;
		
		
		bottomOfPage = emToPx($("div.karriere-content-zone").css("top")) + $("div.karriere-content-zone")[0].offsetHeight;
	}
	else
	{
		//imprintHeight = $("div#karriere-imprint").height();
		imprintHeight = $("div#imprint").height();
		
		bottomOfPage = emToPx($("div.karriere-content-zone").css("top")) + $("div.karriere-content-zone").height();
		
	}
*/
	var newPositionEm = (bottomOfPage + imprintHeight) / 10;
	var windowSizeEm = (getWindowInnerSize().height) / 10;
	
	// is the layer content bigger?
	if(showingLayer)
	{
		var layerHeightEm = 0;
		if($.browser.mozilla) layerHeightEm = ($("div#karriere-layer-content-zone")[0].offsetHeight + getAbsolutePosition($("div#karriere-layer-content-zone")[0]).y) / 10;
		else 				  layerHeightEm = ($("div#karriere-layer-content-zone").height() + getAbsolutePosition($("div#karriere-layer-content-zone")[0]).y) / 10;
	}
	// what is bigger - layer or content?
	if(layerHeightEm+1 > windowSizeEm)
	{ // layer
		$("div#karriere-layer").css("height", (layerHeightEm+1) + "em");
		$("div#karriere-layer").css("bottom", "0");
		
		// layer over karriere-content-zone
		if(!(bottomOfPage > (layerHeightEm * 10)))
		{ 
			newPositionEm = layerHeightEm + 1;
		}
		
		var windowWidth = (getWindowInnerSize().width - 17);
	} 
	else 
	{ 
		windowWidth = (getWindowInnerSize().width); 
		//px fixes ie6/7 em window size issue
		$("div#karriere-layer").css("height", (getWindowInnerSize().height  + "px"));
	}
	
	/*
	var backgroundWidth = $("div.karriere-content-zone").width() + emToPx($("div.karriere-content-zone").css("left"));
	
	// Window smaller than background?
	if (windowWidth < backgroundWidth)
	{
		$("div#karriere-layer").css("width", (backgroundWidth/10) + "em");
	}
	else
	{
		$("div#karriere-layer").css("width", (windowWidth/10) + "em");
	}

	
	if(windowSizeEm < newPositionEm)
	{
		$("div#karriere-imprint").css("top", newPositionEm + "em");
	}
	else
	{
		$("div#karriere-imprint").css("top", "auto");
	}
	
	// get real position of imprint
	//$("div#karriere-layer").css("height", (($("div#karriere-imprint")[0].offsetTop)/10)  + "em");
	//$("div#karriere-layer").css("height", (windowSizeEm  + "em"));
	*/
}

function calculateIeMaxWidth()
{

	// basicly standard
	$("div.karriere-left-position").css("width", basicWidth);
	var maxWidthEm = parseFloat($("div.karriere-left-position").css("max-width"))*10;
	if($("div.karriere-left-position").width() > maxWidthEm)
	{ // to width --> stop
		$("div.karriere-left-position").css("width", $("div.karriere-left-position").css("max-width"));
		
	}
}