jQuery.easing['jswing']=jQuery.easing['swing'];jQuery.extend(jQuery.easing,{def:'easeOutQuad',swing:function(x,t,b,c,d){return jQuery.easing[jQuery.easing.def](x,t,b,c,d)},easeInQuad:function(x,t,b,c,d){return c*(t/=d)*t+b},easeOutQuad:function(x,t,b,c,d){return-c*(t/=d)*(t-2)+b},easeInOutQuad:function(x,t,b,c,d){if((t/=d/2)<1)return c/2*t*t+b;return-c/2*((--t)*(t-2)-1)+b},easeInCubic:function(x,t,b,c,d){return c*(t/=d)*t*t+b},easeOutCubic:function(x,t,b,c,d){return c*((t=t/d-1)*t*t+1)+b},easeInOutCubic:function(x,t,b,c,d){if((t/=d/2)<1)return c/2*t*t*t+b;return c/2*((t-=2)*t*t+2)+b},easeInQuart:function(x,t,b,c,d){return c*(t/=d)*t*t*t+b},easeOutQuart:function(x,t,b,c,d){return-c*((t=t/d-1)*t*t*t-1)+b},easeInOutQuart:function(x,t,b,c,d){if((t/=d/2)<1)return c/2*t*t*t*t+b;return-c/2*((t-=2)*t*t*t-2)+b},easeInQuint:function(x,t,b,c,d){return c*(t/=d)*t*t*t*t+b},easeOutQuint:function(x,t,b,c,d){return c*((t=t/d-1)*t*t*t*t+1)+b},easeInOutQuint:function(x,t,b,c,d){if((t/=d/2)<1)return c/2*t*t*t*t*t+b;return c/2*((t-=2)*t*t*t*t+2)+b},easeInSine:function(x,t,b,c,d){return-c*Math.cos(t/d*(Math.PI/2))+c+b},easeOutSine:function(x,t,b,c,d){return c*Math.sin(t/d*(Math.PI/2))+b},easeInOutSine:function(x,t,b,c,d){return-c/2*(Math.cos(Math.PI*t/d)-1)+b},easeInExpo:function(x,t,b,c,d){return(t==0)?b:c*Math.pow(2,10*(t/d-1))+b},easeOutExpo:function(x,t,b,c,d){return(t==d)?b+c:c*(-Math.pow(2,-10*t/d)+1)+b},easeInOutExpo:function(x,t,b,c,d){if(t==0)return b;if(t==d)return b+c;if((t/=d/2)<1)return c/2*Math.pow(2,10*(t-1))+b;return c/2*(-Math.pow(2,-10*--t)+2)+b},easeInCirc:function(x,t,b,c,d){return-c*(Math.sqrt(1-(t/=d)*t)-1)+b},easeOutCirc:function(x,t,b,c,d){return c*Math.sqrt(1-(t=t/d-1)*t)+b},easeInOutCirc:function(x,t,b,c,d){if((t/=d/2)<1)return-c/2*(Math.sqrt(1-t*t)-1)+b;return c/2*(Math.sqrt(1-(t-=2)*t)+1)+b},easeInElastic:function(x,t,b,c,d){var s=1.70158;var p=0;var a=c;if(t==0)return b;if((t/=d)==1)return b+c;if(!p)p=d*.3;if(a<Math.abs(c)){a=c;var s=p/4}else var s=p/(2*Math.PI)*Math.asin(c/a);return-(a*Math.pow(2,10*(t-=1))*Math.sin((t*d-s)*(2*Math.PI)/p))+b},easeOutElastic:function(x,t,b,c,d){var s=1.70158;var p=0;var a=c;if(t==0)return b;if((t/=d)==1)return b+c;if(!p)p=d*.3;if(a<Math.abs(c)){a=c;var s=p/4}else var s=p/(2*Math.PI)*Math.asin(c/a);return a*Math.pow(2,-10*t)*Math.sin((t*d-s)*(2*Math.PI)/p)+c+b},easeInOutElastic:function(x,t,b,c,d){var s=1.70158;var p=0;var a=c;if(t==0)return b;if((t/=d/2)==2)return b+c;if(!p)p=d*(.3*1.5);if(a<Math.abs(c)){a=c;var s=p/4}else var s=p/(2*Math.PI)*Math.asin(c/a);if(t<1)return-.5*(a*Math.pow(2,10*(t-=1))*Math.sin((t*d-s)*(2*Math.PI)/p))+b;return a*Math.pow(2,-10*(t-=1))*Math.sin((t*d-s)*(2*Math.PI)/p)*.5+c+b},easeInBack:function(x,t,b,c,d,s){if(s==undefined)s=1.70158;return c*(t/=d)*t*((s+1)*t-s)+b},easeOutBack:function(x,t,b,c,d,s){if(s==undefined)s=1.70158;return c*((t=t/d-1)*t*((s+1)*t+s)+1)+b},easeInOutBack:function(x,t,b,c,d,s){if(s==undefined)s=1.70158;if((t/=d/2)<1)return c/2*(t*t*(((s*=(1.525))+1)*t-s))+b;return c/2*((t-=2)*t*(((s*=(1.525))+1)*t+s)+2)+b},easeInBounce:function(x,t,b,c,d){return c-jQuery.easing.easeOutBounce(x,d-t,0,c,d)+b},easeOutBounce:function(x,t,b,c,d){if((t/=d)<(1/2.75)){return c*(7.5625*t*t)+b}else if(t<(2/2.75)){return c*(7.5625*(t-=(1.5/2.75))*t+.75)+b}else if(t<(2.5/2.75)){return c*(7.5625*(t-=(2.25/2.75))*t+.9375)+b}else{return c*(7.5625*(t-=(2.625/2.75))*t+.984375)+b}},easeInOutBounce:function(x,t,b,c,d){if(t<d/2)return jQuery.easing.easeInBounce(x,t*2,0,c,d)*.5+b;return jQuery.easing.easeOutBounce(x,t*2-d,0,c,d)*.5+c*.5+b}});


var isiPad = navigator.userAgent.match(/iPad/i) != null;


var FADE_TIMEOUT = 5000;
var FADE_SPEED = 1350;
var FADE_EASING = 'easeInOutQuad';

var CONTENT_FADE_SPEED = 1200;
var CONTENT_FADE_EASING = 'easeOutQuad';

var PRESENTATION_FADE_SPEED = 350;
var PRESENTATION_FADE_EASING = 'easeOutQuad';


var NAVI_FADE_SPEED = 250;
var NAVI_DARKEN_ALPHA = 0.75;


var $screenWidth = 0;
var $screenHeight = 0;
var $domWidth = 0;
var $domHeight = 0;
var $images = new Array();
var $imageID = 0;
var $slider;
var $firstImage;
var $secondImage;
var $imageHolder;
var $direction;
var $animating;
var TO;


function resizeBackgroundImage(){
	
	$targetImage = $(this);
	$targetImage.css({
		'position': 'absolute',
		'top': '0px',
		'left': '0px',
		'width': 'auto',
		'height': 'auto'
	});
	
	
	if (($screenWidth / $targetImage.width()) < ($screenHeight / $targetImage.height())) {
		$targetImage.css('height', $screenHeight + 'px');
	} else {
		$targetImage.css('width', $screenWidth + 'px');
	}
	
	$targetImage.css('left', 0 - ($targetImage.width() - $screenWidth) / 2 + 'px');
	$targetImage.css('bottom', 0 - ($targetImage.height() - $screenHeight) / 2 + 'px');
	
	return;
	if ($secondImage && $firstImage) {
		var left; var secondImageLeft;
		secondImageLeft = $secondImage.css('left');
		if (secondImageLeft == null) secondImageLeft = 0;
		else secondImageLeft = parseInt(secondImageLeft.replace('px', ''));;
		
		if ($direction < 0) left = secondImageLeft - $firstImage.width();
		else left = secondImageLeft + $firstImage.width();
		$secondImage.css('left', left + 'px');
	}
}

function resize(){
	$screenWidth = $(window).width();
	$screenHeight = $(window).height();
	
	$domWidth = $('#contentHolder').width();
	$domHeight = $('#contentHolder').height() + 107;
	if ($domWidth < $screenWidth) $domWidth = $screenWidth;
	if ($domHeight < $screenHeight) $domHeight = $screenHeight;
	
	resizeBackground();
	
	
	if ($.browser.webkit) {
		var i = 0;
		$('.content').each(function(){
			$(this).css({
				'position':'absolute',
				'display':'block',
				'left':($screenWidth - 650) / 2 + i * $screenWidth + 135 + 'px'
			});
			i++;
		});
		
		$('#contentHolder').css({
			'width': $screenWidth * $('.content').size(),
			'-webkit-transform': 'translate3d(' + (0 - $screenWidth * ContentID) + 'px,0px,0px)',
			'-webkit-perspective': '1000',
			'-webkit-backface-visibility': 'hidden',
		});
	}
}

function resizeBackground(){
	$('#background-slider img').each(resizeBackgroundImage);
	$('#backgroundTrigger').css({
		'width':$domWidth+'px',
		'height':$domHeight+'px',
	});
}

function storeImages(){
	$('#background-slider img').each(function(){
		$images.push($(this));
	});
}


function fade(direction){
	if ($('#background-slider').size() == 0) return;
	if ($animating) return;
	$animating = true;
	
	if (direction == null) direction = 1;
	$direction = direction;
	
	
	if (direction < 0){
		$secondImage = $images[prevID($imageID)];
		$imageID = prevID(prevID($imageID));
	}
	
	$imageHolder.prepend($secondImage);
	resizeBackground();
	
	$imageHolder.css('width', $firstImage.width() + $secondImage.width() + 'px');
	
	var $target = {};
	
	if (direction < 0) $target.left = '+' + $firstImage.width() + 'px';
	else $target.left = '-' + $firstImage.width() + 'px';
	
	//$imageHolder.animate($target, FADE_SPEED, FADE_EASING, nextImage);
	if ($.browser.webkit) {
		$firstImage.css({
			'-webkit-transition':'opacity '+(FADE_SPEED / 1000)+'s ease-in-out',
			'-webkit-transform': 'translate3d(0,0,0)',
			'-webkit-perspective': '1000',
			'-webkit-backface-visibility': 'hidden',
			'opacity': '0'
		});
		window.setTimeout(nextImage, FADE_SPEED);
	} else {
		$firstImage.fadeOut(FADE_SPEED, FADE_EASING, nextImage);
	}
}


	function nextID(id){
		if (id >= ($images.length - 1)) return 0;
		else return (id + 1);
	}
	
	function prevID(id){
		if ((id-1) < 0) return ($images.length - 1);
		else return (id - 1);
	}
	


function nextImage(){	
	$imageHolder.css({'left': '0', 'top': '0'});
	$('#background-slider img').remove();
	
	$imageID = nextID($imageID);
	
	if ($firstImage) {		
		if ($.browser.webkit) $firstImage.css('opacity','1');
		else $firstImage.fadeIn(0);
	}
	
	$firstImage = $images[$imageID];
	$secondImage = $images[nextID($imageID)];
	
	$imageHolder.append($firstImage);
	
	resizeBackground();
	
	$animating = false;
	
	window.clearTimeout(TO);
	TO = window.setTimeout(fade, FADE_TIMEOUT);
}


function startSlider(){
	$imageID = -1;
	storeImages();
	$('#background-slider img').remove();
	
	$slider = $('#background-slider');
	$imageHolder = $('#background-slider .holder');
	
	$slider.css('position', 'absolute');
	$imageHolder.css('position', 'absolute');
	
	nextImage();
}



function setupNavi()
{
	function highlight() { $(this).stop(true, true).fadeTo(NAVI_FADE_SPEED, 1); }
	function darken()    { $(this).stop(true, true).fadeTo(NAVI_FADE_SPEED, NAVI_DARKEN_ALPHA); }
	
	$('#navi li').each(function(){
		$(this).css('cursor', 'pointer');
		$(this).fadeTo(0, NAVI_DARKEN_ALPHA);
		if (!$.browser.webkit && !isiPad) $(this).hover(highlight, darken);
	});
}



function rasterize($target){
	var blendImg = new Image();
	blendImg.onload = initRaster;
	blendImg.src = BasePath+'images/grid.gif';
	
	function initRaster(){
		$target.pixastic('blend', {image:blendImg, mode:'multiply', amount:0.9});
	}
	
}


function getBackgroundImages(){
	getBackgroundImage($('#background-slider img').size());
	
	function getBackgroundImage(id){
		$.ajax({
			type: 'GET',
			url: BasePath + 'backend/action.php',
			data: 'action=get-image&type=small&id=' + id,
			success: function(data){
				if (data.toString().indexOf('image-end') != -1) {
					return;
				}
				
				$i = $(data);
				loadImage($i);
				
				if ($('#background-slider').size() == 0) return;
				else getBackgroundImage(id + 1);
			}
		});
	}
	
	/*
	$.ajax({
		type: 'GET',
		url: BasePath + 'backend/action.php',
		data: 'action=get-all-images',
		success: function(data){
			$imagelist = $(data).find('img');
			$images = new Array();
			$imagelist.each(function(){
				if ($(this).attr('src') != undefined) $images.push($(this));
				//$(this).load(function(){
					//$images.push($(this));
					//alert($images.length);
				//});
			});
		}
	});
	*/
	
}

function loadImage($i)
{
	$i.load(function(){
		$images.push($i);
	});
}






var animatingContent = false;
var $currentContent;


function fetchContent(location){
	if (animatingContent) return;
	animatingContent = true;
	
	var $content;
	var $oldContent;
	var $contentHolder = $('#contentHolder');
	var navigateBack = (location == '');
	
	location = location.replace('#/', '');
	
	function setNaviDisplay(){ $('#navi').css('display', (navigateBack ? 'block' : 'none')); }
	
	function replaceContent(){
		animatingContent = false;
		$currentContent = $content;
		
		if (!$.browser.webkit) {
			$oldContent.addClass('outerContent');
			$contentHolder.css('left', '0');
			$contentHolder.css('width', '100%');
			$content.css({'position':'relative', 'left':'135px'});
		}
		resize();
	}
	
	NewID = getContentID();
	
	$prevContent = $('.content:eq(' + (NewID - 1) + ')');
	$content = $('.content:eq(' + NewID + ')');
	$oldContent = $('.content:eq(' + ContentID + ')');
	
	var slideLeft = false;
	if (NewID < ContentID) slideLeft = true;
	if (ContentID == 0 && NewID == navigation.length - 1) slideLeft = true;
	if (ContentID == navigation.length - 1 && NewID == 0) slideLeft = false;
	
	$contentHolder.css('position', 'absolute');
	if ($.browser.webkit) {
		$contentHolder.css({
			'display':'block',
			'opacity': (navigateBack ? 0 : 1)
		});
	}
	
	$content.css('top', '107px');
	$content.removeClass('outerContent');
	
	if ($.browser.webkit) $('#navi').css({
		'-webkit-transition':'opacity '+(CONTENT_FADE_SPEED / 1000)+'s ease-in-out',
		'-webkit-transform': 'translate3d(0,0,0)',
		'-webkit-perspective': '1000',
		'-webkit-backface-visibility': 'hidden',
	});
	
	$('#navi').stop(true, true);
	
	if (navigateBack && !$.browser.webkit) $('#navi').delay(CONTENT_FADE_SPEED / 1.5);
	if ($.browser.webkit) {
		$('#navi').css({
			'-webkit-transition':'opacity '+(CONTENT_FADE_SPEED / 1000)+'s ease-in-out',
			'-webkit-transition-delay':(navigateBack ? (CONTENT_FADE_SPEED / 2000)+'s' : '0s'),
			'top':'98px',
			'opacity': (navigateBack ? 1 : 0)
		});
		if (!navigateBack) window.setTimeout(function(){$('#navi').css('top', '-5000px');}, (CONTENT_FADE_SPEED));
	}
	else {
		if (navigateBack) $('#navi').fadeTo(CONTENT_FADE_SPEED, 1, CONTENT_FADE_EASING, setNaviDisplay);
		else {
			$('#navi').fadeTo(0, 0, CONTENT_FADE_EASING, setNaviDisplay);
			$('#navi').css('display', 'none');
		}
	}
	
	if (!$.browser.webkit) {
		if (slideLeft) $content.css({'position': 'absolute', 'left': ($screenWidth - 650) / 2 - $screenWidth + 135 + 'px'});
		else $content.css({'position': 'absolute', 'left': ($screenWidth - 650) / 2 + $screenWidth + 135 + 'px'});
		
		$oldContent.css({'position': 'absolute', 'left': ($screenWidth - 650) / 2 + 135 + 'px'});
		$contentHolder.css('width', $screenWidth * 2 + 'px');
	}
	
	setSwipeableContent($content.find('.box'));
	replaceAnchors($content.find('a'));
	
	var $left;
	if (slideLeft) $left = '+'+$screenWidth+'px';
	else $left = '-'+$screenWidth+'px';
	
	if ($.browser.webkit)
	{
		var waitTime = 0;
		var targetX = (0 - $screenWidth * NewID);
		
		if (ContentID == null || ContentID == -1) {
			// FROM NAVI
			$prevContent.fadeOut(0).delay(CONTENT_FADE_SPEED * 1.25).fadeIn(0);
			$contentHolder.css({'-webkit-transform': 'translate3d(' + (0 - $screenWidth * (NewID - 1)) + 'px,0px,0px)','-webkit-transition': '0s ease-in-out'});
			waitTime = 50;
		}
		else if (NewID == -1) {
			// BACK TO NAVI
			targetX = (0 - $screenWidth * (ContentID-1));
			$oldContent.prev().fadeOut('0');
		}
		
		if (NewID != -1) $('#navi').css('display', 'none');
		
		window.setTimeout(function(){
			$contentHolder.css({
				'-webkit-transform': 'translate3d(' + targetX + 'px,0px,0px)',
				'-webkit-perspective': '1000',
				'-webkit-backface-visibility': 'hidden',
				'-webkit-transition': (CONTENT_FADE_SPEED / 1000) + 's ease-in-out'
			});	
		}, waitTime);
		
		window.setTimeout(replaceContent, CONTENT_FADE_SPEED + waitTime);
	} else {
		$contentHolder.animate({left:$left}, CONTENT_FADE_SPEED, CONTENT_FADE_EASING, replaceContent);
	}
	
	ContentID = NewID;
}





function getContents(id){
	var i = 0;
	$('#contents .content').each(function(){
		if (!navigation[i]['content']) navigation[i]['content'] = new Array();
		navigation[i]['content']['de'] = $(this);
		i++;
	});
	
	if ($.browser.webkit) {
		$('#contentHolder').css({
			'display':'none',
			'opacity':'0',
			'-webkit-transform': 'translate3d('+($screenWidth < 1024 ? 1024 : $screenWidth)+'px,0px,0px)',
			'-webkit-perspective': '1000',
			'-webkit-backface-visibility': 'hidden',
			'-webkit-transition': (CONTENT_FADE_SPEED / 1000) + 's ease-in-out'
		});
	}
}





function setSwipeableContent(target){
	$(target).swipe({swipeLeft:nextContent, swipeRight:prevContent, threshold:250, useMouse:true, triggerOnTouchEnd:false});
	
	var moveOptions = {};
	moveOptions.y = [107, 0 - $(target).height() - 214 + $screenHeight];
	moveOptions.target = $(target).parent();
	
	if ($.browser.webkit) moveOptions.target.css({
		'-webkit-transform': 'translate3d(0,0,0)',
		'-webkit-perspective': '1000',
		'-webkit-backface-visibility': 'hidden',
	});
	
	if ($(target).height() + 214 < $screenHeight) moveOptions.y = [107, 107];
	
	$(target).move(moveOptions);
}







function replaceAnchors(target){
	$(target).each(function(){
		var l = $(this).attr('href');
		// only internal links
		if(l.indexOf(BasePath) >= 0 || l.indexOf('/kontakt/') >= 0)
		{
			l = l.toString().replace(BasePath, '/');
			while(l.indexOf('#/') >= 0) l = l.replace('#/', '');
			while(l.indexOf('/#') >= 0) l = l.replace('/#', '');
			while(l.indexOf('../') >= 0) l = l.replace('../', '/');
			//if (l.length > 0) l = '/' + l;
			l = BasePath + '#' + l;
			$(this).attr('href', l);
		}
	});
}




var $presentationMode;
var $presentModeActive;

function setupPresentationMode(){
	$presentationMode = $('#presentation-mode');
	$presentationMode.css('cursor', 'pointer');
	$presentationMode.click(togglePresentation);
	
	$('#top-line-impressum').click(function(){
		if ($presentModeActive) $presentationMode.trigger('click');
	});
}

	function togglePresentation(e){
		e.preventDefault();
		
		$presentModeActive = false;
		
		if ($presentationMode.html().indexOf('AUS') >= 0){
			$presentationMode.html($presentationMode.html().replace('AUS', 'EIN'))
			$presentModeActive = true
		} else {
			$presentationMode.html($presentationMode.html().replace('EIN', 'AUS'))
		}
		
		$('#navi-wrapper').fadeTo(0, ($presentModeActive ? 0 : 1));
		$('#contentHolder').fadeTo(0, ($presentModeActive ? 0 : 1));
		$('#backgroundTrigger').css('background', ($presentModeActive ? 'none' : 'url(/images/grid.png) top left repeat'));
		$('#logo-text').fadeTo(0, ($presentModeActive ? 0 : 1));
	}






/* SWIPE OPTIONS */

var swipeOptions = {
	swipeLeft:swipeLeft,
	swipeRight:swipeRight,
	threshold:250,
	useMouse:true,
	triggerOnTouchEnd:false
}

var id = 0;



function setupSwipeable(){
	//$('#backgroundTrigger').swipe(swipeOptions);
	$('#backgroundTrigger').swipe({swipeLeft:nextContent, swipeRight:prevContent, threshold:250, useMouse:true, triggerOnTouchEnd:false});
	if ($.browser.webkit) $('#backgroundTrigger').css({
		'-webkit-transform': 'translate3d(0,0,0)',
		'-webkit-perspective': '1000',
		'-webkit-backface-visibility': 'hidden',
	});
}

	function swipeRight(){ fade(-1); }
	function swipeLeft(){ fade(1); }

	function nextContent() {
		if(getContentID() != -1 && !$presentModeActive) {
			//fade(1);
			setArea(navigation[nextContentID()]['area'][URLLang]);
		}
		else fade(1);
	}
	function prevContent() {
		if(getContentID() != -1 && !$presentModeActive) {
			//fade(1);
			setArea(navigation[prevContentID()]['area'][URLLang]);
		}
		else fade(-1);
	}

	





	function getContentID(){
		for (var i = 0; i < navigation.length; i++) {
			if (navigation[i]['area'][URLLang].toString().toLowerCase() == URLArea.toString().toLowerCase())
				return i;
		}
		
		return -1;
	}



	function nextContentID(){
		var id = getContentID();
		if (++id >= navigation.length) return (0);
		//if (++id >= navigation.length) return (navigation.length - 1);
		return id;
	}
	
	function prevContentID(){
		var id = getContentID();
		//if (--id < 0) return (0);
		if (--id < 0) return (navigation.length - 1);
		return id;
	}




function setArea(area){
	window.location.hash = '#/' + URLLang + '/' + area + '/';
}




var HCInt;
var storeHash;
var URLLang;
var URLArea;
var ContentID;
var NewID;

setURLVars(window.location.hash);

function setURLVars(hash){
	var URL = hash.split('/');
	if (URL[1])	URLLang = URL[1];
	else if (lang) URLLang = lang;
	else URLLang = 'de';
	
	if (URL[2]) URLArea = URL[2];
	else if (area) URLArea = area;
	else URLArea = '';
}

function checkHash(){
	if ($('#background-slider').size() == 0) return;
	if (!storeHash) storeHash = '';
	var hash = window.location.hash.toString();
	if (hash == '#/') hash = '';
	
	if (storeHash != hash && !animatingContent){
		storeHash = hash;
		setURLVars(hash);
		fetchContent(hash);
	}
	
	HCInt = window.setTimeout(checkHash, 150);
}





function ready(){
	var location = window.location.toString().replace(BasePath, '').replace(window.location.hash, '');
	while (location.indexOf('#/') > 0) location = location.replace('#/', '');
	if (location == '#') location = '';
	
	window.location.href = BasePath + '#/';
	
	$.browser.webkit = false;
	
	resize();
	getContents();
	replaceAnchors('a');
	setSwipeableContent('.content .box');
	checkHash();
	getBackgroundImages();
	setupNavi();
	setupSwipeable();
	setupPresentationMode();
	startSlider();
	resize();
	
	window.setTimeout(resize, 500);
	window.onorientationchange = function(){window.location.reload();}
}


$(document).ready(ready);
$(window).resize(resize);


