function html_entity_decode(str){
	var tarea=$('<textarea />');
	tarea.html(str);
	str = tarea.val()
	tarea.remove();
	return str;
}

$.fn.prepare = function(){
	Cufon.replace($(this).find('h1'),{fontFamily:'bdcn'});
	$(this).find('a[href$=pdf]').attr('target','_blank');
	$(this).find('a[href^=/]').click(function(){
		//if($(this).attr('href').substr(-3)!='pdf'){
			$.historyLoad($(this).attr('href'));
			return false;
		//}
	});
	return this;
}
/*function getFlashMovie(movieName) {
	return ($.browser.msie) ? window[movieName] : document[movieName];
}*/


function getFlashMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}

var oldParentURL = '';
function loadPage(url){
	var parentURL =  (url.replace(/^\//g, "")).split('/')[0];
	var helpURL = parentURL;
	mustchangesubnavi = parentURL!=oldParentURL;

	$('#navigation_sub li a').removeClass('active');

	if($('#navigation_main a[href=/'+parentURL+']').length==0) helpURL = '';
	$('#navigation_main a').removeClass('active');
	$('#navigation_main a[href=/'+helpURL+']').addClass('active');

	if($('#navigation_main a[href=/'+parentURL+']').length==0) parentURL = 'areal-gmbh';

	$('#holder>.content')
		.css('height',400)
		.addClass('loading')
		.empty();

	oldParentURL = parentURL;
	var oldURL = url;
$.ajax({
	url:url,
	type:'GET',
	dataType: 'html',
	complete:function(res,status){
		if(status=='error'){
			window.location = url;
			return;
		}
		var title = $.trim(res.responseText.replace( /([\s\S]+)<title>/, '' ).replace(/<\/title>([\s\S]+)/, '' ));
		document.title = html_entity_decode(title);
		var helper = $('<div />').append(res.responseText.replace(/<script(.|\s)*?\/script>/g, ""));
		if(!$.browser.msie)
			getFlashMovie('headerflash').hello($(helper).find('#header').css('background-image').replace(/^url|[\("\")]/g, ''));
		$('#holder>.content')
			.css('height','')
			.removeClass('loading')
			.html($(helper)
			.find('#holder>.content').html())
			.prepare();
			if(mustchangesubnavi){
				$('#navigation_sub>ul:first').hide().html($(helper).find('#navigation_sub>ul:first').html());
				$('#navigation_sub>ul:first a').each(function(i,e){
					$(e).attr('href',$(e).attr('href').replace('http://'+window.location.hostname,''));
				});
				$('#navigation_sub>ul:first').prepare().fadeIn();
				$('#navigation_sub ul ul').hide();
			}
			$('#navigation_sub li a[href='+url+']').addClass('active');
			$('#navigation_sub li').removeClass('open');
			$('#navigation_sub li a[href='+url+']').parents('li').addClass('open');
			$('#navigation_sub ul li:not(.open) ul').slideUp();
			$('#navigation_sub li.open').children('ul').slideDown();
	}
});
}
$(document).ready(function(){
	$('body').prepare();
	if(window.location.pathname!='/'){
		window.location = '/#'+window.location.pathname;
		return;
	}
	$.historyInit(loadPage,"/");
	
	Cufon.replace($('#subnavi a'),{fontFamily:'ltcn'});
	
	var appent = $('#navigation_main li.col.navi').width()+$('#navigation_main li.col.sidebar').width()
	var append = Math.floor(($('#navigation_main').width()-appent)/$('#navigation_main li:not(.col)').length);
	$('#navigation_main li').each(function(i,e){
			if(!$(e).is('.col')){
				$(e).css({width:append});
				appent += append;
			}
			Cufon.replace($(e).find('.title'),{fontFamily:'bdcn'});
			Cufon.replace($(e).find('.subline'),{fontFamily:'ltcn'});
			$(e).css({position:'relative'});
			$(e).prepend($('<div />').css({
				position: 'absolute',
				height:$(e).height(),
				width:$(e).width(),
				opacity:0	
			})
			.addClass('hoverer')
			);
			$(e).mouseover(function(){
				$(this).find('.hoverer').animate({opacity:1},{queue:false});	
			}).mouseout(function(){
				$(this).find('.hoverer').animate({opacity:0},{queue:false});	
			});
	});
	var missing = $('#navigation_main').width()-appent;
	$('#navigation_main li:nth-child('+($('#navigation_main li').length-1)+')').css({width:append+missing});
	if($.hasFlashPlayerVersion(10)){	
		$('#header').flash({
				swf: '/swf/areal_header.swf',
				width: '100%',
				height: '100%',
				name:'headerflash',
				flashvars: {
					initialPic:$('#header').css('background-image').replace(/^url|[\("\")]/g, '')
				}
		});
	}
	var h = 0;
	$('#proteasers li').each(function(i,e){
		if($(e).height()>h) h=$(e).height();
		$(e).css({position:'absolute',top:0,left:0});
		if(i>0) $(e).hide();
	});
	$('#proteasers').css({position:'relative',height:h});
	$('#proteasers li').css({height:h});
	var tint;
	rint();
	$('#proteasers').mouseover(function(){
		clearInterval(tint)
	});
	$('#proteasers').mouseout(function(){
		rint();
	});
	function rint(){
		tint = setInterval(function(){
			$('#proteasers').append(
				$('#proteasers li:first-child')
					.hide()
					.fadeIn('slow')
			);
		},5000);
	}
});
		

