/* All this code is written by Nathan Weizenbaum unless *
 * otherwise noted. You may freely copy it for any use. *
 * It would be nice to mention me, but not required.    *
 *                                                      *
 * This is known to work well in Firefox 1.5, Opera 9,  *
 * and Internet Explorer 6, at least when paired with   *
 * the relevant styles. Everything but the transparency *
 * effects works in Konqueror. It's suspected that it   *
 * works fine in Safari and Opera 8, but this hasn't    *
 * been tested.                                         */

Element.prependChild = function(parent, node) {
  parent.insertBefore(node, parent.firstChild);
}

/* Warns the user that the document may not be valid.   */
function ValidWarning(location) {
  alert("This document may not actually be valid XHTML or CSS.\nIt is still under development, and being converted\nfrom an invalid website, so some pages may not have\nbeen converted yet and thus may not validate.");
}

var print_friendly = false;
var screens;
Event.observe(window, 'load', init_rsh, false);

function init_rsh() {
	var index = window.location.href.indexOf('?');
	if (index != -1 && window.location.href.substring(index + 1) == "print") {
		printFriendly();
	}
}

function gotoPrint() {
	window.location.href = window.location.href + "?print";
}

function printFriendly() {
  screens = $$('link[media="screen"]');
  for (var i = 0; i < screens.length; i++)
    Element.remove(screens[i]);
      
  var prints = $$('link[media="print"]');
  for (var i = 0; i < prints.length; i++) {
    var newPrint = document.createElement('link');
    newPrint.rel = "stylesheet";
    newPrint.type = "text/css";
    newPrint.media = "screen";
    newPrint.href = prints[i].href;
      
    // This causes the browser to display the new stylsheet.
    var head = document.getElementsByTagName('head')[0];
    head.appendChild(newPrint);
  }
}

function webFriendly() {
	var href = window.location.href;
	var index = window.location.href.indexOf("#");
	if (index != -1) {
		href = href.substring(0, index);
	}
	
	window.location.href = href;
}

var lastMenu;
var deathRowMenu;

/* Checks the browser and returns the menu that the event   *
 * relates to. This is IE's fault for not implementing the  *
 * W3C standard. I do so hope this is fixed in IE7.         *
 *                                                          *
 * About half of this function was written by Peter-Paul    *
 * Koch,  distributed through his fantastic website         *
 * www.quirksmode.org. Go there. Enjoy.                     */
function GetMenu(e) {
  if (!e) var e = window.event;
  var tg = (window.event) ? e.srcElement : e.target
  while (tg.id.slice(-5) != '_link' && tg.id.slice(-5) != '_menu' && tg.nodeName != 'BODY') {
    tg = tg.parentNode;
  }
  return $(tg.id.slice(0, -5) + "_menu");
}

function SetMenus(element) {
  elementNodes = $(element).childNodes;
  
  for (var i = 0; i < elementNodes.length; i++) {
    node = elementNodes[i];
    if (node.id && node.id.slice(-5) == '_link') {
      SetMenu(node);
    }
  }
}

function SetMenu(element) {
  element = $(element);
  var menu = $(element.id.slice(0, -5) + "_menu");
  
  if (menu) {
    Event.observe(element, 'mouseover', OntoElement, false);
    Event.observe(menu, 'mouseover', OntoMenu, false);
    Event.observe(element, 'mouseout', OffOfElement, false);
    Event.observe(menu, 'mouseout', OffOfElement, false);
  }
}

function OntoElement(e) {
  var menu = GetMenu(e);

  if (lastMenu && lastMenu !== menu) {
    CloseMenu(lastMenu);
  }
  lastMenu = menu;
  deathRowMenu = null;
  OpenMenu(menu);
}

function OntoMenu(e) {
  deathRowMenu = null;
}

function OffOfElement(e) {
  var menu = GetMenu(e);
  deathRowMenu = menu;
	
  setTimeout("if (deathRowMenu && deathRowMenu.id == \"" + menu.id
    + "\") { CloseMenu(\"" + menu.id + "\"); "
    + "deathRowMenu = null; }", 500);
}

function OpenMenu(menu) {
  menu = $(menu);
  element = $(menu.id.slice(0, -5) + "_link");
  
  Element.setStyle(menu, { 
    top: element.offsetTop + "px",
    left: (element.clientWidth + 5) + "px",
    "z-index": "2"
  } );
  
  Effect.Appear(menu, {
		duration: 0.3,
		afterFinish: function(e) {
			Element.setStyle(e["element"], 'opacity', 1);
		}
	});
}

function CloseMenu(menu) {
  menu = $(menu); 
  Element.setStyle(menu, { "z-index": "0" } );
  Effect.DropOut(menu, { duration: 0.3 });
}

/* Put default text in the searchbox.  
   Borrowed from the UW homepage
	 by Jack Lee */
	 
	 
var searchplaceholder = ' Enter search terms';

function searchfocus () {	
  var input = $('stext');
  if (Element.hasClassName(input, 'empty')) {
    input.value = '';
    Element.removeClassName(input, 'empty');
  }
}

function searchblur () {
  var input = $('stext');
  if (input.value == '' || input.value == searchplaceholder) {
    Element.addClassName(input, 'empty');
    input.value = searchplaceholder;
  }
}

function submitSearch(e) {
  var input = $('stext');
  if (Element.hasClassName(input, 'empty')) {
    input.value = '';
  }
 
  $('searchbox_011271953735307708656:op4rj5arz-q').submit();
  Event.stop(e);
}

Event.observe(window, "load", function() {
	Event.observe('search_link', 'click', submitSearch, true);
	Event.observe('stext', 'blur', searchblur, false);
	Event.observe('stext', 'focus', searchfocus, false);
	
	// Set up searchbox.
	var input = $('stext');
  Element.addClassName(input, 'empty');
  input.value = searchplaceholder;
 
	SetMenus("link_list");
}, false);