utAddOnload(nvInitMenus);
var gSubmenus = { };
var gNavVariables = { 'zIndex': 1 };
var nvConstants = { 'closeInterval': 500 };
var gSafari = (xUA.indexOf('safari') >= 0);

function nvInitMenus()

{
	var navHolder = xGetElementById('sNavigation');
	var anchors = navHolder.getElementsByTagName('a');
	var anchor = null;
	var subMenu = null;
	var index, key;
	var topMenu = new nvSubmenu(0, -1);
	
	for (index = 0; index < anchors.length; index++) {
		anchor = anchors[index];
		anchor.nvMain = true;
		anchor.pageId = parseInt(anchor.className.replace(/.*pageId([0-9]+).*/, '$1'));
		anchor.parentId = parseInt(anchor.className.replace(/.*parentId([0-9]+).*/, '$1'));
		subMenu = xGetElementById('navMenu' + anchor.pageId);

		if (subMenu)
			anchor.subMenu = new nvSubmenu(anchor.pageId, anchor.parentId);
		else
			anchor.subMenu = null;
	
	}
	
	for (index = 0; index < anchors.length; index++) {
		anchor = anchors[index];
		anchor.ownerMenu = gSubmenus[anchor.parentId];
	}
		
	for (key in gSubmenus) {
		subMenu = gSubmenus[key];
		subMenu.initSelf();
	}
	
	xAddEventListener(document, 'mouseover', nvMouseOver, false);
	xAddEventListener(document, 'mouseout', nvMouseOut, false);
}


function nvMouseOver(evt)

{
	e = new xEvent(evt); 
	t = (e.target.nvMain)?e.target:e.target.parentNode;
	
	if (!xDef(t.nvMain))
		return;
	
	if (t.parentId)
		t.ownerMenu.mouseOver(e);
	if (t.subMenu)
		t.subMenu.mouseOver(e);
		
	xPreventDefault(evt);
	xStopPropagation(evt);
	return false;
}


function nvMouseOut(evt)

{
	e = new xEvent(evt);
	t = (e.target.nvMain)?e.target:e.target.parentNode;
	
	if (!xDef(t.nvMain))
		return;
	
	if (t.subMenu)
		t.subMenu.mouseOut(e);
	else if (t.parentId)
		t.ownerMenu.mouseOut(e);
	
	xPreventDefault(evt);
	xStopPropagation(evt);
	return false;
}


function nvSubmenu(pageId, parentId)

{
	var original = xGetElementById('navMenu' + pageId);
	var popupHolder = xGetElementById('navSubHolder');
	
	this.pageId = pageId;
	this.parentId = parentId;
	gSubmenus[pageId] = this;
	
	if (!this.pageId) {
		this.menuDiv = original;
		this.closeable = false;
		this.top = true;
	} else {
		var innerDiv = document.createElement('div');
		var menuDiv = document.createElement('div');

		this.fullHeight = xHeight(original);
		this.fullWidth = xWidth(original);
		
		menuDiv.className = 'navDropdown';
		menuDiv.id = 'navDropdown' + pageId;
		menuDiv.controller = this;
		innerDiv.className = 'navInner';
		innerDiv.appendChild(original.cloneNode(true));
		menuDiv.appendChild(innerDiv);
		xHide(menuDiv);
		popupHolder.appendChild(menuDiv);
		
		xHeight(innerDiv, this.fullHeight);
		xHeight(menuDiv, this.fullHeight);
		xWidth(innerDiv, this.fullWidth);
		xWidth(menuDiv, this.fullWidth);
		
		xMoveTo(menuDiv, -400, 0);
		xShow(menuDiv);
		
		this.menuDiv = menuDiv;
		this.top = false;
		this.closeable = true;
		this.type = (parentId == 0) ? 'top' : 'sub';
	}	
	this.slideTimeout = 0;
	this.closeTimeout = 0;
	this.state = 'closed';
	this.alive = true;
	this.trigger = null;
	this.reference = 'gSubmenus[' + this.pageId + ']';
	
	this.initSelf = function() {
		var index;
		var anchors = this.menuDiv.getElementsByTagName('a', this.menuDiv);
		var anchor;
				
		this.parentMenu = gSubmenus[this.parentId];
		this.subs = { };
		
		for (index = 0; index < anchors.length; index++) {
			anchor = anchors[index];
			anchor.pageId = parseInt(anchor.className.replace(/.*pageId([0-9]+).*/, '$1'));
			anchor.parentId = parseInt(anchor.className.replace(/.*parentId([0-9]+).*/, '$1'));
			anchor.nvMain = true;
						
			anchor.ownerMenu = this;
			if (anchor.pageId && xDef(gSubmenus[anchor.pageId])) {
				anchor.subMenu = gSubmenus[anchor.pageId];
				gSubmenus[anchor.pageId].trigger = anchor;
				this.subs[anchor.pageId] = anchor;
			} else {
				anchor.subMenu = null;
			}
		}
	
	}
	
	
	this.mouseOver = function(e) {
		if (this.top)	
			return;
		
		switch (this.state) {
			case 'closed'		:	this.slideOpen(e);
									break;
			case 'closing'		:	this.reverseSlide(e);
									break;
			case 'pendingClose'	:	this.maintain(e);
									break;
			case 'waiting'		:	this.maintain(e);
									break;
		}
	}
		
	this.mouseOut = function(e) {
		if (this.top)	
			return;
			
		switch (this.state) {
			case 'open'		:	this.initClose(e);
								break;
		}
	}
		

	this.slideOpen = function(e) {
		var parentX = xPageX(this.parentMenu.menuDiv);
		var parentY = xPageY(this.parentMenu.subs[this.pageId]);
		var t = (e.target.nvMain)?e.target:e.target.parentNode;
		
		if (this.type == 'sub') {
			xWidth(this.menuDiv, 1);
			xHeight(this.menuDiv, this.fullHeight);
			this.interval = 200;
			this.pageX = parentX + xLeft(t) + xWidth(t)-2;
			this.pageY = parentY + xTop(t)-1;
		} else {
			xWidth(this.menuDiv, this.fullWidth);
			xHeight(this.menuDiv, 1);
			this.interval = 100 + (this.fullHeight * 2);
			this.pageX = xPageX(t);
			this.pageY = xPageY(t) + xHeight(t);
		}
		xZIndex(this.menuDiv, gNavVariables.zIndex++);
		xMoveTo(this.menuDiv, this.pageX, this.pageY);
		
		this.menuDiv.onSlideComplete = null;
		
		utSlideCornerTo(this.menuDiv, 'se', this.pageX + this.fullWidth, this.pageY + this.fullHeight, this.interval);
		this.state = 'open';
	}

	this.slideClosed = function() {
		var direc = (this.type == 'sub') ? 'ne' : 'sw';	
		
		this.state = 'closing';
		this.menuDiv.onSlideComplete = this.reference + '.finaliseClose();';
		utSlideCornerTo(this.menuDiv, direc, this.pageX, this.pageY, this.interval);
	}
	
	this.finaliseClose = function() {
		this.state = 'closed';
		xMoveTo(this.menuDiv, -800, 0);
		if (this.parentId) {
			if (this.parentMenu.state == 'waiting')
			this.parentMenu.slideClosed();
		}
	}
	
	this.reverseSlide = function(e) {
		var d = new Date();
		var interval = (this.menuDiv.C + this.menuDiv.slideTime) - d.getTime();
		
		clearTimeout(this.menuDiv.slideTimeout);
		this.menuDiv.onSlideComplete = null;
		this.menuDiv.moving = false;
		this.maintain();
		
		utSlideCornerTo(this.menuDiv, 'se', this.pageX + this.fullWidth, this.pageY + this.fullHeight, interval);
	}

	this.maintain = function(e) {
		this.state = 'open';
		if (this.closeTimeout)
			clearTimeout(this.closeTimeout);
		this.closeTimeout = 0;
		if (this.parentId)
			this.parentMenu.maintain();
		//this.setChainState('open');
	}
	
	this.initClose = function(e) {
		if (this.closeTimeout) clearTimeout(this.closeTimeout);
		this.closeTimeout = setTimeout(this.reference + '.slideClosed();', nvConstants.closeInterval);
		this.state = 'pendingClose';
		this.setChainState('waiting');
	}
	
	this.setChainState = function (state) {
		if (this.parentId) {
			this.parentMenu.state = state;
			this.parentMenu.setChainState(state);
		}
	}

}

