/*
   Author:   Ann Airey
   Date:     May 1, 2010

   Filename: dwtutor.js


   Function List:

   addEvent(object, evName, fnName, cap)
      Adds an event hander to object where evName is the name of the event,
      fnName is the function assigned to the event, and cap indicates whether
      event handler occurs during the capture phase (true) or bubbling
      phase (false)

	setUp()
		Creates arrays of the dropdowns for menu items and adds mouseover event

   openMenu()
      Opens a menu after closing any previously opened one

   closeOldMenu()
      Closes the active menu

   rollDown()
      Applies a roll-down effect to the opening of the active menu 

   -------------------------------------------------------------
   Global Variable List:

   activeMenu
      An object variable pointing to the currently active and open menu
   
*/

/*  variables used in dropdown menu code  */

var activeMenu = null;
var clipHgt = 0;
var timeID;

function addEvent(object, evName, fnName, cap) {
   if (object.attachEvent)
       object.attachEvent("on" + evName, fnName);
   else if (object.addEventListener)
       object.addEventListener(evName, fnName, cap);
}

addEvent(window, "load", setUp, false);
addEvent(window, "load", placeFooter, false);

function setUp()   {

	/*  set up arrays of dropdown menus and changeable images  */

	var menus = new Array();
	var allElems = document.getElementsByTagName("*");

	for (var i = 0; i < allElems.length; i++)  {
		if (allElems[i].className == "mainmenu")
			menus.push(allElems[i]);
	}

	/* add Events to mouseover for menu items */

	for (var i = 0; i < menus.length; i++)  {
		menus[i].onmouseover = openMenu;
		menus[i].onclick = closeOldMenu;
	}

	/*  if user clicks anywhere else, close any open menu  */

	document.getElementById("header").onmouseover = closeOldMenu;
	document.getElementById("header").onclick = closeOldMenu;  
	document.getElementById("announce").onmouseover = closeOldMenu;
	document.getElementById("announce").onclick = closeOldMenu;
	document.getElementById("extras").onmouseover = closeOldMenu;
	document.getElementById("extras").onclick = closeOldMenu;
	document.getElementById("content").onmouseover = closeOldMenu;
	document.getElementById("content").onclick = closeOldMenu;

}

  function placeFooter()  {

	/*  place the footer at the bottom of the page  */

	var headerHeight = document.getElementById("header").offsetHeight;
	var menuHeight = document.getElementById("menu1").offsetHeight;
	var announceHeight = document.getElementById("announce").offsetHeight;
	var contentHeight = document.getElementById("content").offsetHeight;
	var extrasHeight = document.getElementById("extras").offsetHeight;

	var largestHeight;

	/*  determine which is largest, announcements column, extras column, or content column  */

	if (announceHeight >= contentHeight) {     /*  if announcements longer than content  */

		
		if (announceHeight >= extrasHeight)    /*  if announcements also longer than extras  */
			largestHeight = announceHeight;     /*  announcements is longest  */
		else largestHeight = extrasHeight;		/*  extras is longest  */
	}
	else if (contentHeight >= extrasHeight)     /*  content is longer than announcements */
		largestHeight = contentHeight;			/*  and extras  */
	else largestHeight = extrasHeight;

	var totalHeight = headerHeight + menuHeight + largestHeight;
	document.getElementById("announce").style.height = largestHeight + "px";
	document.getElementById("extras").style.height = largestHeight + "px";
	totalHeight = totalHeight + 6;
	document.getElementById("footer").style.top = totalHeight + "px";

} 

function openMenu()  {

	// this function moves the pull-down menu from one title to another

	if (activeMenu)  
		closeOldMenu();

	if (this.id)  {   /*  it has a sub-menu  */
		menuID = "sub" + this.id;
		activeMenu = document.getElementById(menuID);
		activeMenu.style.clip = "rect(0px, 200px, 0px, 0px)";
		activeMenu.style.display = "block";
		timeID = setInterval("rollDown()", 1);
	}
	

}

function closeOldMenu()  {

	if (activeMenu) {
		clearInterval(timeID);
		activeMenu.style.display = "none";
		activeMenu = null;
	}
}

function rollDown()  {

	clipHgt = clipHgt + 10;
	if (clipHgt < 400) {
		activeMenu.style.clip = "rect(0px, 200px," + clipHgt + "px, 0px)";
	}
	else {
		clearInterval(timeID);
		clipHgt = 0;
	}
}

	

