/*

=================================================================================================
>> EASYMENU - XHTML/CSS/DHTML Semantically correct drop down menu
=================================================================================================
Author: 	Sam Hampton-Smith
Site: 		http://www.hampton-smith.com

Description:	This script takes a nested set of <ul>s and turns it into a fully functional
		DHTML menu. All that is required is the correct use of class names, and
		the application of some CSS. For the latest version of this script, go to
		http://www.easymenu.co.uk

Use:		Use of this code is free for all non-commercial applications. If you would like to
		use the code on a commercial website, a link back to http://www.easymenu.co.uk is
		required. You are free to make alterations to the code as required.


Copyright:	This script is copyright 2004-2005 hampton-smith limited. All rights reserved.


--------------------------------------------------------------------------------------------------
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
--------------------------------------------------------------------------------------------------
Credits: 	Inspiration/Code borrowed from Dave Lindquist (http://www.gazingus.org)
		Menu hide functionality was aided by some code I found on http://www.jessett.com/
		Konqueror functionality was aided by some code Scott Wehrenberg emailed over
--------------------------------------------------------------------------------------------------
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
--------------------------------------------------------------------------------------------------

==================================================================================================
>> REVISION HISTORY
==================================================================================================
Date		Version		Changes/Bug Fixes/New functionality added
--------------------------------------------------------------------------------------------------
10/01/2005	1.01		Fixed known issue where hovering over a sub-menu, then returning to
				its parent menu not via the "starter" item on that menu, where the
				new item moused over is a "starter" caused the two sub-menus
				to overlay each other until a further movement of the mouse.
				Fixed known issue where returning to parent menu via a non-"starter"
				menu item on the parent menu would not hide the child menu
08/01/2005	1.00		First full release of easymenu.
				Stylesheets now loaded in through JavaScript to improve accessibility.
01/12/2004	0.9		Base code inherited from client project
				Added suggestion from Scott Wehrenberg enabling Konqueror functionality
--------------------------------------------------------------------------------------------------


Notes:
1.  Each level of UL needs a separate id.
2.  Each of those separate id's needs an entry in the css stylesheet
    that corresponds to a.sub_menu.


*/
	// Load up the stylesheet for the menus. This code is here rather than in the html
	// document to allow for non-javascript browsers to render a seperately styled non-
	// interactive list rather than the DHTML menu
//	document.write("<link rel='stylesheet' href='styles/kb_cssmenu.css' type='text/css' />");

	var currentMenu = null;
	var lastMenuStarter = null;
	var mytimer = null;
	var timerOn = false;
	var opera = window.opera ? true : false;

	if (!document.getElementById)
		document.getElementById = function() { return null; }

	function initialiseDummy(dummy, root) {
		dummy.onmouseover = function() {
			containingMenu = this.parentNode.parentNode;
			for (var x=0;x<containingMenu.childNodes.length;x++) {
				if (containingMenu.childNodes[x].nodeName.toUpperCase()=="LI") {
					if (containingMenu.childNodes[x].getElementsByTagName("ul").length>0) {
						containingMenu.childNodes[x].getElementsByTagName("UL").item(0).style.visibility = 'hidden';
					}
				}
			}
		}
		dummy.onfocus = function() {
			dummy.onmouseover();
		}
	}

	function initialiseMenu(menu, starter, root) {
		var leftstarter = false;

		if (menu == null || starter == null) return;
			currentMenu = menu;

		starter.onmouseover = function() {
			if (currentMenu) {
				if (this.parentNode.parentNode!=currentMenu) {
					currentMenu.style.visibility = "hidden";
					hideAllMenus(currentMenu, root);

				}
				if (this.parentNode.parentNode==root) {
					while (currentMenu.parentNode.parentNode!=root) {
						currentMenu.parentNode.parentNode.style.visibility = "hidden";
						currentMenu = currentMenu.parentNode.parentNode;
					}
				}
				currentMenu = null;
				this.showMenu();
	        	}
		}

		menu.onmouseover = function() {
			if (currentMenu) {
				currentMenu = null;
				this.showMenu();
	        	}
		}

		starter.showMenu = function() {
			if (!opera) {
				if (this.parentNode.parentNode==root) {
					menu.style.left = this.offsetLeft + "px";
					menu.style.top = this.offsetTop + this.offsetHeight + "px";
					if (menu.offsetWidth < this.offsetWidth) menu.style.width = this.offsetWidth;
				}
				else {
				 	menu.style.left = this.offsetLeft + this.offsetWidth + "px";
				 	menu.style.top = this.offsetTop + "px";
				}
			}
			else {
				var rootOffset = root.offsetLeft;
				if (this.parentNode.parentNode==root) {
					menu.style.left = this.offsetLeft - rootOffset + "px";
					menu.style.width = this.offsetWidth;
					menu.style.top = this.offsetHeight + "px";
				}
				else {
				 	menu.style.left = this.offsetWidth - rootOffset + "px";
				 	menu.style.top = this.offsetTop + "px"; //menu.style.top - menu.style.offsetHeight + "px";
				}

			}
			menu.style.visibility = "visible";
			currentMenu = menu;
		}

		starter.onfocus	 = function() {
			starter.onmouseover();
		}

		menu.onfocus	 = function() {
//			currentMenu.style.visibility="hidden";
		}

		menu.showMenu = function() {
//                if(document.getElementById&&!document.all)
//                  document.getElementById("menuProtector").style.display = "";
			menu.style.visibility = "visible";
			currentMenu = menu;
			stopTime();
		}

		menu.hideMenu = function()  {
			if (!timerOn) {
				mytimer = setInterval("killMenu('" + this.id + "', '" + root.id + "');", 1000);
				timerOn = true;
				for (var x=0;x<menu.childNodes.length;x++) {
					if (menu.childNodes[x].nodeName=="LI") {
						if (menu.childNodes[x].getElementsByTagName("UL").length>0) {
							menuItem = menu.childNodes[x].getElementsByTagName("UL").item(0);
							menuItem.style.visibility = "hidden";
						}
					}
				}
			}
		}

		menu.onmouseout = function(event) {
//                if(document.getElementById&&!document.all)
//                  document.getElementById("menuProtector").style.display = "none";
			this.hideMenu();
		}
		menu.onblur = function() {
			this.hideMenu();
		}
		starter.onmouseout = function() {
//                if(document.getElementById&&!document.all)
//                  document.getElementById("menuProtector").style.display = "none";
			for (var x=0;x<menu.childNodes.length;x++) {
				if (menu.childNodes[x].nodeName=="LI") {
					if (menu.childNodes[x].getElementsByTagName("UL").length>0) {
						menuItem = menu.childNodes[x].getElementsByTagName("UL").item(0);
						menuItem.style.visibility = "hidden";
					}
				}
			}
			menu.style.visibility = "hidden";
		}
}

	function killMenu(menu, root) {
		var menu = document.getElementById(menu);
		var root = document.getElementById(root);
		menu.style.visibility = "hidden";
		for (var x=0;x<menu.childNodes.length;x++) {
			if (menu.childNodes[x].nodeName.toUpperCase()=="LI") {
				if (menu.childNodes[x].getElementsByTagName("ul").length>0) {
					menuItem = menu.childNodes[x].getElementsByTagName("UL").item(0);
					menuItem.style.visibility = "hidden";
//				killMenu(menuItem.childNodes[x].getElementsByTagName("UL").item(0), root);
				}
			}
		}
		while (menu.parentNode.parentNode!=root) {
			menu.parentNode.parentNode.style.visibility = "hidden";
			menu = menu.parentNode.parentNode;
		}
		stopTime();
	}
	function stopTime() {
		if (mytimer) {
		 	 clearInterval(mytimer);
			 mytimer = null;
			 timerOn = false;
		}
	}

	window.onload = function() {
                if (top.location != location) {
                  top.location.href = document.location.href ;
                }
		var root = document.getElementById("menuList");
		var rootOffset = root.offsetLeft;
		getMenus(root, root);

//		add_iframe();


                var today = new Date();
                var the_day = today.getDate();
                var the_month = today.getMonth();
                the_month = the_month + 1;
                var the_year = today.getYear();
                if(navigator.appName =="Netscape"){
                  the_year += 1900;
                }

                if(page_name == "home")
                {
                  if(screen.availWidth < 850)
                  {
                    document.getElementById("home_calendar").style.background="white url(images/calendarmiddle01750.jpg)";
                    document.getElementById("home_announcements").style.background="white url(images/calendarmiddle01750.jpg)";
                    document.getElementById("home_publications").style.background="white url(images/calendarmiddle01750.jpg)";
                    document.getElementById("main_header").style.background="white url(images/top01f750.jpg) 0 0 no-repeat";
                    document.getElementById("left_top").style.background="white url(images/top02750.jpg) 0 0 no-repeat";
                    document.getElementById("h_bottom").style.background="white url(images/calendarbottom02750.jpg) 0 0 no-repeat";
                    document.getElementById("home_calendar_date").style.background="white url(images/calendartop01750.jpg) 0 0 no-repeat";
                    document.getElementById("calendarh2").style.background="white url(images/calendarheader04750.jpg) 0 0 no-repeat";
                    document.getElementById("calendarh3").style.background="white url(images/calendarheader02750.jpg) 0 0 no-repeat";
                    document.getElementById("a_academic_header").style.background="white url(images/bigareabordertop750.jpg) 0 0 no-repeat";
                    document.getElementById("a_side_borders").style.background="white url(images/bigareabordermiddle750.jpg)";
                    document.getElementById("a_bottom").style.background="white url(images/bigareaborderbottom750.jpg) 0 0 no-repeat";
                  }
                }


                if(page_name == "contact")
                {
                  if(screen.availWidth < 850)
                  {
                    document.getElementById("main_container").style.background="white url(images/contact01b750.jpg) 0 0 no-repeat";
                  }
                }

                if(page_name == "clubs")
                {
                  if(screen.availWidth < 850)
                  {
                    document.getElementById("main_container").style.background="white url(images/clubs01m750.jpg) 0 0 no-repeat";
                  }
                  pick_one();
                }

                if(page_name == "bells_main")
                {
                  if(screen.availWidth < 850)
                  {
                    document.getElementById("main_container").style.background="white url(images/bells01a750.jpg) 0 0 no-repeat";
                  }
                }

                if(page_name == "sl_main")
                {
                  if(screen.availWidth < 850)
                  {
                    document.getElementById("main_container").style.background="white url(images/sl01b750.jpg) 0 0 no-repeat";
                  }
                  runSlideShow();
                }

                if(page_name == "athletics")
                {
                  if(screen.availWidth < 850)
                  {
                    document.getElementById("main_container").style.background="white url(images/clubs01m750.jpg) 0 0 no-repeat";
                  }
                  runSlideShow();
                }

                if(page_name == "ethnic")
                {
                  if(screen.availWidth < 850)
                  {
                    document.getElementById("main_container").style.background="white url(images/ethnic01a750.jpg) 0 0 no-repeat";
                  }
                }

                if(page_name == "calendar")
                {
                  if(screen.availWidth < 850)
                  {
                    document.getElementById("main_container").style.background="white url(images/calendar01c750.jpg) 0 0 no-repeat";
                    document.getElementById("calendar_bot").src="images/calendar01nbot750.jpg";
                  }
                }

                if(page_name == "fry")
                {
                  if(screen.availWidth < 850)
                  {
                    document.getElementById("main_container").style.background="white url(images/fry01a750.jpg) 0 0 no-repeat";
                    document.getElementById("fry_header").style.background="white url(images/frytop750.jpg) 0 0 no-repeat";
                    document.getElementById("a_side_borders").style.background="white url(images/frymiddle750.jpg)";
                    document.getElementById("a_bottom").style.background="white url(images/frybottom750.jpg) 0 0 no-repeat";
                  }
                  change_fry_list();
                }

                if(page_name == "ib_main")
                {
                  if(screen.availWidth < 850)
                  {
                    document.getElementById("main_container").style.background="white url(images/ib01h750.jpg) 0 0 no-repeat";
                    if(document.getElementById("h_bottom"))
                    {
                      document.getElementById("h_bottom").style.background="white url(images/calendarbottom02750.jpg) 0 0 no-repeat";
                    }
                    if(document.getElementById("home_calendar_date"))
                    {
                      document.getElementById("home_calendar_date").style.background="white url(images/calendartop01750.jpg) 0 0 no-repeat";
                    }
                    if(document.getElementById("home_calendar"))
                    {
                      document.getElementById("home_calendar").style.background="white url(images/calendarmiddle01750.jpg)";
                    }
//                    for(i=0; (a = document.getElementsByTagName("IMG")[i]); i++)
//                    {
//                      a.src="images/ib_colored_bar750.jpg";
//                    }
                  }
                }

                if(page_name == "departments")
                {
                  if(screen.availWidth < 850)
                  {
                    document.getElementById("main_container").style.background="white url(images/dept01a750.jpg) 0 0 no-repeat";
                    document.getElementById("dept_bottom").src="images/dept_menu750.jpg";
                  }
                  runSlideShow();
                }

                if(page_name == "login")
                {
                  document.getElementById('userId').focus();
                }
	}

function getMenus(elementItem, root) {
	var selectedItem;
	var menuStarter;
	var menuItem;
	for (var x=0;x<elementItem.childNodes.length;x++) {
		if (elementItem.childNodes[x].nodeName.toUpperCase()=="LI") {
			if (elementItem.childNodes[x].getElementsByTagName("ul").length>0) {
				menuStarter = elementItem.childNodes[x].getElementsByTagName("A").item(0);
				menuItem = elementItem.childNodes[x].getElementsByTagName("UL").item(0);
				getMenus(menuItem, root);
				initialiseMenu(menuItem, menuStarter, root);
			}
			else {
				initialiseDummy(elementItem.childNodes[x].getElementsByTagName("A").item(0), root);
			}
		}
	}
}
function hideAllMenus(elementItem, root) {
//        if(document.getElementById&&!document.all)
//          document.getElementById("menuProtector").style.display = "";
	for (var x=0;x<elementItem.childNodes.length;x++) {
		if (elementItem.childNodes[x].nodeName.toUpperCase()=="LI") {
			if (elementItem.childNodes[x].getElementsByTagName("ul").length>0) {
				elementItem.childNodes[x].getElementsByTagName("UL").item(0).style.visibility = 'hidden';
				hideAllMenus(elementItem.childNodes[x].getElementsByTagName("UL").item(0), root);
			}
		}
	}
}


//fix for flash issue - add a dynamically created iframe behind overlay
function add_iframe()
{
        var mcontainer = document.getElementById("container");
        var mpres = document.getElementById("menuDiv");
	// This creates an IFrame so that the window won't allow dropdowns to show through it.
	var ifrm = document.createElement("iframe");
	ifrm.style.height = 900;
	ifrm.style.width = 700;
	ifrm.style.border = "none";
	ifrm.style.background = "green";
	ifrm.id = "menuProtector";
	mcontainer.insertBefore(ifrm, mpres);
}




