/**
 * Author: Stefan Church
 * Date: 02/05/2007
 * Description: Code for menu system. Uses JQuery library.
 */

// ----------- PRELOADING STUFF ----------- 

/* Preload images jquery function */
jQuery.preloadImages = function()
{
	for(var i = 0; i<arguments.length; i++)
	{
		jQuery("<img>").attr("src", arguments[i]);
	}
}

// preload images first (can run before page is fully loaded)
$.preloadImages("/images/new_e7/arrowDownWhteBgBlue.jpg","/images/new_e7/arrowDownBlueBgLightBlue.jpg","/images/new_e7/arrowWhteBgBlue.jpg","/images/new_e7/arrowBlueBgLightBlue.jpg");


$(document).ready(function(){
	
	/* Show the selected menu if one is selected */
	$("#subMenuShown").next().show();
	$("#subMenuShown").find('img').attr("src","/images/new_e7/arrowDownBlueBgLightBlue.jpg");
	$("#subMenuShown").each(function(){
		setMenuDroppedDown(this);
	});
	
});

// ----------- MAIN METHODS ----------- 

/* Array to store all currently droped down menus */
menusDroppedDown = new Array();

/* Function to check if a menu is droped down */
function isMenuDroppedDown(menu){
	var found = false;
	for(i=0; i < menusDroppedDown.length; i++){
		if(menusDroppedDown[i] == menu)
		{
			found = true;
		}
	}
	return found;
}

/* Function to set a menu as dropped down */
function setMenuDroppedDown(menu){
	menusDroppedDown.push(menu);
}

/* Function to set a menu as not dropped down */
function setMenuNotDroppedDown(menu){
	/* Index of element to remove */
	var menuIndexToRemove = -1;
	
	/* Find the index of element to remove. If it's not found it will be -1 */
	for(i=0; i < menusDroppedDown.length; i++){
		if(menusDroppedDown[i] == menu)
		{
			menuIndexToRemove = i;
		}
	}
	
	if (menuIndexToRemove != -1){
		menusDroppedDown.splice(menuIndexToRemove,1);
	}
}

/* Toogles the selected menu */
function toggleMenu(selectedMenuItem){
	
	/* Set whether the menu is dropped down or not and change to correct arrow */
	if (isMenuDroppedDown(selectedMenuItem)){
		setMenuNotDroppedDown(selectedMenuItem);
		$(selectedMenuItem).find('img').attr("src","/images/new_e7/arrowWhteBgBlue.jpg");
	}else{
		setMenuDroppedDown(selectedMenuItem);
		$(selectedMenuItem).find('img').attr("src","/images/new_e7/arrowDownWhteBgBlue.jpg");
	}
	
	/* Make the next element slide up/down */
	$(selectedMenuItem).next().slideToggle("slow");
	
	/* Return false so the link isn't followed */
	return false;
}

/* Checks which image arrow to show when the mouse hovers over the menu item*/
function checkArrowMouseOver(selectedMenuItem){
		
	if (isMenuDroppedDown(selectedMenuItem)){
		$(selectedMenuItem).find('img').attr("src","/images/new_e7/arrowDownWhteBgBlue.jpg");
	}else{
		$(selectedMenuItem).find('img').attr("src","/images/new_e7/arrowWhteBgBlue.jpg");
	}
}

/* Checks which image arrow to show when the mouse hovers off the menu item */
function checkArrowMouseOut(selectedMenuItem){
	
	if (isMenuDroppedDown(selectedMenuItem)){
		$(selectedMenuItem).find('img').attr("src","/images/new_e7/arrowDownBlueBgLightBlue.jpg");
	}else{
		$(selectedMenuItem).find('img').attr("src","/images/new_e7/arrowBlueBgLightBlue.jpg");
	}
}