// JavaScript Document





/*** BEGIN MAIN MENU FUNCTIONALITY ***
/* Functions to control Main Menu functionality *
var dropMenu = null;
var timeout = 250;
var closer = 0;
function menuClose() {
	if (dropMenu) {
		dropMenu.css('visibility', 'hidden');
	}
}
function menuTimerCancel() {
	if (closer) {
		window.clearTimeout(closer);
		closer = null;
	}
}
function swapIn(event) {
	menuTimerCancel();
	menuClose();
	dropMenu = $(this).find('div').css('visibility', 'visible');
}
function swapOut(event) {
	closer = window.setTimeout(menuClose, timeout);
}
/*** END MAIN MENU FUNCTIONALITY ***/

var dropMenu = null;
var timeout = 500;
var closer = 0;
function menuClose() {
	if (dropMenu) {
		dropMenu.hide();
	}
}
function menuTimerCancel() {
	if (closer) {
		window.clearTimeout(closer);
		closer = null;
	}
}
function swapIn(event) {
	menuTimerCancel();
	menuClose();
	dropMenu = $(this).find('ul:first').show();
}
function swapOut(event) {
	closer = window.setTimeout(menuClose, timeout);
}





$.fn.descendantOf = function(element) {
    element = $(element)[0];
    var current = this;
    var body    = document.body;
    while (current && current != element && current != document.body) {
        current = $(current).parent()[0];
    }
    if (typeof(current) == "undefined" || typeof(current) == "null") {
        return false;
    } else if (current == element) {
        return true;
    } else if (current == document.body) {
        return false;
    }
}







/*********************************************************************
/* Initialize the menu behavior
/********************************************************************/
function initMenus(){
	var menuActive = 0;
	var subActive = 0;
	var currMenu = null;
	var prevMenu = null;
//	$("#capitalSubNavigation ul ul").css({display: "none"}); // Opera Fix
	$("#capitalSubNavigation li").hover(function() {
			currMenu = $(this);
			
			if ( (currMenu != prevMenu) && (prevMenu != null) && (!$(currMenu).descendantOf(prevMenu)) ) {
				prevMenu.find('ul:first').hide();
				prevMenu = currMenu;
			}
			else {
				prevMenu = currMenu;
			}
			if (!menuActive) {
				menuActive = 0.5;
				if ($(this).find('ul:first').html())
					//$(this).css('width', '352px');
					$(this).css('padding-right', '382px');
				//$(this).find('ul:first').css({visibility: "visible",display: "none"}).show('slide', {direction: 'up'}, 'fast');
				$(this).find('ul:first').show('slide', {direction: 'up'}, 300, function() { menuActive=1; subActive=1; });
			}
			
			//NOTE - problem exists here... if i don't check for a subactive, then the menu drop won't repeat, but the first sub menu won't fire
			//       if i do check for subactive, the menu drop will repeat, but the first sub menu will fire
			//       try to set it so that the menu drop will not repeat, and the first sub menu will fire
			
			else if (menuActive == 0.5) {
				if ($(this).find('ul:first').html())
					$(this).css('padding-right', '352px');
				$(this).find('ul:first').show('slide', {direction: 'up'}, 300);
			}
			/*
			else if (subActive) {
				if ($(this).find('ul:first').html())
					$(this).css('padding-right', '352px');
				$(this).find('ul:first').show('slide', {direction: 'up'}, 300, function() { subActive=0; });
			}
			*/
			/*
			else if (menuActive > 0) {
				if ($(this).find('ul:first').html())
					$(this).css('padding-right', '352px');
				$(this).find('ul:first').show('slide', {direction: 'up'}, 300);
			}
			*/
			/*
			else if ( (menuActive > 0) && subActive) {
				if ($(this).find('ul:first').html())
					$(this).css('padding-right', '352px');
				$(this).find('ul:first').show('slide', {direction: 'up'}, 300);
			}
			*/
		}, function() {
			if (menuActive == 0.5) {
				if (parseInt($(this).css('padding-right')) > 200)
					$(this).css('padding-right', '0px');
				$(this).find('ul:first').hide();
				menuActive = 0;
			}
			else if (menuActive) {
				//if ($(this).width() > 200)
				if (parseInt($(this).css('padding-right')) > 200)
					$(this).css('padding-right', '0px');
					//$(this).css('width', '190px');
				//$(this).find('ul:first').css({visibility: "hidden", display: "none"});
				$(this).find('ul:first').hide();
				menuActive = 0;
			}
			else {
				$(this).css('padding-right', '0px');
				$(this).find('ul:first').hide();
				menuActive=0;
			}
	});
}
/*********************************************************************
/* ALTERED REPLICA OF ABOVE CODE
/********************************************************************
function hideMenu() {
	//if ($(this).width() > 200)
	if (parseInt($(this).css('padding-right')) > 200)
		$(this).css('padding-right', '0px');
		//$(this).css('width', '190px');
	//$(this).find('ul:first').css({visibility: "hidden", display: "none"});
	$(this).find('ul:first').hide();
}
function showMenu() {
	$(this).unbind();
	if ($(this).find('ul:first').html())
		//$(this).css('width', '352px');
		$(this).css('padding-right', '352px');
	//$(this).find('ul:first').css({visibility: "visible",display: "none"}).show('slide', {direction: 'up'}, 'fast');
	$(this).find('ul:first').show('slide', {direction: 'up'}, 500, function() {$(this).hover(showMenu, hideMenu);});
}

function initMenus(){
//	$("#capitalSubNavigation ul ul").css({display: "none"}); // Opera Fix
	$("#capitalSubNavigation li").hover(showMenu, hideMenu);
}

*/



































/*********************************************************************
/* Function to remove the last of the menus after 3
/********************************************************************/
function limitMenus(activeMenuLevelNum) {
	//limit the menus
	var menuLimit = activeMenuLevelNum + 3;
	if ( $('li.egMenuLevel_' + menuLimit).html() ) {
		$('li.egMenuLevel_' + menuLimit).parent().parent().find('ul').remove();
	}
}

/*********************************************************************
/* Function to determine which menu is active, and remove the parents
/********************************************************************/
function determineMenus() {
    
	//variable to store the class in it's entirety
	var activeMenuClassWhole = $('a.egSelected:last').attr('class');

	if (activeMenuClassWhole) {
	    //  var regex = /egMenuLevel_[0-9]*/; //regular expression to match just the egMenuLevel_XX where XX is the numeric value of the menu... this checks for 0 or more occurances
	    //  var regexNum = /[0-9]+/; 		//regular expression to grab just the numeric value of egMenuLevel... this checks for 1 or more occurance... for some strange reason, if the + were changed to * (as in the above expression), it doesn't work

	    var regex = new RegExp(/egMenuLevel_[0-9]*/); //regular expression to match just the egMenuLevel_XX where XX is the numeric value of the menu... this checks for 0 or more occurances
	    var regexNum = new RegExp(/[0-9]+/); 		//regular expression to grab just the numeric value of egMenuLevel... this checks for 1 or more occurance... for some strange reason, if the + were changed to * (as in the above expression), it doesn't work

	    //variable for the leftover classes apart from the active
	    var activeMenuClassStore = activeMenuClassWhole.replace(regex, '');

	    //get just the class name for the level of the menu (e.g. egMenuLevel_2) and convert it to a string
	    var activeMenuLevel = activeMenuClassWhole.match(regex) + '';

	    //get just the numeric value from the menu level and convert to an integer
	    var activeMenuLevelNum = parseInt(activeMenuLevel.match(regexNum));

	    //limit the menu presentation
	    limitMenus(activeMenuLevelNum);

	    //if the active menu is greater than the primary (root) menu, copy it
	    if (activeMenuLevelNum > 0) {
	        var menuStore = $('a.egSelected:last').parent().parent().html();
	        $('#capitalSubNavigation').html(menuStore);
	        initMenus();
	    }
	}
	else {
	    limitMenus(0);
	}
}

$(document).ready(function(){
	initMenus();
	determineMenus();
});









