// JavaScript Document
// amcMenu.js 
// ------------------------
// ANZIIF | Chinese 

// Controller for main pulldown menu 
// === 
 

// NB To be replaced by more appropriate server-side XSLT functionality when possible : 
// this function 'manually' identifies the current page (by matching Nav URLs) and marks the relevant 
// top level menu item accordingly ...
var setCurrentMenuItem = function() { 
	var url    = $('#side.nav > h3 > a').attr('href'), 
		$items = $('li.chm_level0');  // all top level items 
	
	// loop thru items, find match and apply '.current' classname 
	$items.each( function() { 
		if ( $(this).find('span.btn > a').attr('href') == url ) { 
			$(this).addClass('current'); 
			return false; 
		} 
	}); 

} 


$(document).ready( function() { 

	// mark current top menu tab 
	setCurrentMenuItem(); 
	
	// remove indicator arrow from any top level items that do not contain a submenu 
	$('#menu > ul > li').each( function() { 
		if ( !$(this).find('ul.submenu.level1').length ) { 
			$(this).find('span.btn > a').css('background','none'); 
		} 
	}); 
	
	// insert a non-break space into empty elements that may cause trouble  
	$('span.btn > a, span.endcap').each( function() { 
		if ( !$(this).text().length ) { 
			$(this).html('&nbsp;'); 
		} 
	}); 
	
	// hide all submenus 
	$('ul.submenu').hide(); 
	
	// kill submenus on mouse exit  
	$('#menu ul.submenu:visible').hide().bind('mouseleave', function() { 
		$(this).hide(); 
	}); 

	// on hover, hilight selected menu and display submenu 
	$('#menu span.btn > a').bind('mouseover', function() { 
		var $curItem = $(this).parents('li'); 
		$curItem.siblings().removeClass('select'); 
		$curItem.siblings().find('ul.submenu').hide(); 
		$curItem.addClass('select'); 
		$curItem.find('ul.submenu').fadeIn('fast'); 
	}); 
	
	// kill selected menu and submenu on mouse exit 
	$('#menu span.btn > a').bind('mouseleave', function() { 
		var $curItem = $(this).parents('li'); 
		if ( $curItem.find('ul.submenu:hidden').length ) { 
			$curItem.removeClass('select'); 
		} 		
	}); 
	
	// kill any displayed submenu and selection on mouse wander  
	$('#masthead, #menu, #banner').bind('mouseenter', function() { 
		$('#menu ul.submenu:visible').hide();		 
		$('#menu li.select').removeClass('select');
	}); 	
	
}); 

