$(document).ready(function() {

$('a').not('[tabindex]').attr('tabindex',0);  //fixes tabindexing in opera

// ======== hover and focus for primary-navigation ============
$('#site-navigation li.primary-nav-item').find('div.subnav-container').hide(); //init menus: hide all sub menus
	//NOTE: use mouse enter/leave instead of over/out to avoid conflicts between tabbing and mouse position
	//FF fires mouseover even when mouse is still so it can conflict with tabbing
	//Use timeout to show nav to allow the user to quickly mouseover the menu without activating it.
	//On mouseover, set a timeout to show the menu, if the mouse is still over the menu then show
	//it, else don't show it. The status is stored in the data object on the element.
	$('#site-navigation li.primary-nav-item')
		.mouseenter(function(evt) {
			var $elm = $(this);
			$elm.data('mouseover', true); //set mouseover status to true for tracking (see mouseleave below)
			//set timeout, if mouse is still over element then show. allows for quick mouseover without triggering
			setTimeout(function(){if($elm.data('mouseover'))$elm.children('div.subnav-container:hidden').slideDown();},100); })
		.mouseleave(function(evt) {
			var $elm = $(this);
			$elm.data('mouseover', false); 	//set mouseover status to false, value is used in timeout above
			$elm.children('div.subnav-container:visible').slideUp(); //hide the menu
            //
            var l_name = '';
            for (ii=1;ii<7;ii++) {
                l_name = 'primary-nav-link-' + ii;
                document.getElementById(l_name).style.color = '#FFFFFF';
            }
		})
	//blur fires on the 'old' element before focus fires on the 'new' element, so set timeout on blur
	//if focus is set to another element within the same nav then cancel the hide() timeout. This allows
	//any blur (back-tab, mouse click) to hide the menu, but still keep menu open while tabbing
 	$('#site-navigation li.primary-nav-item a')
		.focus(function() {
			var $elm = $(this).parents('.primary-nav-item');
 			$elm.find('.subnav-container').show();
 			clearTimeout($elm.data('timer')); //remove the blur timeout callback
		}).blur(function() {
			var $elm = $(this).parents('.primary-nav-item'); //set local reference on this element
 			//store a handle on the timeout on the wrapper so the focus handler can easily access/cancel it
			$elm.data('timer',setTimeout(function(){$elm.find('div.subnav-container:visible').hide();},100)); //see quirk note above
		});

// ======== hover and focus for tertiary-navigation ============
	$('#section-navigation div.nav-box ul.tertiary-navigation').hide(); //on init, hide menus

	//tertiary nav is a bit different: the <a> can activate/open the menu like a regular menu but
	//it is not closed until the user navigates away from the entire menu. Hence, the mouseenter is
	//on the .nav-box but the mouseleave is on the section-navigation. Like the primary nav, a timeout
	//is used to trigger show so the user can mouseover the menu without triggering the slidedown
 	$('#section-navigation div.nav-box')
		.mouseenter(function(evt) {
		//console.log(this, 'mouseenter');
			var $elm = $(this);
			$elm.data('mouseover', true);	//set mouszeover status to true
			setTimeout(function(){
				if($elm.data('mouseover')){
					$elm.find('ul.tertiary-navigation:hidden').slideDown();
				}
			},100);
		})
		.mouseleave(function(evt) {
		//console.log(this, 'mouseleave');
			var $elm = $(this);
			$elm.data('mouseover', false); 	//set mouseover status to false
		})

//	this should target the entire section-navigation, not the nav-box or tertiary-navigation
//	CK: changed target from '#section-navigation' to '#sidebar-content'
// 	$('#section-navigation')
	$('#sidebar-content')
 		.mouseleave(function(evt){
 			$(this).find('div.nav-box ul.tertiary-navigation').slideUp();
 		})

 	//focus/blur -  see note on primary nav above
 	//target the section navigation links. User can tab down menu, on blur of any element
	$('#section-navigation a')
		.focus(function() {
		//console.log(this, 'focus');
			var $elm = $(this); //get handle on original event target
			//find parent wrapper 'nav-box' then back down to the menu to show
			$elm.parents('div.nav-box').find('ul.tertiary-navigation').show(); //see quirk note above re: show()/hide()
			//a handle on the timer for hiding the tertiary nav is stored in the section-navigation wrapper
			clearTimeout($('#section-navigation').data('timer')); //remove the hide timeout
		}).blur(function() {
		//console.log(this, 'blur');
 			$('#section-navigation').data('timer',setTimeout(function(){$('#section-navigation div.nav-box ul.tertiary-navigation').hide();},100));
		})
		
// ======== hover and focus for home block boxes ============
	$('#HomeBlock div.HomeBox div.boxcontent').hide(); //on init, hide menus

	//tertiary nav is a bit different: the <a> can activate/open the menu like a regular menu but
	//it is not closed until the user navigates away from the entire menu. Hence, the mouseenter is
	//on the .nav-box but the mouseleave is on the section-navigation. Like the primary nav, a timeout
	//is used to trigger show so the user can mouseover the menu without triggering the slidedown
 	$('#HomeBlock div.HomeBox')
		.mouseenter(function(evt) {
		//console.log(this, 'mouseenter');
			var $elm = $(this);
			$elm.data('mouseover', true);	//set mouszeover status to true
			setTimeout(function(){
				if($elm.data('mouseover')){
					$elm.find('div.boxcontent:hidden').slideDown();
				}
			},100);
		})
		.mouseleave(function(evt) {
		//console.log(this, 'mouseleave');
			var $elm = $(this);
			$elm.data('mouseover', false); 	//set mouseover status to false
		})

//	this should target the entire section-navigation, not the nav-box or tertiary-navigation
//	CK: changed target from '#section-navigation' to '#sidebar-content'
// 	$('#section-navigation')
	$('#HomeBlock')
 		.mouseleave(function(evt){
 			$(this).find('div.HomeBox div.boxcontent').slideUp();
 		})

 	//focus/blur -  see note on primary nav above
 	//target the section navigation links. User can tab down menu, on blur of any element
	$('#HomeBlock a')
		.focus(function() {
		//console.log(this, 'focus');
			var $elm = $(this); //get handle on original event target
			//find parent wrapper 'nav-box' then back down to the menu to show
			$elm.parents('div.HomeBox').find('div.boxcontent').show(); //see quirk note above re: show()/hide()
			//a handle on the timer for hiding the tertiary nav is stored in the section-navigation wrapper
			clearTimeout($('#HomeBlock').data('timer')); //remove the hide timeout
		}).blur(function() {
		//console.log(this, 'blur');
 			$('#HomeBlock').data('timer',setTimeout(function(){$('#HomeBlock div.HomeBox div.boxcontent').hide();},100));
		})
		
		

});




