// JavaScript Document
var subBg = "#eaeaef";
var subHi = "#cacad4";
var mainBg = "#333366";
var mainHi = "#000033";
var greenBg = "#006600";
var greenHi = "#003300";

// THIS IS FOR APPEARANCE OF SUBMENU STUFF

// Initialize necessary variables to keep track of menu depth
var sub1Parent = null;
var sub1 = null;
var sub2Parent = null;
var sub2 = null;
var sub3Parent = null;
var sub3 = null;
// This is for menu elements with no submenus
var lastElement = null;
var lastDepth = null;

// TRACK MOUSE INFORMATION AND CHECK IF OVER SUBMENUS
function trackMouse(e) {
  // GET MOUSE COORDINATES
	mY = Event.pointerY(e);
	mX = Event.pointerX(e);
	if (sub3 && sub2 && sub1) {
		if(!Position.within($(sub3), mX, mY) &&
		   !Position.within($(sub2), mX, mY) && 
		   !Position.within($(sub1), mX, mY) && 
		   !Position.within($(sub1Parent), mX, mY)) {
			$(sub3).hide();
			$(sub2).hide();
 		  	$(sub1).hide();
		  	deLight(sub1Parent, 1);
		  	deLight(sub2Parent, 2);
			deLight(sub3Parent, 3);
		  	sub1Parent = null;
		  	sub1 = null;
		  	sub2Parent = null;
		  	sub2 = null;
			sub3Parent = null
			sub3 = null;
		   } else if (!Position.within($(sub3), mX, mY) &&
					  !Position.within($(sub3Parent), mX, mY)) {
			   $(sub3).hide();
			   deLight(sub3Parent, 3);
			   sub3Parent = null;
			   sub3 = null;
		   } 
	}
	else if (sub2 && sub1 && !sub3) {
		// If mouse is not within any menu of depth 2 hide everything
	  	if (!Position.within($(sub2), mX, mY) && 
			!Position.within($(sub1), mX, mY) && 
		  	!Position.within($(sub1Parent), mX, mY)) {
		  	$(sub2).hide();
 		  	$(sub1).hide();
		  	deLight(sub1Parent, 1);
		  	deLight(sub2Parent, 2);
		  	sub1Parent = null;
		  	sub1 = null;
		  	sub2Parent = null;
		  	sub2 = null;
			//alert("a");
			
	   // If mouse is not within sub2 or sub2Parent and
	   // is within either sub1 or sub1Parent just hide sub2
	   }else if (!Position.within($(sub2), mX, mY) && 
	   		!Position.within($(sub2Parent), mX, mY)) {
		  	deLight(sub2Parent, 2);
		  	$(sub2).hide();
		  	sub2 = null;
		  	sub2Parent = null;
			//alert("b");
	  } 
	  
	  // If there is no sub2 and the mouse
	  // is not in the sub1 or sub1Parent
	  // hide the sub1
 	} else if (sub1 && !sub2) {
	 if(!Position.within($(sub1), mX, mY) && 
		!Position.within($(sub1Parent), mX, mY)) { 
		 $(sub1).hide();
		 deLight(sub1Parent, 1);
		 sub1Parent = null;
		 sub1 = null;
		 //alert("c");
	 }
 	} 
	
	if (lastElement) {
		if (!Position.within(lastElement, mX, mY)) {
			deLight(lastElement, lastDepth);
			lastElement = null;
			lastDepth = null;
			//alert("d");
		}
	}
		
}

function hideAll(depth) {
	//Function hides all showing submenus
	//can be modified to work with more depths
	if(depth == 1) {
		if(sub1) {
			$(sub1).hide();
			deLight(sub1Parent, depth);
			sub1 = null;
			sub1Parent = null;
		}
	} 
	if (depth == 2) {
		if(sub2) {
			$(sub2).hide();
			deLight(sub2Parent, 2);
			sub2 = null;
			sub2Parent = null;
		}
	}
	if (depth == 3) {
		if(sub3) {
			$(sub3).hide();
			deLight(sub3Parent, 3);
			sub3 = null;
			sub3Parent = null;
		}
	}
	if (lastElement) {
		deLight(lastElement, lastDepth);
		lastElement = null;
		lastDepth = null;
	}
}

/*function setLocation(parentId, elementId) {
	t = getRelativeTop(parentId);
  	t2 = getTop(parentId);
  	h = getHeight(parentId);
  	l = -138;
	var dim = Element.getDimensions($(elementId));
	h2 = dim.height;
  	b = t;
  	
	wh = getWindowHeight();
  	if ((t2 + h2) > wh) {
   		b = t - ((t2 + h2) - wh);
  	}
	
	lval = l + "px";
	tval = b + "px";
	$(elementId).setStyle({left: lval, top: tval});
}*/	

function setLocation(parentId, elementId) {
	//Offset Top of viewport
	top_page = Position.page($(parentId))[1];
  	
	//Preliminary margins in a perfect world with a window size that fits everything
	top_m = Position.positionedOffset($(parentId))[1];
	margin_top = (parentId.className == 'topbutton') ? (top_m + 23) : top_m;
	margin_left = (parentId.className == 'topbutton') ? Position.positionedOffset($(parentId))[0] : 160;
	
	//Heights of elements and window
	parent_height = $(parentId).getDimensions().height;
	element_height = $(elementId).getDimensions().height;
	window_height = getWindowHeight();
	
	if ((top_page + element_height) > window_height) {
		margin_top -= ((top_page + element_height) - window_height);
	}
	
	lval = margin_left + "px";
	tval = margin_top + "px";
	$(elementId).setStyle({left: lval, top: tval});
}	

//Event handler for showSubs
function eventSubs(e) {
	var curElem = Event.element(e);
	if (curElem.tagName == 'A' || curElem.tagName == 'a') {
		curElem = Event.findElement(e, 'div');
	}
	
	if (curElem.className == 'topbutton') {
		showSubs(curElem, 1);
	} else if (curElem.className == 'subcolbutton') {
		showSubs(curElem, 2);
	} else if (curElem.className == 'subcolbutton2') {
		showSubs(curElem, 3);
	}
}

// Master function to show all submenus
function showSubs() {
	if (arguments.length == 3) {
		var elementId = arguments[0];
		elementId = $(elementId);
		var parentId = arguments[1];
		parentId = $(parentId);
		var depth = arguments[2];
		if (parentId && depth){
			hideAll(depth);
			//Checks for depth, if necessary can be modified to add more depths
			if (depth == 3) {
				sub3 = elementId;
				sub3Parent = parentId;
			} else if(depth == 2) {
				sub2 = elementId;
				sub2Parent = parentId;
				//document.getElementsByTagName('subcolbutton2');
			} else {
				sub1Parent = parentId;
				sub1 = elementId;
			}
			// POSITIONING
			setLocation(parentId, elementId);
			elementId.show();
			hiLight(parentId, depth);
		} 
	} else {
		var element = arguments[0];
		var depth = arguments[1];
		hideAll(0);
		lastElement = element;
		lastDepth = depth;
		hiLight(element, depth);
	}
}

// hiLights the element can be modified for more depths.
function hiLight(elementId, depth) {
	if (depth == 'green') {
		$(elementId).setStyle({backgroundColor: greenHi});
		$(elementId).className="greenbuttonover";
	}
	if(depth == 1) {
		$(elementId).setStyle({backgroundColor: mainHi});
		$(elementId).className = "topbuttonover";
	} else if (depth == 2) {
		$(elementId).setStyle({backgroundColor: subHi});
	} else if (depth == 3) {
		$(elementId).setStyle({backgroundColor: subHi});
	}
}

// DehiLights the element can be modified for more depths.
function deLight(elementId, depth) {
	if (depth == 'green') {
		$(elementId).setStyle({backgroundColor: greenBg});
		$(elementId).className="greenbutton";
	}
	if(depth == 1) {
		$(elementId).setStyle({backgroundColor: mainBg});
		$(elementId).className = "topbutton";
	} else if (depth == 2) {
		$(elementId).setStyle({backgroundColor: subBg});
	}  else if (depth == 3) {
		$(elementId).setStyle({backgroundColor: subBg});
	}
}

// TABBING THROUGH MENU STUFF
function hideSubs(what,withShift) {
	
  if (withShift == 1 && !shifty) {
    return;
  }
  if (withShift == 0 && shifty) {
    return;
  }
  what = $(what);
  if(what == sub3) {
	  $(what).hide();
	  deLight(sub3Parent, 3);
	  sub3 = null;
	  sub3Parent = null;
  }
  if(what == sub2) {	  
  	  $(what).hide();
	  deLight(sub2Parent, 2);
	  sub2 = null;
	  sub2Parent = null;
  } 
  if (what == sub1) {
	  $(what).hide();
	  deLight(sub1Parent, 1);
	  sub1Parent = null;
	  sub1 = null;
  } 
  if (what == lastElement) {
	  deLight(lastElement, lastDepth);
	  lastElement = null;
	  lastDepth = null;
	  if (sub3) {
		  deLight(sub3Parent, 3);
		  $(what).hide();
		  sub3 = null;
		  sub3Parent = null;
	  }
	  if (sub2) {
		deLight(sub2Parent, 2);
  	  	$(what).hide();
	  	sub2 = null;
	 	sub2Parent = null;
	  }
	  if (sub1) {
	  	deLight(sub1Parent, 1);
	  	$(what).hide();
	  	sub1Parent = null;
	  	sub1 = null;
	  }
  }
}

shifty = null;
function checkKeyUp(e) {
  kc = (IE) ? event.keyCode : (e.keyCode) ? e.keyCode : e.which;
  if (kc == 16) {
    shifty = null;
  }
}

function checkKeyDown(e) {
  kc = (IE) ? event.keyCode : (e.keyCode) ? e.keyCode : e.which;
  if (kc == 16) {
    shifty = 1;
  }
}

// EVENT HANDLERS

function setEvents() {
		
	// Apply mouseover events for subcolbuttons
	var sub1Buttons = $$('div.subcolbutton');
	for(i=0;i<sub1Buttons.length;i++) {
		if (!sub1Buttons[i].id) {
			Event.observe(sub1Buttons[i], 'mouseover', eventSubs);
			Event.observe(sub1Buttons[i].down(), 'focus', eventSubs);
		}
	}
	
	var sub2Buttons = $$('div.subcolbutton2');
	for(i=0;i<sub2Buttons.length;i++) {
		if (!sub2Buttons[i].id) {
			Event.observe(sub2Buttons[i], 'mouseover', eventSubs);
			Event.observe(sub2Buttons[i].down(), 'focus', eventSubs);
		}
	}
/*	if (notnav) {
		// Apply mouseover events for subsubcolbuttons
		var sub2Buttons = $$('div.subcolbutton2');
		for(i=0;i<sub2Buttons.length;i++) {
			if (!sub2Buttons[i].id) {
				Event.observe(sub2Buttons[i], 'mouseover', eventSubs);
				Event.observe(sub2Buttons[i].down(), 'focus', eventSubs);
			}
		}
		
		//Apply mouseover events for colbuttons
		var colbuttons = $$('div.colbutton');
		for(i=0;i<colbuttons.length;i++) {
			if (!colbuttons[i].id) {
				Event.observe(colbuttons[i], 'mouseover', eventSubs);
				Event.observe(colbuttons[i].down(), 'focus', eventSubs);
			}
		}
		
		//Apply mouseover events fro colbuttons_withline
		var colbutton_withlines = $$('div.colbutton_withline');
		for(i=0;i<colbutton_withlines.length;i++) {
			if (!colbutton_withlines[i].id) {
				Event.observe(colbutton_withlines[i], 'mouseover', eventSubs);
				Event.observe(colbutton_withlines[i].down(), 'focus', eventSubs);
			}
		}
	}*/
}
