var firstCall = true;
var firstPart = document.location.protocol + '//' + document.location.hostname;
if (document.location.pathname !== "/") {
	document.location.href = firstPart + '#' + document.location.pathname;
}
var fromNavigateTo = false;
SWFAddress.addEventListener(SWFAddressEvent.CHANGE, changeUrl);
SWFAddress.addEventListener(SWFAddressEvent.EXTERNAL_CHANGE, changeUrl);
var languagePrefix = '';
function changeUrl() {
	
	// per ajax link nachladen
	if (!fromNavigateTo) {
		navigateTo(SWFAddress.getValue());
	}
	// trigger aufheben
	fromNavigateTo = false;
 
}
$(document).observe("dom:loaded", function() {
	if (document.location.pathname !== "/" || document.location.hash !== "" || document.location.hash.length <= 4 || document.location.pathname == '/home') {
		if ($('navi_container') && swfobject.hasFlashPlayerVersion("8")){ 
			$("navi_container").setStyle({
				height : "394px"
			});
		}
	}
	$$("a").invoke("observe", "click", navigateTo);
});
var ajax_navi = '';

function html_entity_decode(str) {
	str = str.replace(/&amp;/g,"&");
	return str;
}
function navigateTo(url, fromFlash, q) {
	var e, elem = null;
	if (typeof url !== "string") {
		e=url;
		var elem = e.findElement("a");
		if (!elem || elem.target == '_blank') {
			return;
		}
		if ($("navi_html")) {
			$("navi_html").select("a").invoke("removeClassName", "active");	
		}
		
		elem.addClassName("active");
		url = elem.href.substr(firstPart.length);
		if(elem.target != '_self')
			e.stop();
	} else if (url.length == 3 && fromFlash) {
		SWFAddress.setValue('/');
		document.location.pathname = url;
	}	
	var params = {
			ajax : 1,
			font : fontSize, 
			ajaxKey : Math.round(Math.random()*1000)
		};
	if (typeof q !== "undefined") {
		params.q = q;
	}
	new Ajax.Request(url, {
		method : 'get',
		parameters : params,
		onSuccess : function(elem, data) {

			$('content').fade({ 
				duration: 0.5, from: 1, to:0.01,
				afterFinish : function(elem) {
					$("content").update(data.responseText);
					if ($("navi") && $("ajax_navi"))
						$("navi").update($("ajax_navi").innerHTML);
					$("content").select("a").invoke("observe", "click", navigateTo);
					if ($("ajax_title"))
						SWFAddress.setTitle(html_entity_decode($("ajax_title").innerHTML));
					fromNavigateTo = true;
					if ($("ajax_url"))
						url = $("ajax_url").innerHTML;
					$("meta_image_container").update($("ajax_meta_image").innerHTML).setStyle({'backgroundImage':  $("ajax_meta_image").style.backgroundImage});
					SWFAddress.setValue(url);
					ajax_navi = makeNavi();
					try {
						reloadFlash();
						if (url.length > 0)
							$("flashsite").navigateTo(url, ajax_navi);
						
					} catch(e) {
						
					}
					if (swfobject.hasFlashPlayerVersion("8")) {
						if (url !== '/' && url.length > 3 &&  !url.match(/.{0,3}\/home/)) {
							if (headersize) {
								$("navi_container").setStyle({height : '330px' });
								$('content').setStyle({top : '330px'});
							} else {
								$("navi_container").setStyle({height : '170px' });
								$('content').setStyle({top : '170px'});
							}
						} else {
							$("navi_container").setStyle({height : '394px'});
							$('content').setStyle({top : '330px'});
						}
					}
					
					$("content").appear({
						duration : 0.5, 
						from : 0, 
						to :1,
						afterFinish: function(elem) {
							if (typeof initCallback == 'function') {
								initCallback();
							}
						}.curry(elem)
					});
				}.curry(elem)
			});
		}.curry(elem)
	});
}
function reloadFlash(){
	try {
		if (!$('flashsite').makeNavi(ajax_navi)) {
			window.setTimeout(reloadFlash, 100);
		}
	}
	catch (e) {
		window.setTimeout(reloadFlash, 100);
	}
}
function makeNavi(navi) {
	var ajaxNaviElem = new Element('div');
	var naviObj = $("navi");
	if (!naviObj) {
		naviObj = $("ajax_navi");
	}
	if (!naviObj) {
		naviObj = Element(navi);
	}
	if (!naviObj) {
		return;
	}
	naviObj.select("li").each(function(a,li) {
		if (li.hasClassName("first")) {
			return;
		}
		var linkWrapper = Element('div');
		linkWrapper.insert({bottom : li.innerHTML});
		var link = linkWrapper.down();
		
		link.innerHTML = link.down().innerHTML;
		link.href = 'event:' + link.href.substr(firstPart.length);
		link.writeAttribute('id', false).writeAttribute('class', false);
		a.insert({bottom : link})
	}.curry(ajaxNaviElem));

	return html_entity_decode(ajaxNaviElem.innerHTML);
}

function log(message) {
	if (typeof (console) !== 'undefined')
		console.log(message);
}

function setFontSize(size) {
	fontSize = size;
	$(document.body).removeClassName("large").removeClassName("small").addClassName(size);
	resizeMenu("true");
}

function setSearch(searchTerm) {
	navigateTo(languagePrefix + "/search", null, searchTerm);
}

function resizeMenu(menuState) {
	if (headersize || $("navi_container").getHeight() == 404) { // on big header, do nothing
		return;
	}
	if (menuState == "true") { // menu open
		//$('flashsite').writeAttribute('wmode', 'transparent');
		if (fontSize == 'large') {
			$('navi_container').setStyle({  height : '295px' });
		} else {
			$('navi_container').setStyle({  height : '225px' });
		}
	} else {
		//$('flashsite').writeAttribute('wmode', 'window');
		$('navi_container').setStyle({  height : '170px' });

	}
}
var moveContentEffect = false;
var buttonEnabled = true;
var moveButtonEffect = false;
function setHeadersize(e) {
	headersize = !headersize;
	e.stop();
	if (!buttonEnabled) {
		return false;
	}
	setButtonStatus(false);
	setSessionHeadersize(headersize);
	if (headersize) { // big header
		$("navi_container").addClassName("full_header");
		
		moveContentEffect = new Effect.Morph('content', {
			style: 'top:330px', 
			afterFinish : function() {
				$('navi_container').setStyle({ height: '330px' });
				$('flashsite').setHeadersize(headersize);
				window.setTimeout(setButtonStatus.curry(true), 1000);
			}
		});
		moveButtonEffect = new Effect.Morph('headersizelink', {
			style: 'top:160px'
		});

	} else {
		$("navi_container").removeClassName("full_header");
		$('flashsite').setHeadersize(headersize);
		
		moveContentEffect = new Effect.Morph('content', {
			style: 'top: 170px',
			delay: 1,
			afterSetup : function() {
				$('navi_container').setStyle({ height: '170px' });
				
			},
			afterFinish : function() {
				setButtonStatus(true);
			}
		});
		moveButtonEffect = new Effect.Morph('headersizelink', {
			style: 'top:0px',
			delay : 1
		});
	}
	
}
function setSessionHeadersize(headersize) {
	new Ajax.Request('/sites/setHeadersize', {
		method : 'get',
		parameters : {
			'headersize' : headersize
		}	
	});
}
function setButtonStatus(status) {
	buttonEnabled = status;
	if (status) {
		if (!headersize) {
			$("headersize").select("a")[0].removeClassName("true").addClassName("false");
		} else {
			$("headersize").select("a")[0].removeClassName("false").addClassName("true");
		}
		
	}
}

