// JavaScript Document

// Don't change these parameters
var delay        = 550; /////
var menuElement  = new Array ();
var usedWidth    = 0;
var numOfMenus   = 0;
/// ----------------------------

// Array of Menus

var menus = new Array;
menus[0] = "News";


/// ----------------------------

var topID  = -1;

var xPos = 0;
var yPos = 0;

function offsetLeft(o){
    var i = 0;
    while (o.offsetParent!=null) {
        i += o.offsetLeft;
        o = o.offsetParent;
    }
    return i + o.offsetLeft;
}
function offsetTop(o){
    var i = 0;
    while (o.offsetParent!=null) {
        i += o.offsetTop;
        o = o.offsetParent;
    }
    return i + o.offsetTop;
}

function menuPos(ID)
{
	var explorerTest = windowsExplorer();
	
	xPos = offsetLeft(document.getElementById('ref' + ID));
	xSitePos = offsetLeft(document.getElementById('news'));
	
	xPos = xPos - xSitePos;
	
//	explorerTest == true ? yPos = 5 : yPos = 128;
	yPos = offsetTop(document.getElementById('ref' + ID));
	ySitePos = offsetTop(document.getElementById('news'));
	
	yPos = yPos - ySitePos + 15;


}

// constructor of menu elements
function menuConstructor(id)
{
	this.ID            = id; // the Current Menu ID
	this.parentID      = -1; // the Current Menu's Parent Menu ID
	this.parentItemID  = -1; // the Current Menu's Parent Menu Item ID
	this.timerID       = -1; // the Current Menu's Delay timer
	this.isOn          = false; // the Current Menu's Display Status
	this.currItemID    = -1; // the i don't know
	
	/*if(id > 1)
	{
		this.parentID      = 1;
		this.parentItemID  = id - 2;
	}*/
	
	return this;
}

function enterTopItem(ID)
{//alert('enterTopItem');
	if (topID != ID && topID != -1)
	{
		hideTree(topID);
	}
	menuPos(ID);
	releaseTree(ID);
	topID = ID;
	show(ID);
}

function enterSearchOpsItem(ID)
{//alert('enterTopItem');
	if (topID != ID && topID != -1)
	{
		hideTree(topID);
	}
	menuPos(ID);
	releaseTree(ID);
	topID = ID;
	showSearchOps(ID);
}

function enterInfoItem(ID)
{//alert('enterTopItem');
	if (topID != ID && topID != -1)
	{
		hideTree(topID);
	}
	menuPos(ID);
	releaseTree(ID);
	topID = ID;
	showBucket(ID);
}

function exitTopItem(ID)
{//alert('exitTopItem');
	menuElement[ID].timerID = setTimeout('hide("' + ID + '")', delay);
}


// Called by PopMenu Items

function enterItem(menuID, itemID)
{//alert('enterItem');
	var currItemID = menuElement[menuID].currItemID;
	menuPos(2, itemID);
	if (currItemID > -1)	
	{
		hide (currItemID);
	}

	for (var i = 0; i < numOfMenus; i++)
	{
		if (menuElement[menus[i]].parentID == menuID && menuElement[menus[i]].parentItemID == itemID)
		{
			clearTimeout (menuElement[menus[i]].timerID);
			menuElement[menus[i]].timerID = -1;
			show(menus[i]);
			return 0;
		}
	}

	return -1;
}

function exitItem(menuID, itemID)
{//alert('exitItem');
	
	for (var i = 0; i < numOfMenus; i++)
	{
		if (menuElement[menus[i]].parentID == menuID && menuElement[menus[i]].parentItemID == itemID)
		{
			menuElement[menus[i]].timerID = setTimeout ('hide ("' + menus[i] + '")', delay);
			return 0;
		}
	}
	
	/*menuElement[menuID].timerID = setTimeout ('hide (' + menuID + ')', delay);
		
	if (menuElement[menuID].parentID > -1)
	{
		menuElement[menuElement[menuID].parentID].timerID = setTimeout ('hide (' + menuElement[menuID].parentID + ')', delay);
	}*/
}


// Called By Main Div Holders of Nav Peace

function enterMenu(ID)
{//alert('enterMenu');
	var parentID = menuElement[ID].parentID;
	
	if (parentID == -1)
	{
		clearTimeout (menuElement[ID].timerID);
		menuElement[ID].timerID = -1;
	}
	else
	{
		releaseTree(ID);
	}
}

function exitMenu(ID)
{//alert('exitMenu');
	timeoutTree(ID);
}


function hideTree(ID)
{//alert('hideTree');
	if(ID == "all")
	{
		for (var j = 0; j < numOfMenus; j++)
		{
			if (menuElement[menus[j]].isOn)
			{
				hide(menus[j]);
			}
		}
	}
	else
	{
		hide(ID);
		
		for (var j = 0; j < numOfMenus; j++)
		{
			if (menuElement[menus[j]].parentID == ID && menuElement[menus[j]].isOn)
			{
				hideTree(menus[j]);
				return 0;
			}
		}
	}
}

function releaseTree(ID)
{
	clearTimeout(menuElement[ID].timerID);
	menuElement[ID].timerID = -1;

	var parentID = menuElement[ID].parentID;
	
	if (parentID > -1)
	{
		releaseTree(parentID);
	}
}

function timeoutTree(ID)
{//alert('timeoutTree');
	menuElement[ID].timerID = setTimeout ('hide ("' + ID + '")', delay);
	
	var parentID = menuElement[ID].parentID;
	
	if (parentID > -1)
	{
		timeoutTree(parentID);
	}
}


function showHolder()
{//alert('showHolder');
	document.getElementById('MenuHolder').style.display = 'block';
}

function hideHolder()
{//alert('hideHolder');
	document.getElementById('MenuHolder').style.display = "none";
}

function show(ID)
{//alert('show');
	//showHolder();

	document.getElementById('Menu' + ID).style.visibility = 'visible';
	document.getElementById('Menu' + ID).style.left = xPos + 'px';
	document.getElementById('Menu' + ID).style.top =  yPos + 'px';

	menuElement[ID].isOn = true;

	if (menuElement[ID].parentID > -1)
	{
		menuElement[menuElement[ID].parentID].currItemID = ID;
	}
}

function showSearchOps(ID)
{//alert('show');
	//showHolder();

	document.getElementById('Menu' + ID).style.visibility = 'visible';
	
	document.getElementById('Menu' + ID).style.top =  yPos + 'px';
	
	menuElement[ID].isOn = true;

	if (menuElement[ID].parentID > -1)
	{
		menuElement[menuElement[ID].parentID].currItemID = ID;
	}
}

function showBucket(ID)
{//alert('show');
	//showHolder();

	document.getElementById('Menu' + ID).style.visibility = 'visible';
	
	document.getElementById('Menu' + ID).style.left = xPos + 15 + 'px';
	document.getElementById('Menu' + ID).style.top =  yPos - 15 + 'px';
	
	menuElement[ID].isOn = true;

	if (menuElement[ID].parentID > -1)
	{
		menuElement[menuElement[ID].parentID].currItemID = ID;
	}
}

function hide(ID)
{//alert('hide');
	document.getElementById('Menu' + ID).style.visibility = "hidden";
	menuElement[ID].isOn = false;

	if (menuElement[ID].parentID > -1)
	{
		menuElement[menuElement[ID].parentID].currItemID = -1;
	}
	
	var elementOn = 0;
	
	for(var j = 0; j < numOfMenus; j++)
	{
		if(menuElement[menus[j]].isOn)
		{
			elementOn++;
		}
	}
	
	if(elementOn == 0)
	{
		//hideHolder();
	}
}


function createMenuTree()
{
	for (var i = 0; i < menus.length; i++)
	{
		menuElement[menus[i]] = new menuConstructor(i);
		numOfMenus++;
	}
}

createMenuTree ();

