/**
* Norrlandspall
* Author: Eastwest Jan Östlund, janne@eastwest.se, 2009
*/
$(document).ready(function()
{
	
	// Reset navigation when moving outside the navigation area
	$("#nav-area").hover(function () 
	{
       // no navigation, do nothing
	}, 
    function () 
	{
		$("#navbar img").hide();
		
		// moving out of navigation area, reset
        if($.config.current_id != 'm' + $.config.parent_id)
		{
			show_subs('m' + $.config.parent_id);
		}
	})
	
	// ---------------------------------------------------------------------
	// MAIN NAVBAR
	// ---------------------------------------------------------------------	

	$("ul#menu-items a").hoverIntent(makeTall,makeShort);
	
	// On hover main navbar
	function makeTall()
	{
		var id = $(this).parent().attr("id");
		
		
		//console.log(id);
		show_subs(id);
	}
	
	// On out
	function makeShort()
	{
	  // ...do nothing. Resetting via nav-area
	}
	
	function show_subs(id)
	{
		$.config.current_id = id;
		
		var total = $("#subnav-" + id + " li").size();
		
		// if this branch has children and is hidden, show it
		if(total > 0)
		{
			
			if ($("#navbar").is(":hidden")) 
			{
				$("#navbar").slideDown("slow");
      		}
		}
		// if not, hide cursor
		else
		{
			$("#navbar img").hide();
		} 
		
		// Remove cursor just in case
		$("#navbar img").hide("slow");
		
		// Remove all hover
		$("ul#menu-items li").removeClass("hover");
		
		// Add hover to current, if it's not the parent
		if('m' + $.config.parent_id != id)
		{
			$("ul#menu-items li#" + id ).addClass("hover");
		}
		// Hide all navbar items
		$(".navbar-items").hide();
		
		// Show current
		$("#subnav-" + id).fadeIn("slow");
		//console.log('fading in: ' + "#subnav-" + id)
		
		
	}
		
	// ---------------------------------------------------------------------
	// SUBS NAV
	// ---------------------------------------------------------------------
	
	// Hide on default
	$("#navbar").slideDown("slow");
	
	// Hide cursor
	$("#navbar img").hide();
	
	// On over link
	$(".navbar-items li a").hover(function(e){
		
		// Calculate the position of the link
		var p = $(this);
		var position = p.position(); 
		
		// Get the width minus padding
	 	var padding = (($(this).width() / 2) - 5); 
		
		// Show cursor
		$("#navbar img").show();
	
		// Move cursor to the closes link
		$('#navbar img').animate({left: position.left + padding}, 200, "swing");
   	},
	// On out
	function () {
   		// .. do nothing
	}
   ); 
	
	
	// Köp pallar
	$("#salja-btn").hover(function()
	{
		$(this).attr('src', $.config.sitespecific + '/images/salja_hover_btn.png'); 
		
	},
	function () 
	{
		$(this).attr('src', $.config.sitespecific + '/images/salja_btn.png');
    });
	
	
	// ---------------------------------------------------------------------
	// PAGE FOOTER ROLLOVER 
	// ---------------------------------------------------------------------
	
	// Köp pallar
	$(".footer-1").hover(function()
	{
		$(this).addClass("footer-1-hover");
	},
	function () 
	{
		$(this).removeClass("footer-1-hover");
    });
	
	// Sälj pallar
	$(".footer-2").hover(function()
	{
		$(this).addClass("footer-2-hover");
	},
	function () 
	{
    	$(this).removeClass("footer-2-hover");
    });
	  
	  // Värmebehandlad pall
	$(".footer-3").hover(function()
	{
		$(this).addClass("footer-3-hover");
	},
	function () 
	{
    	$(this).removeClass("footer-3-hover");
    });  
	
	// Link the whole div
	$("#footer-list li").click(function()
	{
		window.location=$(this).find("a").attr("href");return false;
	});
});	
	

/**
* Set a cursor to current sublevel
*
* @param int id current levels id
*
*/
function select_subnav(id)
{
	
	$('#m' + id).addClass("selected-menu");
	$.config.parent_id = id;
	
	if ( $("#subnav-m" + id).length )
	{
		$("#navbar").slideDown("slow");
		$("#subnav-m" + id).show("fast");
	
		var p = $("#subnav-m" + id);
	 	var position = p.position();
	 	var padding = $("#subnav-m" +id).width() / 6;
	}
}


