(function()
{
	$(document).ready(init);
	
	function init()
	{
		//$(window).bind("resize", windowResize);
		//windowResize();
		
		if($(".naviThird:not(.slideDownOnLoad)").length)
		{
			var temp = location.href.lastIndexOf("#scr_");
			var scrTop = 0;
			if(temp != -1)
				scrTop = parseInt(location.href.substring(temp+5));
			var scrollApi = $(".naviThird:not(.slideDownOnLoad)").jScrollPane({
				animateScroll: true,
				animateEase: "swing",
				animateDuration: 600
			}).data("jsp");
			if(scrTop > 0)
				scrollApi.scrollToY(scrTop, null);
		}
		
		$(window).load(load);
		
		onClickHrefHandler();
		
		showHandler();
	}
	
	function load()
	{
		$(".fadeInOnLoad").fadeIn(600, "swing",
			function()
			{
				$(this).addClass("active");
				$(".projectBoxMainElem.fadeInOnLoad").jScrollPane({
					animateScroll: true,
					animateEase: "swing",
					animateDuration: 600
				});
				$(".slideDownOnLoad").slideDown(600, "swing",
					function()
					{
						$(this).addClass("active");
						$(".naviThird.slideDownOnLoad").jScrollPane({
							animateScroll: true,
							animateEase: "swing",
							animateDuration: 600
						});
					}
				);
			}
		);
		
		if(!$(".fadeInOnLoad").length)
			$(".slideDownOnLoad").slideDown(600, "swing",
				function()
				{
					$(this).addClass("active");
					$(".naviThird.slideDownOnLoad").jScrollPane({
						animateScroll: true,
						animateEase: "swing",
						animateDuration: 600
					});
				}
			);
	}
	
	var wWidth = 0, wHeight = 0;
	function windowResize()
	{
		wWidth = $(window).width();
		wHeight = $(window).height();
	}
	
	var href;
	function onClickHrefHandler()
	{
		$(".naviFirst a").bind("click",
			function()
			{
				href = $(this).attr('href');
				$(".projectBoxMainElem.active").fadeOut(600, "swing");
				$(".projectBoxSide").fadeOut(600, "swing");
				$(".naviSecond").slideUp(600, "swing");
				$(".naviThird").slideUp(600, "swing");
				$(".teaserBox").fadeOut(600, "swing");
				$(".rightColTextBox > div").fadeOut(600, "swing");
				$(".twoSmallColTextBox > div").fadeOut(600, "swing");
				$(".twoColTextBox .slideDownOnLoad").slideUp(600, "swing");
				$(".twoColTextBox .fadeInOnLoad").fadeOut(600, "swing");
				setTimeout(onClickReady, 600);
				return false;
			}
		);
		$(".naviSecond a").bind("click",
			function()
			{
				href = $(this).attr('href');
				$(".projectBoxMainElem.active").fadeOut(600, "swing");
				$(".projectBoxSide").fadeOut(600, "swing");
				$(".naviThird").slideUp(600, "swing");
				$(".teaserBox").fadeOut(600, "swing");
				$(".rightColTextBox > div").fadeOut(600, "swing");
				$(".twoSmallColTextBox > div").fadeOut(600, "swing");
				$(".twoColTextBox .slideDownOnLoad").slideUp(600, "swing");
				$(".twoColTextBox .fadeInOnLoad").fadeOut(600, "swing");
				setTimeout(onClickReady, 600);
				return false;
			}
		);
		$(".naviThird a").bind("click",
			function()
			{
				href = $(this).attr('href') + "#scr_" + $(".naviThird .jspPane").css("top").replace("px", "").replace("-", "");
				$(".projectBoxMainElem.active").fadeOut(600, "swing");
				$(".projectBoxSide").fadeOut(600, "swing");
				$(".teaserBox").fadeOut(600, "swing");
				$(".twoColTextBox .slideDownOnLoad").slideUp(600, "swing");
				$(".twoColTextBox .fadeInOnLoad").fadeOut(600, "swing");
				setTimeout(onClickReady, 600);
				return false;
			}
		);
		
		$(".projectBoxSideNavi a").bind("click",
			function()
			{
				href = $(this).attr('href') + "#scr_" + $(".naviThird .jspPane").css("top").replace("px", "").replace("-", "");
				$(".projectBoxMainElem.active").fadeOut(600, "swing");
				setTimeout(onClickReady, 600);
				return false;
			}
		);
	}
	function onClickReady()
	{
		location.replace(href);
	}
	
	var loaded = false;
	var timeout = false;
	function showHandler()
	{
		var teaserbox = $(".teaserBox.show");
		if(!teaserbox.length)
			return;
		
		$(".teaserBox.show img.active").next("img").bind("load", nextImgOnLoad);
		loaded = true;
		setTimeout(currImgTimeout, 5000);
	}
	function nextImgOnLoad()
	{
		loaded = true;
		changeImgs();
	}
	function currImgTimeout()
	{
		timeout = true;
		changeImgs();
	}
	function changeImgs()
	{
		if(!loaded || !timeout)
			return;
		
		var img = $(".teaserBox.show img.active");
		var nextImg = img.next("img");
		img.fadeOut(2000,
			function()
			{
				img.remove();
			}
		);
		nextImg.fadeIn(2000,
			function()
			{
				nextImg.addClass("active");
				var next = parseInt(nextImg.attr('src').replace("/res/img/show/", "").replace(".jpg", ""))+1;
				if(next > 22)
					next = 1;
				$('<img src="/res/img/show/' + next + '.jpg" />').appendTo(nextImg.parent()).bind("load", nextImgOnLoad);
				setTimeout(currImgTimeout, 5000);
				loaded = false;
				timeout = false;
			}
		);
	}

})();
