﻿function NavItem(eContainer) {
	var oThis = this;
	this.eContainer = eContainer;
	this.sId = this.eContainer.id;
	this.sInitClass = eContainer.className;

	if (this.sId && this.sId != '') {
		this.ePanelId = 'panel-' + this.sId;
		this.ePanel = document.getElementById(this.ePanelId);

		if (this.ePanel) {
			/* hover */
			this.eContainer.onmouseover = function() { oThis.eContainer.className = oThis.sInitClass + ' nav-hover'; }
			this.eContainer.onmouseout = function() { oThis.eContainer.className = oThis.sInitClass; }

			/* dropdown */
			this.eArrow = this.eContainer.getElementsByTagName('s')[0];
			if (this.eArrow) {
				this.eArrow.onclick = function(oEvt) { oThis.showPanel(oEvt); }
				/* delete next row! */
				this.eArrow.onmouseover = function(oEvt) { oThis.showPanel(oEvt); }
			}
			this.eStart = this.eContainer.getElementsByTagName('i')[0];
			if (this.eStart) this.eStart.onclick = function(oEvt) { oThis.showPanel(oEvt); }
			this.eBody = this.eContainer.getElementsByTagName('b')[0];
			if (this.eBody) this.eBody.onclick = function(oEvt) { oThis.showPanel(oEvt); }
		}
	
	}
	
	return this;
}

NavItem.prototype.showPanel = function(oEvt) {
	/* hide previous */
	if (oActiveNavItem != this) hideNavPanel();
	/* position */
	var aCurrentPos = getPageXY(this.eContainer);
	this.ePanel.style.left = aCurrentPos[0] - 8;
	this.ePanel.style.top = aCurrentPos[1] - 9;
	/* display */
	this.ePanel.style.display = 'block';
	oActiveNavItem = this;
	if (oEvt && oEvt.stopPropagation) oEvt.stopPropagation();
	if (window.event) window.event.cancelBubble = true;
	return false;
}

function hideNavPanel() {
	if (oActiveNavItem && oActiveNavItem.ePanel) {
		oActiveNavItem.ePanel.style.display = 'none';
		oActiveNavItem = null;
	}
}

var aNavItems = [];
var oActiveNavItem = null;
function initNav() {
	for (var i = 0; (eDiv = document.getElementsByTagName('div')[i]); i++) {
		if (eDiv.className.indexOf('nav') >= 0) {
			aNavItems[aNavItems.length] = new NavItem(eDiv);
		}
	}
}

function addEvent(objElement, strEventType, ptrEventFunc, bPhase) {
	if (!bPhase) bPhase = false;
	if (objElement.addEventListener) objElement.addEventListener(strEventType, ptrEventFunc, bPhase);
		else if (objElement.attachEvent) objElement.attachEvent('on' + strEventType, ptrEventFunc);
}

function getPageXY( oElement ) {
	var iPosX = oElement.offsetLeft;
	var iPosY = oElement.offsetTop;
	while ( oElement.offsetParent != null ) {
		oElement = oElement.offsetParent;
		iPosX += oElement.offsetLeft;
		iPosY += oElement.offsetTop;
		if (oElement.tagName == 'BODY') break;
	}
	return [iPosX, iPosY];
}


addEvent(window, 'load', initNav);
addEvent(document, 'click', hideNavPanel);
addEvent(window, 'resize', hideNavPanel);