var dropdownsEnabled = document.getElementById && !document.layers;

startList = function() {
		if(window.buildList) buildList();
		navRoot = document.getElementById("nswAgMenu");
		addHoverEvent(navRoot);
}

var clickAlreadyRegistered = false;

function addHoverEvent(aList) {

	for (i=0; i<aList.childNodes.length; i++) {
		node = aList.childNodes[i];
		if (node.nodeName=="LI" ) {
			node.onmouseover=function() {
				if (this.className=="") this.className="bling";
				else this.className+=" bling";
				childULId = this.getAttribute("childList");
				if (childULId) {
					theChildNode = document.getElementById(childULId);
					addHoverEvent(theChildNode);
					fixHorizontalPosition(theChildNode);
				}
			}
			node.onmouseout=function() {
				if (this.className=="bling") this.className="";
				else this.className=this.className.replace(" bling", "");
			}
			node.onmousedown=function() {
				if (!clickAlreadyRegistered) this.className+=" click";
				clickAlreadyRegistered = true;
			}
			node.onmouseup=function() {
				this.className=this.className.replace(" click", "");
			}			
		}
	}
}


function fixHorizontalPosition(aNode) {
  
  var viewportHeight = xClientHeight();
  var liY = xPageY(aNode.offsetParent );
  var submenuHeight =  xHeight(aNode );
  var scrolledDistance =  xScrollTop();
  
	var distanceFromBottom = viewportHeight + scrolledDistance - (liY + submenuHeight); 
	if (distanceFromBottom < 0) {
		 agSetElementStyle(aNode,'top',new String(distanceFromBottom)+"px");
	} 
}

function xClientWidth() {
	var w = 0;
	if (!window.opera && document.documentElement && document.documentElement.clientWidth)
		w = document.documentElement.clientWidth;
	else if (document.body && document.body.clientWidth)
		w = document.body.clientWidth;
	else if ( xDef(window.innerWidth, window.innerHeight, document.height) ) {
		w = window.innerWidth;
		if (document.height > window.innerHeight) w -= 16;
	}
	return w;
}

function xClientHeight() {
	var h = 0;
	if (!window.opera && document.documentElement && document.documentElement.clientHeight)
		h = document.documentElement.clientHeight;
	else if (document.body && document.body.clientHeight)
		h = document.body.clientHeight;
	else if ( xDef(window.innerHeight, window.innerWidth, document.width) ) {
		h = window.innerHeight;
		if (document.width > window.innerWidth) h -= 16;
	}
	return h;
}

function xScrollLeft() {
	var offset = 0;
	if ( xDef(window.pageXOffset) )
		offset = window.pageXOffset;
	else if ( document.documentElement && document.documentElement.scrollLeft )
		offset = document.documentElement.scrollLeft;
	else if ( document.body && xDef(document.body.scrollLeft ) )
		offset = document.body.scrollLeft;
	return offset;
}

function xScrollTop() {
	var offset = 0;
	if ( xDef(window.pageYOffset) )
		offset = window.pageYOffset;
	else if ( document.documentElement && document.documentElement.scrollTop )
		offset = document.documentElement.scrollTop;
	else if ( document.body && xDef(document.body.scrollTop ) )
		offset = document.body.scrollTop;
	return offset;
}

function xWidth(e, uW) {
	if ( !e ) return 0;
	uW = e.offsetWidth;
	return uW;
}

function xHeight(e, uH) {
	if ( !e ) return 0;
	uH=e.offsetHeight;
	return uH;
}

function xMoveTo(e, iX, iY) {
	xLeft(e,iX);
	xTop(e,iY);
}

function xLeft(e, iX) {
	if ( !(e = xGetElementById(e)) ) return 0;
    if ( xDef(iX) )
		e.style.left = iX + "px";
	else {
    	if ( xDef(e.offsetLeft) )
			iX = e.offsetLeft;
		else
			iX = parseInt(e.style.left);
    	if (isNaN(iX))
			iX = 0;
	}
	return iX;
}

function xTop(e, iY) {
	if ( !(e = xGetElementById(e)) ) return 0;
    if ( xDef(iY) )
		e.style.top = iY + "px";
	else {
		if ( xDef(e.offsetTop) )
			iY = e.offsetTop;
		else
			iY = parseInt(e.style.top);
		if (isNaN(iY))
			iY = 0;
	}
	return iY;
}

function xPageX(e) {
	if ( !(e = xGetElementById(e)) ) return 0;
	var x = 0;
	while (e) {
		if ( xDef(e.offsetLeft) ) x += e.offsetLeft;
		else break;
		e = e.offsetParent;
	}
	return x;
}

function xPageY(e) {
	if ( !(e = xGetElementById(e)) ) return 0;
	var y = 0;
	while (e) {
		if ( xDef(e.offsetTop) ) y += e.offsetTop;
		else break;
		e = e.offsetParent;
	}
	return y;
}

function xDef() {
	for (var i=0; i<arguments.length; ++i) {
		if ( typeof(arguments[i]) == "undefined" )
			return false;
	}
	return true;
}

function xGetElementById(e) {
	if (typeof(e) != "string") return e;
	if (document.getElementById)
		e = document.getElementById(e);
	else
		e = null;
	return e;
}