// these variables are global for the subnavigation functions
var nav_timer; // setTimeout Identifier for auto hiding the sub nav
var nav_reset; // setTimeout Identifier for retrying a slide if the previous attempt was busy
var active_subnav; // subnav element that is currently displaying. If none, this is null
var status = 0; // status is set to 1 when animating. This prevents the jumping effect from sliding before the current transition has finished
var nav_obj; // this is used within the functions that require the local obj variable to be global

function show_subnav(obj) {
	if(active_subnav != null && active_subnav != obj) {
		// if a navigation item is showing, call the function to hide it
	  auto_hide_nav();
	}
	
	// if there is content in the sub nav div, slide the sub nav down, otherwise
	// set the active subnav to null and exit the function
	if(!$(obj).empty() && active_subnav != obj) {
			slide_down_nav(obj);
	} else {
		// subnav object has no content so exit function
		return false;
	}
}

// auto hide function, this will slide the nav back up
function auto_hide_nav() {
	// if not in the middle of an animation, start the slide effect.
	// the status=0/1 variable determines the animation state
	// 1= working, 0= finished
	if($(active_subnav) && status == 0) {
			// scriptaculous slide effect
			// when done, set active_subnav to null
	  	Effect.SlideUp(active_subnav, {beforeStart: function(){status=1}, duration: .2, afterFinish:function(){status=0;}});
	  	active_subnav = null;
	} else if (status == 1){
		// retry every 0.3 seconds until previous animation is finished
		setTimeout("auto_hide_nav()", 300);
	}
}

// slide down function, this function performs the actual sliding down of the navigation
function slide_down_nav(obj) {
	// some functions require obj to be global,
	// nav_obj is global so this will alleviate that issue
	nav_obj = obj;
	
	// if not in the middle of an animation, start the slide effect.
	// the status=0/1 variable determines the animation state
	// 1= working, 0= finished
	if(status == 0) {
		Effect.SlideDown(nav_obj, {beforeStart: function(){status=1}, duration: .2, afterFinish:function(){status=0;}});
		
		// set the active_subnav to the showing subnav
		active_subnav = obj;

		// always reset the nav_timer berfore
		// this timer is the setting that auto hides the 
		// subnav if not used. seconds * 1000 determines
		// the timer in seconds, currently it is set to 3 seconds
		clearTimeout(nav_timer);
		nav_timer = setTimeout("auto_hide_nav()", 3000);
	} else {
			// retry every 0.3 seconds until previous animation is finished
			clearTimeout(nav_reset);
			nav_reset = setTimeout("slide_down_nav(nav_obj);", 300);
	}
}


// function to clear the auto hide timer
// this function is called when a user is navigating through sub-nav links
// by resetting the timer, the navigation is not auto-hidden until the user stops
// navigating the links
function clear_nav_timer() {
	if(active_subnav != null) {
		clearTimeout(nav_timer);
		nav_timer = setTimeout("auto_hide_nav()", 3000);
	}
}

