﻿/* 
  Handles rearranging of the navigation at runtime using jQuery.
*/
jQuery(function( $ ){
	// NB: it's FlyOuts, not Flyouts (capital O)

	// id == #zz1_GlobalNavn2Items
	var baseId = "";

	// look to see if 2nd level is selected
	var elSubNavItems = $('table.topNavFlyOutsSelected').parent().parent().parent().parent().parent();
	if(elSubNavItems && elSubNavItems.length > 0 && elSubNavItems.attr('id') != null) {
		baseId = elSubNavItems.attr('id').replace(/Items/,'');
	} else {
		// look to see if 1st level is selected
		var elSelectedTopNav = $('table.topNavSelected').parent().parent();
		if(elSelectedTopNav && elSelectedTopNav.length > 0) {
			baseId = elSelectedTopNav.attr('id');
			elSubNavItems = $('div#'+baseId+'Items');
		}
	}
	if(baseId != "" && elSubNavItems != null) {
		// change the ID so the Sharepoint Menu code can't find it to hide it
		// ref: Menu_ResetSiblings/Menu_ResetTopMenus in WebResource.axd (dynamically loaded)
		elSubNavItems.attr('id',baseId+'StayOpen');

		// this will match the parent nav element ID
		$('#'+baseId+'>td').parent().after("<tr class=\"tempSecondLevelNav\"><td></td></tr>");
		$('#'+baseId+'>td>table').addClass("topNavSelected");
		$('.tempSecondLevelNav').addClass("currentSecondLevelNav");
	
		elSubNavItems.appendTo('tr.currentSecondLevelNav>td');
	
		// any other display tweaks that should be fixed at runtime
		$('tr.currentSecondLevelNav .topNavFlyOuts .topNavFlyOutsItem td').css('white-space','normal');
	
		// make the items visible
		$('tr.currentSecondLevelNav :hidden').css('visibility','visible');
		$('tr.currentSecondLevelNav div.topNavFlyOuts').css('display','block');
	
		// remove mouse handlers so it doesn't get re-hidden later
		$('tr.currentSecondLevelNav table tbody tr').removeAttr('onmouseover').removeAttr('onmouseout').removeAttr('onkeyup');
		$('#'+baseId).removeAttr('onmouseover').removeAttr('onmouseout').removeAttr('onkeyup');
	}
});         
