document.observe('dom:loaded',function () {
	if (window.navigator.appVersion.indexOf("IE 6.0") != -1) {
		/* If we are in hell */
		var IE6 = true;
	}

 	function off (e) {
		e.removeClassName("glow");
		e.already = false;
		if (e.sub) {
			Effect.Fade(e.sub, { duration: 0.5, queue: { position: "end", scope: e.sub.identify() } });
			e.sub.shown = false;
		}
	}
	function link (e) { 
		var a = e.firstDescendant();
		if(a && a.tagName == "A") {
			if ((a.href.match(/\/$/) ? a.href + "index.html" : a.href) == (window.location.href.match(/\/$/)  ? window.location.href + "index.html" : window.location.href)) {
				a.addClassName("self");
			}
			e.observe("click", function () { window.location = a.href } );
			e.addClassName("link");
			if (a.href == document.location) {
				e.addClassName("self");
			}
		}
	}
	function ie6_hover (e) {
		e.addClassName("glow");
	}
	function ie6_out (e) {
		e.removeClassName("glow");
	}
	function gettext (e) {
		if (!document.all) {
			return e.firstChild.textContent;
		} else {
			if (e.firstChild.innerText) {
				return e.firstChild.innerText;
			} else {
				return e.firstChild.nodeValue;
			}
		}
	}
	function width (e) {
		var totalwidth=e.getWidth()
		var characters = 0;
		var children = e.childElements()
		children.each(function (i) {
			characters = characters + gettext(i).length;
		});
		var remain = totalwidth - 1;
		for (var i=0,l=children.length; i < l; ++i) {
			var width;
			if (i != l - 1) {
				width = Math.floor(gettext(children[i]).length * (totalwidth / characters)); 
				remain = remain - width;
				if (IE6) { 
					children[i].style.borderLeft = 0;
				}
			} else {
				width = remain; 
			}
			children[i].style.width = width - 1 + "px"; 
			children[i].select("ul").each(function (e) {
				e.style.width = width - 1 + "px";
				if (Prototype.Browser.IE) {
					/* Dirty hack for IE */
					var offset = children[i].cumulativeOffset();
					offset.top = offset.top + parseInt(children[i].getStyle("height"));
					e.style.marginTop = "0px";
					e.style.position = "absolute";
					if (IE6) {
						e.style.top = children[1].cumulativeOffset().top + parseInt(children[1].getStyle("height")) - 8 + "px";
					} else { 
						e.style.top = offset.top + "px";
					}
					e.style.left = "3px";
					e.style.marginLeft = totalwidth - (i != l -1 ? remain : 0) - width + "px";
				}
			});
		}
	}
	function mouseover (e,ev) {

		if (!e.already) {
			e.already = true;
			if (e.parentNode.c && e.parentNode.c != e) {
				off(e.parentNode.c);
			}
			e.addClassName("glow");
			if (IE6) {

			}
			e.observe('mouseout',function () { 
				window.clearTimeout(e.c); 
				e.c = off.delay(0.5,e) 
			}); 

			e.sub = e.firstDescendant();
			if (e.sub && !e.sub.shown) {
				e.sub.shown = true;
				e.sub.effect = Effect.Appear(e.sub, { duration: 0.5, queue: { position: "end", scope: e.sub.identify() } });
//				e.sub.show();
			}
			e.parentNode.c = e;
		} else if (e.c) { 
			window.clearTimeout(e.c); 
			e.c = false;
		}
	}
	/* Apply effects to each ul with class menu */
	$$('ul.menu').each(function (i) { 
		var c = i.childElements()
		width(i);
		c.each(function(i) { 
			link(i);
			if (IE6) {
				i.addClassName("top");
			}
			i.addClassName("tagged");
			var menu = i.firstDescendant();
			if (menu && menu.tagName == "UL") {
				menu.hide();
				i.observe('mousemove', mouseover.curry(i)) 
				menu.childElements().each(link)
				if (IE6) {
					menu.childElements().each(function (e) {
						e.observe('mouseover', ie6_hover.curry(e));
						e.observe('mouseout', ie6_out.curry(e));
					});
				}
			}
		});
		i.next().style.clear = "both";
	});
})

