/*------------------
	Drop Down Menus
	
	Create Dropdown menus in main navigation
-------------------*/
$(function(){
	$('#main-navigation>ul>li').hover(function(){
		//window.log('enter');
		$('ul', $(this)).fadeIn('fast');
	}, function(){
		//window.log('leave');
		$('ul', $(this)).fadeOut('fast');
	});
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)) || (navigator.userAgent.match(/Android/i))) {
		$('#main-navigation').find('a').bind('click', function(e){
			if($(this).next('ul').length>0){
				e.preventDefault();
			}
    	});
		$('#main-navigation>ul>li').bind('touchstart', function(){
			$('ul', $(this)).fadeIn('fast');
    	});
		$('#main-navigation>ul>li').bind('touchened', function(){
			$('ul', $(this)).fadeOut('fast');
    	});
	}

});




/*------------------
	Nav Alignment
	
	make the main navigation line up with the main content column
-------------------*/

$(function(){

	//get the width of the main column
	var w = $('.page-body').width();
	
	
	//no main column, use best guess
	if(!w)
	{
		w = 750;
	}
	
	
	//Get number of links in main nav
	var navCount = $('#main-navigation>ul>li').size()
	
	var sumNavWidth = 0;
	
	//get total width of all nav items, excluding the space between them.
	$('#main-navigation>ul>li').each(function(){
		sumNavWidth += $(this).width();
	});
	
	//find remaining space for margins
	var spaceLeft = w - sumNavWidth;
	
	var margin;
	
	if(spaceLeft>0)
	{
		//find the ideal  margin width (rounded down to the nearest whole number)
		margin = Math.floor(spaceLeft / (navCount - 1));
		
		
	}else{
		//if the nav is too wide, just use a small margin
		margin = 10;
	}
	
	
	
	//Set the left margin of the main nav items (except the first one)
	$('#main-navigation>ul>li:not(:first)').css('margin-left', margin + "px");

		
	//Apply any remaining space between first and second link	
	var extraMargin = spaceLeft % (navCount - 1);


	$('#main-navigation>ul>li:eq(1)').css('margin-left', margin + extraMargin + 1 + "px");

	
	
});


/*------------------
	Drop Down Width
	
	set the drop-down width for IE6
-------------------*/
$(function(){
	$(".ie6 #main-navigation ul ul").each(function(){
		w = $(this).width();
		pw = $(this).parent().width();
		
		if(w < pw){
			$(this).width(pw);
		}
		
	});
});






