// INDEX SPECIFIC MENU CODE

var sub2Bg = "#333366";
var sub2Hi = "#cacad4";
var subBg = "#333366";
var subHi = "#cacad4";
var mainBg = "#f0f0f0";
var mainHi = "#cacad4";
var topBg = "#000000";
var topHi = "#7f7f7f";

// 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 (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 >= 1) {
		if(sub2) {
			$(sub2).hide();
			deLight(sub2Parent, 2);
			sub2 = null;
			sub2Parent = null;
		}
		if(sub3) {
			$(sub3).hide();
			delight(sub3Parent, 3);
			sub3 = null;
			sub3Parent = null;
		}
	}
			
	if (lastElement) {
		deLight(lastElement, lastDepth);
		lastElement = null;
		lastDepth = null;
	}
}

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
	margin_top = Position.positionedOffset($(parentId))[1];
	margin_left = -138;
	
	//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 == 'colbutton' || curElem.className == 'colbutton_withline') {
		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 == 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) {
	elementId = (typeof elementId == "object") ? elementId : $(elementId);
	//alert(elementId.className);
	if(depth == 1) {
		$(elementId).setStyle({backgroundColor: mainHi});
	} else if (depth == 2) {
		$(elementId).setStyle({backgroundColor: subHi});
		// Sets the link font color
		$(elementId).down().setStyle({color: '#333366'});
	} else if (depth == 3) {
		$(elementId).setStyle({backgroundColor: sub2Hi});
		// Sets the link font color
		$(elementId).down().setStyle({color: '#333366'});
	}
}

// DehiLights the elemnt can be modified for more depths.
function deLight(elementId, depth) {
	elementId = (typeof elementId == "object") ? elementId : $(elementId);
	if(depth == 1) {
		$(elementId).setStyle({backgroundColor: mainBg});
	} else if (depth == 2) {
		$(elementId).setStyle({backgroundColor: subBg});
		// Sets the link font color 
		$(elementId).down().setStyle({color: '#ffffff'});
	}  else if (depth == 3) {
		$(elementId).setStyle({backgroundColor: sub2Bg});
		// Sets the link font color 
		$(elementId).down().setStyle({color: '#ffffff'});
	}
}

// TABBING THROUGH MENU STUFF
function hideSubs(what,withShift) {
	
  if (withShift == 1 && !shifty) {
    return;
  }
  if (withShift == 0 && shifty) {
    return;
  }
  what = (typeof what == "object") ? 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 (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);
		}
	}
	
	// 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);
		}
	}
}

