/* navigation.js */
/* javascript to provide support to the page navigation */

function fixNav()
{
	// foreach main menu item
	$('div#page_head ul#page_nav li a.top_level').each(function()
	{
		// get the width of the title item
		var itemWidth = $('span.title', $(this)).width();
		
		// set the width of the description item to be the title's + 20px
		$('span.description', $(this)).width(itemWidth + 15);
		
		// change the title to display as block
		$('span.title', $(this)).css('display', 'block');
		
		// if the current menu item is selected, display the top bar for it.
		var parent = $(this).parent();
		if (parent.hasClass('selected'))
		{
			showTopBar(parent);
		}	
		// otherwise apply an event to this item to show the top bar on hover
		else
		{
			// show and hide the top bar on mouse over
			$(this).mouseover(function(){
				showTopBar(parent);
			}).mouseout(function(){
				hideTopBar(parent);
			});
		}	
	});
	
	$('div#page_head ul#page_nav').show();
}


// display the top bar for the provided menu item..
function showTopBar(element)
{
	var topBar = "<span class='top_bar' style='width: " + (element.width()) + "px;'>";
	topBar += "<span class='top_bar_left'>&nbsp;</span>";
	topBar += "<span class='top_bar_centre' style='width: " + (element.width() - 10) + "px;'>&nbsp;</span>"; 
	topBar += "<span class='top_bar_right'>&nbsp;</span>";
	topBar += "</span>";
	
	element.append(topBar);
}

function hideTopBar(element)
{
	$('span.top_bar', element).remove();
}







//On Hover Over
function showSubMenu()
{
	// find the sub menu
    var sub = $(this).find(".sub");

	// if the overall x position of the sub menu is greater than 700px (relative to the container), make it join to the right hand side
	if (sub.parent().offset().left > 700)
	{
		sub.css('left', 'auto');
		sub.css('right', '-5px');
	}

	// attach a mouse over and mouse out event to it if the parent is not already selected
	if (!sub.parent().hasClass('selected'))
	{
		sub.mouseover(function(){
			sub.parent().addClass('selected');
			showTopBar(sub.parent());
		}).mouseout(function(){
			sub.parent().removeClass('selected');
			hideTopBar(sub.parent());
		});
	}	

	// fade it in
	sub.show(); //fadeTo('fast', 1).show(); 
	

    (function($) {
        //Function to calculate total width of all ul's
        jQuery.fn.calcSubWidth = function() {
            rowWidth = 0;
            //Calculate row
            $(this).find("ul").each(function() { //for each ul...
                rowWidth  = $(this).width(); //Add each ul's width together
            });
        };
    })(jQuery); 

    if ( $(this).find(".row").length > 0 ) { //If row exists...

        var biggestRow = 0;	

        $(this).find(".row").each(function() {	//for each row...
            $(this).calcSubWidth(); //Call function to calculate width of all ul's
            //Find biggest row
            if(rowWidth > biggestRow) {
                biggestRow = rowWidth;
            }
        });

        $(this).find(".sub").css({'width' :biggestRow}); //Set width
        $(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin

    } else { //If row does not exist...

        $(this).calcSubWidth();  //Call function to calculate width of all ul's
        $(this).find(".sub").css({'width' : rowWidth}); //Set Width

    }
}
//On Hover Out
function hideSubMenu()
{
	// find the sub menu and hide it
  	$(this).find(".sub").hide();  // fadeTo('fast', 0, function() { //Fade to 0 opactiy
//      $(this).hide();  //after fading, hide it
//  });
}



//On Hover Over show the quick links
function showQl()
{
	// find the sub menu and show it
    var sub = $(this).find(".sub").show();
}


// run this when the document has finished loading
$(document).ready(function() 
{
	// fix the navigation widhts
	fixNav();
	
	// set hover intent
	var config = {
	     sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
	     interval: 200, // number = milliseconds for onMouseOver polling interval
	     over: showSubMenu, // function = onMouseOver callback (REQUIRED)
	     timeout: 500, // number = milliseconds delay before onMouseOut
	     out: hideSubMenu // function = onMouseOut callback (REQUIRED)
	};

//	$("ul#page_nav li .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
	$("ul#page_nav li").hoverIntent(config); //Trigger Hover intent with custom configurations
	
	
	// set hover intent for the quick links
	var qlConfig = {
	     sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
	     interval: 200, // number = milliseconds for onMouseOver polling interval
	     over: showQl, // function = onMouseOver callback (REQUIRED)
	     timeout: 500, // number = milliseconds delay before onMouseOut
	     out: hideSubMenu // function = onMouseOut callback (REQUIRED)
	};
	
//	$("div#quick_links .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
	$("div#quick_links").hoverIntent(qlConfig); //Trigger Hover intent with custom configurations
});

