var Nav = Class.create();
Nav.prototype = {
	initialize: function (sec, sub) {
		this.prepare(sec, sub);
	},
	
	prepare: function (sec, sub) {
		var nodes = $("globalnav").childNodes;
		var count = null;
		for (var i = 0; i < nodes.length; i++)
			if (nodes[i].nodeType == 1)
				if (nodes[i].tagName.toLowerCase() == "li") {
					count++;
					var child = nodes[i].childNodes;
					if (child.length > 2) {
						for (var j = 0; j < child.length; j++)
							if (child[j].nodeType == 1)
								if (child[j].tagName.toLowerCase() == "ul") {
									var subnav = child[j];
									if (count==sec) {
										var subcount = null
										for (var m = 0; m < subnav.childNodes.length; m++) {
											if (subnav.childNodes[m].nodeType == 1)
												if (subnav.childNodes[m].tagName.toLowerCase() == 'li') {
													subcount++;
													if (subcount==sub)
														subnav.childNodes[m].childNodes[0].style.fontWeight = 'bold';
												}
												
										}
									}
								} else if (child[j].tagName.toLowerCase() == "a")
									var link = child[j];
						this.link(subnav, link, count, sec);
					} else {
						if (count==sec)
							nodes[i].childNodes[0].style.fontWeight = 'bold';
					}
				}
	},
	
	link: function (subnav, link, count, sec) {
		link.onclick = function() {
			nav.toggle(subnav, link, count, sec);
			return false;
		}
	},
	
	toggle: function (node, link, count, sec) {
		if (!node) location.href = link.href;
		if (node.parentNode.parentNode.id == "globalnav")
			this.hide(sec);
		
		if (node.style.display == "") {
			Effect.BlindUp(node, {duration: 0.2});
			if (count==sec) {
				link.style.fontWeight = 'bold';
			}
		} else {
			Effect.BlindDown(node, {duration: 0.2});
			if (count==sec) {
				link.style.fontWeight = 'normal';
			}
		}
	},
	
	hide: function (sec) {
		var node = $("globalnav");
		var count = null;
		
		for (var i=0; i<node.childNodes.length; i++) {
			var child = node.childNodes[i];
			var last;
			if (child.nodeType == 1) {
				if (child.tagName.toLowerCase() == "li") {
					count++;
					for(var j=0; j<child.childNodes.length; j++) {
						var grandchild = child.childNodes[j];
						if (grandchild.nodeType == 1)
							if (grandchild.tagName.toLowerCase() == "a")
								last = grandchild;
						if (grandchild.nodeType == 1)
							if (grandchild.tagName.toLowerCase() == "ul") {
								if (grandchild.style.display == '') {
									Effect.BlindUp(grandchild, {duration: 0.2});
									if (count==sec) {
										last.style.fontWeight = 'bold';
									}
								}
							}
					}
				}
			}
		}
	}
}

var nav;

function initPage() {
	nav = new Nav(sec, sub);
}

Event.observe(window, 'load', initPage, false);