$(document).ready(function(){
	
	// NAVIGATION
	$("#navigation li.level1-nav").hover(
		function() {
			$("#navigation ul.level2-nav").hide();
			$(this).children("ul").show();
		},
		function() {
			$(this).children("ul").show();
		}
	);
	// always switch back to showing subnav of active section when leaving nav
	$("#navigation").bind("mouseleave",function(){
		$("#navigation ul.level2-nav").hide();
		$(this).find(".active").children("ul").show();
	});
	
	// TEASERBOXES
	// Link on whole box
	$("div.teaser-box").click(function() {
		location.href = $(this).find("a").attr("href");
	});
	
	// TEASER CARROUSEL
	$("div#teaser-boxes").smoothDivScroll({	scrollingSpeed: 5, 
											mouseDownSpeedBooster: 4, 
											autoScrollDirection: "endlessloop", 
											autoScrollSpeed: 2, 
											visibleHotSpots: "always",
											startAtElementId: "current-teaser-box",
											hotSpotsVisibleTime: 9 });
	

	// TABBED CONTAINERS
	$("div.tabbed-container div.tabbed-container-item").slice(1).hide(); // hide all but first
	
	//initial handling
	$("div.tabbed-container").each(function(){
		set_background_image(0);
		var first_item = $("div.tabbed-container div.tabbed-container-item:first");
		handle_tab_resize(first_item);
		handle_product_links(first_item);
	});
	
	//on click
	$("div.tabbed-container .tabbed-container-nav li").click(function() {
		
		nav_item_position = $(this).prevAll().length;
		
		set_background_image(nav_item_position);
		
		$("div.tabbed-container .tabbed-container-nav li").removeClass("active");
		$(this).addClass("active");
		
		var selected_tab_item = $("div.tabbed-container div.tabbed-container-item").hide().slice(nav_item_position, nav_item_position + 1);
		
		handle_tab_resize(selected_tab_item);
		handle_product_links(selected_tab_item);
		selected_tab_item.show();
	});
	
	//sometimes the background image is different for each tab. if so, this var is defined in implementing template
	function set_background_image(nav_item_position) {
		if(typeof(tab_background_images) != "undefined") {
			$(".page-container").css("background", "#ffffff url('/images/content/" + tab_background_images[nav_item_position] + "') no-repeat");
		}
	}
	function handle_tab_resize(selected_tab_item) {
		if(selected_tab_item.hasClass("full")) {
			$(".page-container").addClass("no-teasers");
			$("#teaser-boxes").hide();
		} 
		else {
			$(".page-container").removeClass("no-teasers");
			$("#teaser-boxes").show();
		}
	}
	function handle_product_links(selected_tab_item) {
		if(selected_tab_item.hasClass("no-links")) {
			$(".product-link").hide();
		} 
		else {
			$(".product-link").show();
		}
	}
	
	
	// PRODUCT LINKS
	$("a.product-link").hover(
	  function () {
	    $(this).find("img").attr("src", "/images/site/icons/arrow_up_orange.png");
		$(this).addClass("hover");
	  },
	  function () {
	    $(this).find("img").attr("src", "/images/site/icons/arrow_up.png");
		$(this).removeClass("hover");
	  }
	);
});
