var isPlaying = false;
var showHeadPlayerTimeoutID = null;
$(document).ready(function() {
	$('#primaryNavigation').primaryNavigation();

//	same height columns
	var maxContentColumnH = 0;
	$('#boxedTextContentBody .contentColumn').each(function(i, el){
		maxContentColumnH = Math.max(maxContentColumnH, $(el).height());
	});
	$('#boxedTextContentBody .contentColumn').height(maxContentColumnH);
	
//	obscured emails	
	$('.obscured').each(function () {
		var m = $(this).attr('href').split('__');
		$(this).attr('href', 'mailto:' + m[7] + '@' + m[3]);
	});
	
//	url parameters
	params = window.location.search.substring(1).split('&');
	for (var i=0;i<params.length;i++) {
		var pSplit = params[i].split('=');
		switch (pSplit[0]) {
			case 'autoplay':
				AUTOPLAY = true;
				break
		}
	}
	
//	Header img / video replacement logic
	if (typeof(FLASHVARS) != 'undefined' && FLASHVARS) {
		
		
		var headContentImg = $('#headContent #headContentImages').clone();
		headContentImg.css('visibility', 'visible');
		$('#headContent').bind("touchstart , mouseenter" , function(evt){
			if (isPlaying) return;
			isPlaying = true;
			if (showHeadPlayerTimeoutID) clearTimeout(showHeadPlayerTimeoutID);
			showHeadPlayerTimeoutID = setTimeout(function(){showHeadPlayer(FLASHVARS);}, 750);
		});
		$('#headContent').bind("touchend , mouseleave" , function(evt){
			isPlaying = false;
			if (showHeadPlayerTimeoutID) clearTimeout(showHeadPlayerTimeoutID);
			$('#headContent object').replaceWith(headContentImg.clone());
			$('#headContent div').replaceWith(headContentImg.clone());
			$('#headContentImages').cycle({});
		});
		
		if (typeof(AUTOPLAY) != 'undefined' && AUTOPLAY) {
			isPlaying = true;
			showHeadPlayer(FLASHVARS);
		}
	}
	
//	slideshow
	if (typeof(PREVENT_CYCLE) == 'undefined') {
		if ($('#headContentImages img').length > 1) $('#headContentImages').cycle({});
	}
	
//	iOS FIX #footer
	if(navigator.platform.indexOf('iPad') != -1 || navigator.platform.indexOf('iPhone') != -1 || navigator.platform.indexOf('iPad') != -1) {
		$("#footer").css("position", "static");
	};
});

function showHeadPlayer (vars) {
	if (swfobject.getFlashPlayerVersion().major == 0) {
		div = document.createElement( 'div' );
		video = document.createElement( 'video' );
	    img = document.createElement( 'img' );
		$(img).attr("src","/img/ui/arrow.png");
		$(img).css({"padding" : "5px"});
		$(div).css({"text-align" : "center" ,"background-color" : "#FFFFFF"});
	    video.autoplay =  true;
	    video.controls = true;
	    video.width =  1024;
	    video.height = 576;
	    video.type= "video/mp4"	; 
	    video.codecs="avc1.42E01E, mp4a.40.2";
	    video.src=vars.video.substring(0,vars.video.length-3)+"mp4";
	    $(div).append(video);
	    $(div).append(img);
	    $("#headContentImages").replaceWith(div);
	} else {
		var flashvars = vars;
		var params = {};
		params.allowScriptAccess = "always";
		params.allowfullscreen = "true";
		params.bgcolor= "#ffffff";
		params.wmode= "opaque";
		var attributes = {};
		swfobject.embedSWF("/de/swf/header_player.swf", "headContentImages", "1024", "576", "9", "/js/libs/swfobject/expressInstall.swf", flashvars, params, attributes);
	}
}
