//enumerations
var ITEMCLASS = "iclass";
var ITEMCLASSACTIVE = "iclassactive";
var LEVELCLASS = "lvlclass";
var LAYOUT = "layout";
var TEXT = "text";
var SUBLEVEL = "sublvl";
var ITEMS = "items";
var XOFFSET = "xoffset";
var YOFFSET = "yoffset";
var URL = "url";
var URLCLASS = "urlclass";
var URLCLASSACTIVE = "urlclassactive";
var NEWWINDOW = "newwindow";
var STATUSMSG = "statusmsg";

var IDLE = 0;
var ACTIVE = 1;
var DEACTIVATED = 2;

var TIMEOUT = 750;
var TIMEIN = 750;

//globals
var idcounter = 0;
var gvmenu = [];
var timer = null;
var activeItem = null;
var activeMainItem = false;

function MenuLevel(item, parent, menu) {
	this.parent = parent;
	this.items = [];
	this.id = idcounter++;
	this.xpos = null;
	this.ypos = null;
	
	try {
		!und(item[LEVELCLASS]) ? this.lvlclass = item[LEVELCLASS] : this.lvlclass = this.parent.parent.lvlclass;
		!und(item[XOFFSET]) ? this.xoffset = item[XOFFSET] : this.xoffset = this.parent.parent.xoffset;
		!und(item[YOFFSET]) ? this.yoffset = item[YOFFSET] : this.yoffset = this.parent.parent.yoffset;
		!und(item[LAYOUT]) ? this.layout = item[LAYOUT] : this.layout = this.parent.parent.layout;
	} catch (e) {
		var message = "A required attribute for menuLevel, which is a child of menuItem with text: \n";
		message += "  '" + this.parent.text + "'\n";
		message += "could not be found (one of lvlclass, xoffset, yoffset, layout).\n";
		message += "Please check top level menuItems for missing attributes. \n\n";
		!und(e.description) ? message += e.description : message += e;
		alert(message);
		return false;
	}
	
	menu.levels[menu.levels.length] = this; //keep record of all the menuLevels
	
	if (!und(item[ITEMS])) {
		for (var i = 0; i < item[ITEMS].length; i++) {
				this.items[i] = new MenuItem(item[ITEMS][i], this, menu);
		}
	}
	
	this.show = menuLevel_show;
	this.draw = menuLevel_draw;
	this.hide = menuLevel_hide;
}
function menuLevel_hide() {
	this.htmlObj.style.display = "none";
	for (var i = 0; i < this.items.length; i++) {
		if (this.items[i].status != IDLE) {
			this.items[i].deactivate();
		}
	}
}
function menuLevel_show() {
	
	browseroffset = 185;
	
	version=0
	if (navigator.appVersion.indexOf("MSIE")!=-1){
	temp=navigator.appVersion.split("MSIE")
	version=parseFloat(temp[1])
	}
	
	if (version>=5.5) {//NON IE browser will return 0
		if (version < 7){
			browseroffset = 0;
		}
	}
	
	//if (this.xpos == null) {
		if (this.layout == "horizontal") {
			this.xpos = findPosX(this.parent.htmlObj) + this.xoffset - (document.getElementById('global_container').offsetLeft)
		} else {
			this.xpos = findPosX(this.parent.htmlObj) + this.parent.htmlObj.offsetWidth + this.xoffset
		}
		this.htmlObj.style.left = this.xpos + "px"
	//}
	//if (this.ypos == null) {
		if (this.layout == "horizontal") {
			this.ypos = findPosY(this.parent.htmlObj) + this.parent.htmlObj.offsetHeight + this.yoffset + browseroffset
		} else {
			this.ypos = findPosY(this.parent.htmlObj) + this.yoffset + browseroffset
		}
		this.htmlObj.style.top = this.ypos + "px"
	//}
	this.htmlObj.style.display = "block";
}
function menuLevel_draw() {
	document.write("<div id=\"" + this.id + "\" class=\"" + this.lvlclass + "\" >");
	for (var i = 0; i < this.items.length; i++) {
		this.items[i].draw();
	}
	document.write("</div>");
	this.htmlObj = document.getElementById(this.id);
}

function MenuItem(item, parent, menu) {
	this.parent = parent;
	this.status = IDLE;
	this.id = idcounter++;
	this.menuInstance = menu;
	this.sublevel = null;
	
	!und(item[TEXT]) ? this.text = item[TEXT] : this.text = "";
	!und(item[URL]) ? this.url = item[URL] : this.url = "#";
	
	try {
		!und(item[ITEMCLASS]) ? this.iclass = item[ITEMCLASS] : this.iclass = this.parent.parent.iclass;
		!und(item[ITEMCLASSACTIVE]) ? this.iclassactive = item[ITEMCLASSACTIVE] : this.iclassactive = this.parent.parent.iclassactive;
		!und(item[URLCLASS]) ? this.urlclass = item[URLCLASS] : this.urlclass = this.parent.parent.urlclass;
		!und(item[URLCLASSACTIVE]) ? this.urlclassactive = item[URLCLASSACTIVE] : this.urlclassactive = this.parent.parent.urlclassactive;
		!und(item[NEWWINDOW]) ? this.newwindow = item[NEWWINDOW] : this.newwindow = "";
		!und(item[STATUSMSG]) ? this.statusmsg = item[STATUSMSG] : this.statusmsg = this.url;
	} catch (e) {
		var message = "A required attribute for menuItem with text: \n";
		message += "  " + this.text + "\n";
		message += "could not be found (one of iclass, iclassactive, urlclass, urlclassactive).\n";
		message += "Please check top level menuItems for missing attributes. \n\n";
		!und(e.description) ? message += e.description : message += e;
		alert(message);
		return false;
	}
	
	menu.items[this.id] = this;
	
	if (!und(item[SUBLEVEL]))
		this.sublevel = new MenuLevel(item[SUBLEVEL], this, menu);
	
	this.draw = menuItem_draw;
	this.activate = menuItem_activate;
	this.deactivate = menuItem_deactivate;
}
function menuItem_draw() {
	var mouseover = null;
	var mouseout = null;
	var click = null;
	var html = null;
	var href = null;
	
	mouseover = " onmouseover=\"gvmenu['" + this.menuInstance.instancename + "'].activateItem('" + this.id + "')" + "\" ";
	
	/*setTimeout("document.three.hi.value='Three seconds have gone!'",3000)
	mouseover = " onmouseover=\"gvmenu['" + this.menuInstance.instancename + "'].activateItem('" + this.id + "')" + "\" ";*/
	
	mouseout = " onmouseout=\"gvmenu['" + this.menuInstance.instancename + "'].deactivateItem('" + this.id + "')" + "\" ";
	
	if (this.newwindow == "true") {
		click = " onclick=\"window.open('" + this.url + "');return false;\" "
	} else {
		click = " onclick=\"document.location.href = '" + this.url + "'\" "
	}
	
	html = "<div id=\"" + this.id + "\" class=\"" + this.iclass + "\"" + mouseout + click + "><a id=\"a" + this.id + "\" class=\"" + this.urlclass + "\"" + mouseover + ">" + this.text + "</a></div>";
	
	document.write(html);
	this.htmlObj = document.getElementById(this.id);
	this.urlObj = document.getElementById("a" + this.id);
}
function menuItem_activate() {
	if (this.status != ACTIVE) {
		for (var i = 0; i < this.parent.items.length; i++) {
			this.parent.items[i].deactivate();
		}
		
		this.status = ACTIVE;
		this.htmlObj.className = this.iclassactive;
		this.urlObj.className = this.urlclassactive;
		window.status = this.statusmsg;
		if (this.sublevel != null) {
			this.sublevel.show(); //show the child level
		}
		this.menuInstance.settimer(this.id, true);
		
	}
}
function menuItem_deactivate() {
	if (this.sublevel != null) {
		this.sublevel.hide();
	}
	
	this.status = IDLE;
	window.status = "";
	this.htmlObj.className = this.iclass;
	this.urlObj.className = this.urlclass;
}

function Menu(items, parent, aclass) {
	this.instancename = parent;
	this.htmlObj = document.getElementById(parent);
	this.styleClass = this.htmlObj.className;
	this.styleClassActive = "a" + this.styleClass;
	this.levels = [];
	this.items = [];
	
	this.text = this.htmlObj.innerHTML;
	this.htmlObj.className = this.styleClass;
	
	this.mainLevel = new MenuLevel(items, this, this);
	
	this.activateItem = Menu_activateItem;
	this.deactivateItem = Menu_deactivateItem;
	this.show = Menu_show;
	this.settimer = Menu_settimer;
	
	for (var i = 0; i < this.levels.length; i++) {
		this.levels[i].draw();
	}
	
	gvmenu[parent] = this;
	this.htmlObj.onmouseover = function () {
		settimerMainitem(true);
		/*pausecomp(TIMEIN);*/
		gvmenu[parent].show()
		
	}
	
	this.htmlObj.onmouseout = function () {
		settimerMainitem(false)
	}
	
}

/*function pausecomp(millis) {
	date = new Date();
	var curDate = null;
	
	do { var curDate = new Date(); } 
	while(curDate-date < millis);
}*/

function Menu_deactivateItem(item) {
	this.items[item].status = DEACTIVATED;
	this.settimer(this.items[item].id, true);
}
function Menu_activateItem(item) {
	this.items[item].activate();
}
function Menu_show() {
	closeAllMenus();
	this.htmlObj.className = this.styleClassActive;
	
	this.mainLevel.show();
	//this.settimer(null, false);

	hideElement('select');
}
function Menu_settimer(item, checked) {
	if (timer != null)
		clearTimeout(timer);
	if (checked) {
		activeItem = this.items[item];
		timer = setTimeout(checktimer, TIMEOUT)
	} else {
		activeItem = null;
		timer = setTimeout(closeAllMenus, TIMEOUT)
	}
}

function checktimer() {
	if (activeItem != null) {
		if (activeItem.status != ACTIVE)
		closeAllMenus();
	}
}

function settimerMainitem(check) {
	activeMainItem = check;
	if (timer != null)
		clearTimeout(timer);
	timer = setTimeout(checktimerMainitem, TIMEOUT)	
}

function checktimerMainitem() {
	if (!activeMainItem) closeAllMenus()
}

//closes all active menus
function closeAllMenus() {
	for (var i in gvmenu) {
		gvmenu[i].htmlObj.className = gvmenu[i].styleClass;
		gvmenu[i].mainLevel.hide();
		showElement('select');
	}
}
//finds the absolute X coords of the object on the screen in pixels
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}
//finds the absolute Y coords of the object on the screen in pixels
function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}
//checks if obj is undefined
function und(obj){
	return typeof(obj) == 'undefined';
}

var	ie=document.all
var parentTag = "DIV" // used to calculate the positioning of the calender in the first parenttag with the value parentTag

//hides <select> and <applet> objects (for IE only)
function hideElement( elmID, overDiv ){
	if( ie ){
		for( i = 0; i < document.all.tags( elmID ).length; i++ ){
			obj = document.all.tags( elmID )[i];
			if( !obj || !obj.offsetParent ){
    				continue;
			}

			objHeight = obj.offsetHeight;
			objWidth = obj.offsetWidth;

			//obj.style.display = "none";
			obj.style.visibility = "hidden";
		}
	}
}

// unhides <select> and <applet> objects (for IE only)
function showElement( elmID ){
	if( ie ){
		for( i = 0; i < document.all.tags( elmID ).length; i++ ){
			obj = document.all.tags( elmID )[i];
			if( !obj || !obj.offsetParent ){
				continue;
			}
			//obj.style.display = "block";
			obj.style.visibility = "visible";
		}
	}
}