/*

	Main Nav
	
	The main nav has two levels. This script breaks the second level into two columns if necessary

*/


jQuery(document).ready(function($){
	
	var header = $('#header');
	var nav = header.find('#top-nav');
	
	var subs = nav.find('ul ul');
	
	var topNavLeft = header.find('#top-nav').position().left;
	var maxHeight = header.find('.bottom').innerHeight();
	
	
//-- Set width of each sub nav panel
	subs.each(function(){
		var e = $(this);
		e.css({
			width: ((header.innerWidth() - (e.closest('li').position().left + topNavLeft)) - 20)
		});
	});
	
//-- Create columns
	subs
		.css({
			opacity: 0,
			display: 'block'	
		})
		.each(function(){
		var e = $(this);
		if(e.innerHeight() > maxHeight)//If height of subnav column is bigger than the height of the panel with the banner
		{
			var list = e.children().detach();//Detach list from the parent ul
			var li1 = $('<li></li>').attr('class','column-1').appendTo(e);	//Create first column and add it to ul
			var li2 = $('<li></li>').attr('class','column-2').appendTo(e);	//Create second column and add it to ul
			var division = Math.floor(list.length / 2);//Split list into two
			var oUl1 = $('<ul></ul>');
			for(var i = 0; i < division; i++)//Add first part of list to first column
			{
				oUl1.append(list[i]);	
			}
			li1.append(oUl1);
			var oUl2 = $('<ul></ul>');
			for(var i = division; i < list.length; i++)//Add second part of list to second column
			{
				oUl2.append(list[i]);	
			}
			li2.append(oUl2);
			
			
			if(e.innerHeight() < maxHeight)e.height(maxHeight - 20);
			
		} else {
			e.height(maxHeight - 20);	
		}
		
		//Attach hover effect to
		e.parent().hover(
			function(){
				$(this).find('ul:first').css('display','block').stop().animate({opacity:1},600,'easeInOutExpo');		
			},
			function(){
				$(this).find('ul:first').stop().animate({opacity:0},600,'easeInOutExpo',function(){$(this).css('display','none')});	
			}
		);
		
	});
	
	
	
});
