(function( $,window,document ){
	/* main functions */
	var adaptContainerSizes, fancySlider, backgroundler, hideFooter, showFooter, fancyAnimation, showActiveSection, lngChange, keyboardNav, playIntro, prepareForIntro;
	
	
	hideFooter = function($footer, timeOut){
		setTimeout(function(){
			$footer.stop( false,true ).css3animate( {"height": "2.4em"},300 );
		}, timeOut || 0);
	};
	
	showFooter = function($footer, timeBeforeHide){
		$footer.stop( false,true ).css3animate({"height": "6em"},300);
		if( timeBeforeHide ){
			setTimeout(function(){
				hideFooter( $footer,0 );
			}, timeBeforeHide);
		}
	};
	
	prepareForIntro = function(){
		$("#slideNavigation").find("a.nextSlide").css("right", "-40px").end()
							 .find("a.prevSlide").css("left", "-40px");
	};
	
	playIntro = function(timeOut){
		var $keynav = $("#keyboardNavigation");
		setTimeout(function(){
			hideFooter($("footer"), 1555);
			$("#container > header").find("h1").fadeIn(500).end().find("ul").delay(500).fadeIn(300);
			$("#slideNavigation").find("a.nextSlide").css3animate({"right": "0"}, 500).end()
							 	 .find("a.prevSlide").css3animate({"left": "0"}, 500);
			$("footer > div > *").fadeIn(300);
			
			
			setTimeout(function(){
				
				$keynav.css3animate({"opacity": "1"},800).delay(550).fadeOut(800);
				
						$keynav.find(".left").css3animate({"opacity": 1},150, function(){
							$keynav.find(".left").css3animate({"opacity": .5},150, function(){
								$keynav.find(".up").css3animate({"opacity": 1},150, function(){
									$keynav.find(".up").css3animate({"opacity": .5},150, function(){
										$keynav.find(".right").css3animate({"opacity": 1},150, function(){
											$keynav.find(".right").css3animate({"opacity": .5},150, function(){
												$keynav.find(".down").css3animate({"opacity": 1},150, function(){
													$keynav.find(".down").css3animate({"opacity": .5},150);
												});
											});
										});
									});
								});
							});
						});
				
			},2100); 
			
		},timeOut || 0);
	};
	
	fancyAnimation = function( $hovers,$footer ){
		$hovers.hover(
			function(){
				var $that = $(this);
				
				if( $that.is("footer") ){
					showFooter($footer)	;
				}
				
				$that.find("span:last").stop( false,true ).css3animate({"opacity": 0}, 300);
			},
			function(){
				var $that = $(this);
				
				if( $that.is("footer") ){
					hideFooter($footer,0)	;
				}
				
				$that.find("span:last").stop( false,true ).css3animate({"opacity": 1}, 300);
			}
		);
	};
	
	backgroundler = function( $images,refWidth,refHeight ){
		if( Modernizr.backgroundsize ) {
			$images.each(function(){
				var $that = $(this);
				$that.parent().parent().css({"background-image": "url(" + $that.remove().attr("src") + ")", 
											 "-webkit-background-size": "cover", 
											 "-moz-background-size": "cover", 
											 "-o-background-size": "cover",
											 "-ms-background-size": "cover",  
											 "background-size": "cover" });
			});
			
			return false;
		}else{
			var refRatio, $refImg, imgRatio, imgWidth, imgHeight;
			
			$refImg = $images.filter(":first");
			
			imgWidth = $refImg.width();
			imgHeight = $refImg.height();
			
			imgRatio = imgWidth / imgHeight;
			refRatio = refWidth / refHeight;
			
			if( imgRatio > refRatio ) {
				return $images.css({
					"height": "100%",
					"width": "auto",
					"margin": "0 0 0 " + ((refWidth - imgWidth) / 2) + "px"
				});
			}else{
				return $images.css({
					"height": "auto",
					"width": "100%",
					"margin": ((refHeight - imgHeight) / 2) + "px 0 0 0"
				});
			}
		}
	};
	
	showActiveSection = function( $slideToCome,forward,$links ){
		if( forward ){
			if( $slideToCome.is(":first-child") ){
				$links.removeClass("selected");
				$links.filter(":[href=#" + $slideToCome.closest("section").attr("id") + "]").addClass("selected");
				showFooter( $("footer"), 999 );
			}
		}else{
			if( $slideToCome.is(":last-child") ){
				$links.removeClass("selected");
				$links.filter(":[href=#" + $slideToCome.closest("section").attr("id") + "]").addClass("selected");
				showFooter( $("footer"), 999 );
			}
		}
	};
	
	fancySlider = function( $triggers,$slideContainer,$slideCategories,$slides,speed ) {
		var transitionDurations, determineSlidingSpeed, findNextSlide;
	
		//returns a speed in MS
		determineSlidingSpeed = function( $actualSlide,$slideToGoTo,multiplicator ) {
			var numberOfSlides = ( ($actualSlide.position().left - $slideToGoTo.position().left) / $actualSlide.width() );
			if( numberOfSlides < 0 ) {
				numberOfSlides = numberOfSlides * -1;
			}
			multiplicator =  numberOfSlides === 1 ? multiplicator : multiplicator / 2;
			return ( numberOfSlides * multiplicator );
		};
		
		
		findNextSlide = function( $actualSlide,direction ){
			var $slideToGoTo;
			if(direction === "next"){
				$slideToGoTo = $actualSlide.next();
				$slideToGoTo = $slideToGoTo.length ? $slideToGoTo : $actualSlide.closest("section").next().find("li:first-child");
				$slideToGoTo = $slideToGoTo.length ? $slideToGoTo : $actualSlide.closest("section").siblings().filter(":first").find("li:first");
			}else{
				$slideToGoTo = $actualSlide.prev();
				$slideToGoTo = $slideToGoTo.length ? $slideToGoTo : $actualSlide.closest("section").prev().find("li:last-child");
				$slideToGoTo = $slideToGoTo.length ? $slideToGoTo : $actualSlide.closest("section").siblings().filter(":last").find("li:last");
			}
			
			return $slideToGoTo;
		};
		
		$slides.eq(0).addClass("active");
		
		$triggers.click(function(e){
			var $that,link, $oToGoTo, coordsToGoTo,$activeSlide, slideSpeed;
			
			e.preventDefault();
			$that = $(this);
			link = $that.attr("href");
			$activeSlide = $slides.filter(".active");
			
			if(link === "#nextSlide"){
				$oToGoTo = findNextSlide( $slides.filter(".active"),"next" );
				slideSpeed = speed;
				showActiveSection( $oToGoTo,true,$triggers );
			}else if(link === "#prevSlide"){
				$oToGoTo = findNextSlide( $slides.filter(".active"),"prev" );
				slideSpeed = speed;
				showActiveSection( $oToGoTo,false,$triggers );
			}else{
				$oToGoTo = $("section" + link).find("li:eq(0)");
				slideSpeed = determineSlidingSpeed( $activeSlide,$oToGoTo,speed );
				$triggers.removeClass("selected");
				$that.addClass("selected");
			}
			
			coordsToGoTo = -1 * $oToGoTo.position().left;
			
			$activeSlide.removeClass("active");
			$oToGoTo.addClass("active");
			 
			callback = function(){ 
				
			};
			
			$slideContainer.css3animate( {"left": coordsToGoTo + "px"},slideSpeed,callback );
			
		});
	};
	
	adaptContainerSizes = function($container, $ref, $sliderContainer){
		var $allSlides, $sections, resize, $imgs, imgsLoaded, imagesStillInDOM;
		$sections = $container.find("section");
		$allSlides = $sections.find("li");		
		$imgs = $allSlides.find("img");
		imagesStillInDOM = true;
		
		resize = function(){
			var refWidth, refHeight, slidePosition;
			
			refWidth = $ref.width();
			refHeight = $ref.height(); 
			
			slidePosition = $allSlides.index( $allSlides.filter(".active") );
			slidePosition = slidePosition === -1 ? 0 : slidePosition;
			
			$container.width( $allSlides.length * refWidth );
			
			
			$sections.each(function(){
				var $that = $(this);
				$that.width( $that.find("li").length * refWidth );
			});
			
			$allSlides.width(refWidth);
			$allSlides.height(refHeight);
			$sliderContainer.css("left", -1 * (slidePosition * refWidth) + "px");
			
			if( imagesStillInDOM ) {
				imagesStillInDOM = backgroundler( $imgs,refWidth,refHeight );
			}
		};
		
		imgsLoaded = 0;
		$imgs.hide();
		
		$imgs.each(function(){
		  $("<img />",{src: $imgs.attr("src")}).load(function(){
			++imgsLoaded;
			if($imgs.length === imgsLoaded) {
				resize();
				$imgs.fadeIn(300);
				$(window).resize(function(){ resize(); });
				playIntro();
			};
		  });
		})
		
	};
	
	lngChange = function( $trigger, $contents ){
		$trigger.click(function(e){
			e.preventDefault();
			var $that,$parent, lng;
			
			$that = $(this);
			$parent = $that.parent();
			
			lng = $that.attr("href").split("#")[1];
			$parent.stop( false,true ).fadeOut(200, function(){
				$parent.siblings().eq(0).stop(false,true).fadeIn(200);
			});
			
			$contents.hide();
			$contents.filter("." + lng).fadeIn(400);

		});
	};
	
	keyboardNav = function( $document,$footer,$slideNav ){
		var keybindings = {
			"up"	: 38,
			"right"	: 39,
			"down"	: 40,
			"left"	: 37
		};

		$document.bind("keydown", function(e){
			var pressedKey = e.keyCode || e.which;
			
			switch (pressedKey) {
				case keybindings.up:
					showFooter($footer);
				break;
				case keybindings.down:
					hideFooter($footer);
				break;
				case keybindings.right:
					$slideNav.filter(".nextSlide").trigger("click");
				break;
				case keybindings.left:
					$slideNav.filter(".prevSlide").trigger("click");
				break;
			}
			
		});
	};
	
	$(function(){
		//dom reciycling
		var $stage, $slider, $sections, $footer;
		
		$stage		= $("div#stage");
		$slider		= $stage.find("div.slider");
		$sections	= $slider.find("sections");
		$footer		= $("footer");
		
		prepareForIntro();
		
		adaptContainerSizes($stage, $("div#content"), $slider);
		
		fancyAnimation( $("footer, a.prevSlide,a.nextSlide"), $footer );

		fancySlider( $("nav a, nav#slideNavigation a, header h1 a"),$slider,$sections,$slider.find("li"),400 );

		lngChange( $("header ul li a"), $("div.content div.en, div.content div.de, footer nav ul, footer section em") );

		keyboardNav( $(document), $footer, $("nav#slideNavigation a") );
	});

}( jQuery,window,document ));
