// JavaScript Document

$(document).ready(function() {
	
  /****************************************
   * PRIMARY NAVIGATION MENU
   ***************************************/
	
	if (isRunningIE6OrBelow != true) {
	    
		/**
		 * Changes the "top" value of all "hidden" sub-menus
		 */
		$.each($("div#primary-nav > ul > li"), function(i, mainMenuItem) {
		    // Get the child menu of each main menu item (if one exists)
			if ($(this).find("ul").html() != null) {
				var childMenu = $(this).find("ul");
			}
			// If a child menu exists, move it down 30 pixels
			if (childMenu && childMenu.hasClass("hidden")) {
			    childMenu.css({"top": "30px"});
			}
		});
		
		/**
		 * When a main menu button is moused over
		 */
		$("div#primary-nav > ul > li > a").mouseover(function() {
			// Get the child menu of the moused-over main menu item (if one exists)
			if ($(this).parent().find("ul").html() != null) {
				// Reveal the child menu
				var childMenu = $(this).parent().find("ul");
				revealSubMenu(childMenu);
			}
		});
		
		/**
		 * When the cursor moves beyond the boundaries of the menu container
		 */
		$("div#primary-nav").mouseleave(function() {
			restoreMenuState();
		});
		
		/**
		 * Reveals a specific sub-menu and hides all the others
		 */
		function revealSubMenu(menu) {
			// Loop through all of the main menu items
			$.each($("div#primary-nav > ul > li"), function(i, mainMenuItem) {
				// Get the child menu of each main menu item (if one exists)
				if ($(this).find("ul").html() != null) {
					var childMenu = $(this).find("ul");
				}
				// If a child menu exists
				if (childMenu) {
					// If the child menu is the same menu this function was told to reveal
					if (childMenu.html() == menu.html()) {
						// Reveal the child menu
						childMenu.animate({height: 'show', top: "0px", opacity: 'show'}, 'medium');
					} else {
						// Hide the child menu
						childMenu.animate({height: 'hide', top: "30px", opacity: 'hide'}, 'medium');
					}
				}
			});
		}
		
		/**
		 * Restores all the sub-menus to their initial state
		 */
		function restoreMenuState() {
			// Loop through all of the main menu items
			$.each($("div#primary-nav > ul > li"), function(i, mainMenuItem) {
				// Get the child menu of each main menu item (if one exists)
				if ($(this).find("ul").html() != null) {
					var childMenu = $(this).find("ul");
				}
				// If a child menu exists
				if (childMenu) {
					// If the child menu is the same menu this function was told to reveal
					if (childMenu.hasClass("hidden")) {
						// Hide the child menu
						childMenu.animate({height: 'hide', top: "30px", opacity: 'hide'}, 'medium');
					} else {
						// Reveal the child menu
						childMenu.animate({height: 'show', top: "0px", opacity: 'show'}, 'medium');
					}
				}
			});
		}
	
	}

});

