if(!window.Node){
  var Node = {ELEMENT_NODE : 1, TEXT_NODE : 3};
}

function checkNode(node, filter){
  return (filter == null || node.nodeType == Node[filter] || node.nodeName.toUpperCase() == filter.toUpperCase());
}

function getChildren(node, filter){
  var result = new Array();
  var children = node.childNodes;
  for(var i = 0; i < children.length; i++){
    if(checkNode(children[i], filter)) result[result.length] = children[i];
  }
  return result;
}

function getChildrenByElement(node){
  return getChildren(node, "ELEMENT_NODE");
}

function getFirstChild(node, filter){
  var child;
  var children = node.childNodes;
  for(var i = 0; i < children.length; i++){
    child = children[i];
    if(checkNode(child, filter)) return child;
  }
  return null;
}

function getFirstChildByText(node){
  return getFirstChild(node, "TEXT_NODE");
}

function getNextSibling(node, filter){
  for(var sibling = node.nextSibling; sibling != null; sibling = sibling.nextSibling){
    if(checkNode(sibling, filter)) return sibling;
  }
  return null;
}
function getNextSiblingByElement(node){
        return getNextSibling(node, "ELEMENT_NODE");
}

// Menu Functions & Properties

var activeMenu = null;

function showMenu() {
  if(activeMenu){
    activeMenu.className = "";
    var e = getNextSiblingByElement(activeMenu)
    if ( e ) {
		e.style.display = "none";
	}
  }
  if(this == activeMenu){
    activeMenu = null;
  } else {
    this.className = "active";
    var e = getNextSiblingByElement(this)
    if ( e ) {
	    e.style.display = "block";
	}
    activeMenu = this;
  }
  return false;
}

function initMenu(){
  var menus, menu, text, a, i;
  menus = getChildrenByElement(document.getElementById("menu"));
  for(i = 0; i < menus.length; i++){
    menu = menus[i];
    // Allow custom anchor to be used
//    text = getFirstChildByText(menu);
    text = getFirstChild(menu);
    if ( text.tagName != "A" || !text.custom ) {
	    a = document.createElement("a");
	    a.href = "#";
	    menu.replaceChild(a, text);
	    a.appendChild(text);
	} else {
		a = text
	}
    a.onmouseover = showMenu;
    a.onfocus = function(){this.blur()};
  }

	PersistObject.init(
		{
			'loopElements' : function(handler) {
				var menus = getChildrenByElement(document.getElementById("menu"));
				for ( var i = 0; i < menus.length; i++ ) {
					menu = menus[i]
					if ( menu.childNodes.length > 1 ) {
						var subMenu = menu.childNodes[1]
						handler(i, subMenu)
					}
				}
			},
			
			'onGet' : function(o) {
				if ( o.style ) {
					return o.style.display
				}
			},
			
			'onSet' : function(o, value) {
				if ( value && o.style ) {
					o.style.display = value
				}
			},
			
			'onLoad' : function() {
				var i = this.get("activeMenu")
				if ( i ) {
					activeMenu = getChildrenByElement(document.getElementById("menu"))[i].childNodes[0]
				}
			},
			
			'onSave' : function() {
				var menus = getChildrenByElement(document.getElementById("menu"))
				for ( var i = 0; i < menus.length; i++ ) {
					if ( activeMenu == menus[i].childNodes[0] ) {
						this.set("activeMenu", i)
					}
				}
			}
		}
	)
}

if(document.createElement) {
	window.onload = initMenu;
}

function NewWindow(theURL, myname, w, h, scroll) {
var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;
winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
win = window.open(theURL, myname, winprops)
if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

document.writeln("<script src='js/PersistObject.js'></script>")
