

(function($) {
	
	var left = 0;
	var duration = 1500;
	var sliderItemWidth = 414;
	var actItem = 0;
	
	var ua = navigator.userAgent;
	var checker = {
		ipad: ua.match(/(iPad)/),
		iphone: ua.match(/(iPhone|iPod|iPad)/),
		blackberry: ua.match(/BlackBerry/),
		android: ua.match(/Android/)
	};
	/*
	var timerId = null;    
	var freeze = false;
	var timeout = 3000;
	
	function doFreeze() {
		
		freeze = true;
		$('div#freeze').fadeOut('slow');
		$('iframe').fadeIn('fast');
		$('#sectionNav li.right').fadeIn('fast');
		clearTimeout(timerId);

		timerId = setTimeout(function(){
			$('.part_left_1-3.sliderItem iframe').fadeOut('fast');
			$('#sectionNav li.right').fadeOut('fast');
			$('div#freeze').fadeIn(2000);
			timeout += 1000;
			//$('div#freeze').bind('mousemove mousedown mouseup touchmove touchstart touchend scroll', function() {doFreeze();});
		}, timeout);
		if($.browser.msie) {
			$(document).unbind('mousemove');
		}
	}
	
	$(document).bind('mousemove mousedown mouseup touchmove touchstart touchend scroll keydown', function(){
		doFreeze();	
	});
	*/
	$().ready(function() {
		/*
		if(checker.ipad || checker.iphone) {
			$('div#freeze').css("background", "url('img/freeze_ipad.jpg')");
		}
		
		if(!freeze) {
			timerId = setTimeout(function(){
				$('iframe').fadeOut('fast');
				$('div#freeze').fadeIn(2000);
			}, timeout/2);
		}
		*/
		setMenuHeight();
		
		if((checker.iphone || checker.android || checker.blackberry) && $(document).width() > 600) {
			
			$(".itemMask").touchwipe({
				 wipeLeft: function() { 
					slideRight();
				},
				 wipeRight: function() {
					slideLeft();
				 }
			});
		}
		
		
		$(".various").fancybox({
			width	: 710,
			height	: 430,
			maxWidth	: 710,
			maxHeight	: 430,
			padding	: 10,
			fitToView	: false,
			closeBtn	: false,
			arrows		: true,
			autoSize	: false,
			closeClick	: true,
			loop		: false,
			openEffect	: 'none',
			closeEffect	: 'none',
			nextEffect	: 'fade',
			prevEffect	: 'fade',
			keys: {
				next: [39],//next: [13, 32, 34, 39, 40], // enter, space, page down, right arrow, down arrow
				prev: [37],//prev: [8, 33, 37, 38], // backspace, page up, left arrow, up arrow
				close: [27, 40]//close: [27] // escape key
			}
		});
		
		$(".imageLink").fancybox({
			width	: 710,
			height	: 430,
			maxWidth	: 710,
			maxHeight	: 430,
			padding	: 10,
			fitToView	: true,
			closeBtn	: false,
			arrows		: true,
			autoSize	: false,
			closeClick	: true,
			loop		: false,
			openEffect	: 'none',
			closeEffect	: 'none',
			nextEffect	: 'fade',
			prevEffect	: 'fade',
			keys: {
				next: [39],//next: [13, 32, 34, 39, 40], // enter, space, page down, right arrow, down arrow
				prev: [37],//prev: [8, 33, 37, 38], // backspace, page up, left arrow, up arrow
				close: [27, 40]//close: [27] // escape key
			}
		});
		
		var docwidth = $(window).width();
		var windowwidth = $(document).width();
		
		$("#sectionNav .act").width(docwidth - 29 - 29);
		$("#sectionNav #red.act").width(docwidth - 28 - 29 - 29);
		
		$(window).resize(function() {
			docwidth = $(window).width();
			windowwidth = $(document).width();
			$("#sectionNav .act").width(docwidth - 29 - 29);
			$("#sectionNav #red.act").width(docwidth - 28 - 29 - 29);
		});

		$("#sectionNav li#green").click(function(event){
			
			//pageTracker._trackPageview("/Referenzen");
			
			checkPrevent(event);
			
			$(this).addClass("act");
			
			if($(this).hasClass("left")) {
				$(this).width(docwidth - 29 - 29);
				
				$("#sectionNav li#blue.left").animate({
					left: docwidth - 29 - 29
				}, duration, function() {
					$(this).css("right", 29);
					$(this).css("width", 29);
					$(this).css("left", "inherit");
					$(this).attr("class", "right");
					resetSliderPos();
				});			

				$("#sectionNav li#red.left:not(:animated)").animate({
					left: docwidth - 29
				}, duration, function() {
					$(this).css("right", 0);
					$(this).css("width", 29);
					$(this).css("left", "inherit");
					$(this).attr("class", "right");
					resetSliderPos();
				});	
				
				setMenuHeight();
			}
		});
		
		$("#sectionNav li#blue").click(function(event){
			
			//pageTracker._trackPageview("/News");
			
			checkPrevent(event);
			
			if($(this).hasClass("left")) {
				$(this).addClass("act")
				$(this).width(docwidth - 28 - 29);
				
				$("#sectionNav li#red.left:not(:animated)").animate({
					left: docwidth - 29
				}, duration, function() {
					$(this).css("right", 0);
					$(this).css("width", 29);
					$(this).css("left", "inherit");
					$(this).attr("class", "right");
					resetSliderPos();
				});	
				
			}
			
			if($(this).hasClass("right")) {
				
				$(this).addClass("act");
				
				$("#sectionNav li#green.act").animate({
					width: 28
				}, duration, function(){
					$(this).removeClass("act")
				});
				
				
				$(this).width(docwidth - 28 - 29);
				$(this).css("left", docwidth - 29 - 29);
				$(this).animate({
					left: 28
				}, duration, function() {
					$(this).css("right", "inherit");
					$(this).attr("class", "left act");
					resetSliderPos();
				});
			}
			
			setMenuHeight();
			
		});
		
	
		$("#sectionNav li#red").click(function(event){
			
			//pageTracker._trackPageview("/Screenteam");
			
			checkPrevent(event);
			
			if($(this).hasClass("right")) {
				
				$(this).addClass("act");
				
				$("#sectionNav li#green.act").animate({
					width: 28
				}, duration, function(){
					$(this).removeClass("act")
				});
				
				$("#sectionNav li#blue.right").animate({
					right: docwidth - 28 - 29
				}, duration, function() {
					$(this).css("right", "inherit");
					$(this).css("left", 28);
					$(this).css("width", 29);
					$(this).attr("class", "left");
				});
				
				$(this).width(docwidth - 29 - 29 - 28);
				$(this).css("left", docwidth - 29);
				$(this).animate({
					left: 57
				}, duration, function() {
					$(this).css("right", "inherit");
					$(this).attr("class", "left act");
					//$("#red .page").show();
					$("#sectionNav li#blue.left").removeClass("act");
					$("#sectionNav li#blue.left").css("width", 29);
					resetSliderPos();
				});	
				
				setMenuHeight();
			}
		});	
		
		
		$("#keyboard .up").click(function() {
			$("li.act a.various").eq(actItem).click();
		});
		
		$("#keyboard .down").click(function() {
			
		});
		
		$("#keyboard .right").click(function() {
			slideRight();
		});
		
		$("#keyboard .left").click(function() {
			slideLeft();
		});
		
	   
		$(document).keydown(function(event) {
			if(event.keyCode == 37) {
				//left
				slideLeft();
			}
			
			if(event.keyCode == 38) {
				$("li.act a.various").eq(actItem).click();
			}
			
			if(event.keyCode == 39) {
				//right
				slideRight();
			}
		});
		
	});
	
	function slideRight() {
		
		getSliderWidth();
		
		if(left - sliderItemWidth > -$("li.act .itemMask").width()) {
			if($("a.various").size() >= actItem) {
				actItem++;
			}
			left = left - sliderItemWidth;
			contentSlide();
		}
	}
	
	function slideLeft() {
		
		getSliderWidth
		
		if(left < 0) {
			if(actItem > 0) {
				actItem--;
			}
			left = left + sliderItemWidth;
			contentSlide();
		}
	}
	
	function contentSlide() {
		
		if($("li.act .itemMask").width() == 20000) {
			if($("li.act .itemMask")) {
				$("li.act .itemMask").width(((sliderItemWidth) * $("li.act .sliderItem").size()));
			}
		}
		
		if(left <= 0 && left > -$("li.act .itemMask").width()) {
			$("li.act .itemMask").animate({
				left: left
			}, duration-1000, function() { });
		}
	}
	
	function getSliderWidth() {
		var item = $("li.act .itemMask .sliderItem");
		sliderItemWidth = $("li.act .itemMask .sliderItem").outerWidth();
		if(item.hasClass("box_article") || item.hasClass("part_left_1-3")) {
			if($.browser.msie && item.hasClass("box_article")) {
				sliderItemWidth += 6;
			} else {
				sliderItemWidth += 20;
			}
		}
	}
	
	function resetSliderPos() {		
		actItem = 0;
		left = 0;
		$(".itemMask").css("left", 0);
	}
	
	function checkPrevent(event) {
		var target = event.target + "";
		var parts = target.split("/");
		if(parts[parts.length-1] == "Referenzen" || parts[parts.length-1] == "News" || parts[parts.length-1] == "Screenteam") {
			event.preventDefault();
		}
	}
	
	function setMenuHeight() {
		if($(document).width() < 600) {
			$("#site").height($("ul#sectionNav li.act .page").height() + 20);
			$("ul#sectionNav > li").height($("ul#sectionNav li.act .page").height() + 20);
		}
	}
	
})(jQuery);
