$(function()
{
	////// Scrollpane //////

	$('.scrollable').jScrollPane({scrollbarWidth: 17, dragMinHeight: 32, dragMaxHeight: 32});
	$('.info-close').click(
		function () {
			if($('#info').css("right") == '0px') {
				$('#info').animate({"right": "-346px", "duration": "slow", "easing": "easeOutQuint"});
			}
			else {
				$('#info').animate({"right": "0px", "duration": "slow", "easing": "easeOutQuint"});
			}
		}
	)
	$('.teilnahme-close').click(
		function () {
			//$('#teilnahmelayer').css({display:"none"});
			$('#teilnahmelayer').animate({"height": "0px", "duration": "slow", "easing": "easeOutQuint"});
		}
	)
	$('.teilnahme-open').click(
		function () {
			//$('#teilnahmelayer').css({display:"block"});
			$('#teilnahmelayer').animate({"height": "380px", "duration": "slow", "easing": "easeOutQuint"});
		}
	)


	////// Main navigation //////

	var newClassName;
	var currentCarousel;

	//configure carousel for subnavigations
	$("#subnavi table").each(
		function () {
			if ($(this).find("li").size() > 8) {
				$(this).find("ul").addClass("jcarousel jcarousel-skin-tango");
				jQuery('.jcarousel').jcarousel({
    				buttonPrevEvent: "click",
    				buttonNextEvent: "click",
    				scroll: 1,
    				initCallback: carouselCallback
				});
			}
			if ($(this).hasClass("hidden")) {
				$(this).removeClass("hidden");
				$(this).css({display:"none"});
			}			
		}
	);

	function carouselCallback (carousel, state) {
		if (state == 'init') {
        	currentCarousel = carousel;
        }
    };

	function resetCarousel () {
	    currentCarousel.reset();
	}

	initMainNavi();
	
	// > Navigation Logic / main //

	function initMainNavi() {
		$("#header li a.nav-inactive, #header li a.nav-inactive-highlight").hover(
			function () {
				var item = $(this).find("span").attr("class");
				chooseNaviContent(item);
				$(this).addClass("nav-active");
				//only open if closed
				if (!$(this).hasClass("opened")) {
					openSubNavi($(this));
				}
	    	},
			function () {}
		);
	};

	$("#header li a.nav-active-opened").each(
		function () {
			openSubNavi($(this));
		}
	);

	$("#subnavi li").click(
		function(){
			var item = $(this).find("span").attr("class");
			var text = $(this).find("span").text();
			if (chooseNaviContent(item)) {
				$("#header ul li").find("a.opened").parent().nextAll().remove();
				$("#header ul li").find("a.nav-inactive-highlight").removeClass("nav-inactive-highlight");
				if ($(this).find("a").hasClass("last-crumb")) {
					$("#header ul").append('<li><a class=\"active\" href=\"#\"><span class=\"' + newClassName + '\">' + text + '</span></a></li>');
				}
				else {
					$("#header ul").append('<li><a class=\"nav-inactive-highlight\" href=\"#\"><span class=\"' + newClassName + '\">' + text + '</span></a></li>');
				}
				initMainNavi();
				closeSubNavi($(this));
			}
		}
	);

	function chooseNaviContent ($item) {
		var naviAvailable = false;
		$("#subnavi table").each(
			function () {
				if ($(this).hasClass($item)) {
					$(this).css({display:""});
					newClassName = $(this).attr("class");
					naviAvailable = true;
				}
			}
		);
		if (naviAvailable) {
			$("#subnavi table").each(
				function () {
					if (!$(this).hasClass($item)) {
						$(this).css({display:"none"});
					}
				}
			);
			return true;
		}
		else {
			return false;
		}
	};

	function openSubNavi ($this) {
		var item = $this.find("span").attr("class");
		
		if (chooseNaviContent(item)) {
			$("#subnavi").animate({
				"top": "-38px",
				"duration": "fast",
				"easing": "easeOutExpo"
			}, function(){
				$this.addClass("opened");
				$("#subnavi").bind("mouseleave", function(){
					$("#subnavi").bind("mouseleave", function(){
						return false;
					})
					closeSubNavi($this);
				});
			});
		}
	};

	function closeSubNavi ($this) {
		$("#header li").find("a.nav-active").removeClass("nav-active").removeClass("opened").addClass("nav-inactive");
		$("#subnavi").animate({"top": "-169px", "duration": "fast", "easing": "easeOutExpo"},
			function() {
				$this.removeClass("opened");
			}
		);
	};
	
	////// Subnavigation //////
	
	// > Hover-Effects //
	$("#subnavi li").hover(
		function () {
			$(this).colorBlend([{
				fromColor: "#303030",
				toColor: "#cb1337",
				param: "border-color",
				cycles: "0.5"
			}]);
			$(this).find("span").colorBlend([{
				fromColor: "#e7e7e7",
				toColor: "#cb1337",
				param: "color",
				cycles: "0.5"
			}]);	
		},
		function () {
			$(this).colorBlend([{
				fromColor: "#cb1337",
				toColor: "#303030",
				param: "border-color",
				cycles: "0.5"
			}]);
			$(this).find("span").colorBlend([{
				fromColor: "#cb1337",
				toColor: "#e7e7e7",
				param: "color",
				cycles: "0.5"
			}]);	
		}
	);
});