// Copyright (c) 2004. Interfax-CNA.  All rights reserved.

var g_activeMenus = new Array();
var g_menuTimer = null;
var g_mainObj = null;
var g_timerScroll = null;
var g_selectedObj = null;

window.attachEvent("onload", OnLoad);

function OnLoad()
{	
	// Following code is used for selection item into the company card tree
	if (document.getElementById("contentMenuItem") != null)
	{
		g_selectedObj = document.getElementById("contentMenuItem").parentElement.parentElement;
		g_selectedObj.className = "OverItemI";
		classOut = "OutItemI";
		classOver = "OverItemI";
	}
}

////////////////////////////////////////////////////////////////////////////////
//	Show menu for selected item.
////////////////////////////////////////////////////////////////////////////////
function ShowMenu(name, obj, direction)
{
    objLeft   = obj.offsetLeft;
	objTop    = obj.offsetTop;
	objParent = obj.offsetParent;

	while(objParent.tagName.toUpperCase() != 'BODY') 
	{
		objLeft  += objParent.offsetLeft;
		objTop   += objParent.offsetTop;
		objParent = objParent.offsetParent;
	}			
	
	divElem = document.getElementById(name);
	divElem.style.left = objLeft + (direction != null && direction == 1 ? obj.offsetWidth : 0) + (divElem.CorrectX != null ? parseInt(divElem.CorrectX) : 0);
	divElem.style.top  = objTop + (direction != null && direction == 1 ? 0 : obj.offsetHeight) + (divElem.CorrectY != null ? parseInt(divElem.CorrectY) : 0);
		
	divElem.style.display = "";
		
	g_activeMenus[divElem.Level] = divElem;	
	ShowAllItems(divElem);
	fullWidth = divElem.offsetWidth;
	if (document.body.clientHeight < divElem.clientHeight + divElem.style.posTop)
	{
		AddScrollButtons(divElem);		
		divElem.getElementsByTagName('TABLE')[0].style.pixelWidth = fullWidth;
	}
	
}

////////////////////////////////////////////////////////////////////////////////
//	Show all items of menu and remove scroll buttons.
////////////////////////////////////////////////////////////////////////////////
function ShowAllItems(elem)
{	
	var table = elem.getElementsByTagName("TABLE")[0];
	RemoveScrollButtons(table)			
	
	for (i = 0; i < table.rows.length; i++)
		table.rows[i].style.display = "";			
}

////////////////////////////////////////////////////////////////////////////////
//	Remove scroll buttons from menu.
////////////////////////////////////////////////////////////////////////////////
function RemoveScrollButtons(table)
{
	if (table.hasScroll != null && table.hasScroll)
	{
		table.deleteRow(0);
		table.deleteRow(table.rows.length - 1);
		table.hasScroll = false;
	}
}

////////////////////////////////////////////////////////////////////////////////
//	Add scroll buttons (if necessary) for menu.
////////////////////////////////////////////////////////////////////////////////
function AddScrollButtons(elem)
{
	var table = elem.getElementsByTagName("TABLE")[0];
	
	for (i = table.rows.length - 1; i > 0; i--)
	{	
		if (document.body.clientHeight > divElem.clientHeight + divElem.style.posTop + table.rows[0].clientHeight * 2)
			break;			
		table.rows[i].style.display = "none";		
	}
	
	if (i != table.rows.length - 1 && (table.hasScroll == null || !table.hasScroll))
	{
		AddDownScrollButton(table);
		AddUpScrollButton(table);		
		ShowUpScrollButton(table, false);
		
		table.hasScroll = true;
	}
}

function ShowUpScrollButton(table, bShow)
{
	table.cells[0].style.display = (bShow ? "" : "none");
	
	table.hasUpButton = bShow;
}

function ShowDownScrollButton(table, bShow)
{
	table.cells[table.cells.length - 1].style.display = (bShow ? "" : "none");
	
	table.hasDownButton = bShow;
}

function AddDownScrollButton(table)
{
	// Add down scroll button	
	td = table.insertRow(-1).insertCell();
	td.innerHTML = '<IMG align="absMiddle" src="img/scroll_down.gif" border="0" id="imgDiscinfo">';
	td.width = "100%";
	td.align = "center";
	td.classOver = "OverScroll";
	td.classOut	 = "OutSubitemForProducts";
	td.className = "OutSubitemForProducts";		
	td.menu = table;
	td.direction = 2;
	td.attachEvent("onmouseover", OnItemOver);
	td.attachEvent("onmouseout", OnItemOut);		
	td.attachEvent("onmouseover", StartUpTimer);
	td.attachEvent("onmouseout", ClearUpTimer);

	table.hasDownButton = true;		
}
function AddUpScrollButton(table)
{
	// Add up scroll button
	td = table.insertRow(0).insertCell();
	td.innerHTML = '<IMG align="absMiddle" src="img/scroll_up.gif" border="0" id="imgDiscinfo">';
	td.width = "100%";
	td.align = "center";
	td.classOver = "OverScroll";
	td.classOut	 = "OutSubitemForProducts";
	td.className = "OutSubitemForProducts";
	td.menu = table;
	td.direction = 1;
	td.attachEvent("onmouseover", OnItemOver);
	td.attachEvent("onmouseout", OnItemOut);
	td.attachEvent("onmouseover", StartUpTimer);
	td.attachEvent("onmouseout", ClearUpTimer);		
	
	table.hasUpButton = true;
}

////////////////////////////////////////////////////////////////////////////////
//	Start timer for scrolling.
////////////////////////////////////////////////////////////////////////////////
function StartUpTimer()
{
	obj = event.srcElement;
	if (obj.tagName == "IMG")
		obj = obj.parentElement;
		
	window.scrollElement = obj;
	
	switch(obj.direction)	
	{
	case 1:
		g_timerScroll = window.setInterval(OnUpScroll, 100);
		break;
	case 2:
		g_timerScroll = window.setInterval(OnDownScroll, 100);
		break;
	}
}

////////////////////////////////////////////////////////////////////////////////
//	Clear timer of scroll.
////////////////////////////////////////////////////////////////////////////////
function ClearUpTimer()
{
	window.clearInterval(g_timerScroll);
}

////////////////////////////////////////////////////////////////////////////////
//	Scroll up menu.
////////////////////////////////////////////////////////////////////////////////
function OnUpScroll()
{
	table = window.scrollElement.menu;
	info = GetItemsInfo(table);
	
	if (info.first - 1 > 0)
	{		
		if (table.hasDownButton)		
			table.rows[info.first - 1].style.display = "";
			
		table.rows[info.last].style.display = "none";
	}
	
	if (info.first - 2 == 0)
	{
		table.rows[info.last + 1].style.display = "";
		ShowUpScrollButton(table, false);
	}
	
	ShowDownScrollButton(table, true);
}

////////////////////////////////////////////////////////////////////////////////
//	Get information about menu.
////////////////////////////////////////////////////////////////////////////////
function GetItemsInfo(table)
{
	info = new Object;
	nCount = 0;
	
	for (i = 1; i < table.rows.length - 1; i++)
		if (table.rows[i].style.display == "") 
		{
			if (info.first == null)
				info.first = i;
				
			info.last = i;
			
			nCount++;
		}
			
	info.count = nCount;
	
	return info;	
}

////////////////////////////////////////////////////////////////////////////////
//	Scroll down menu.
////////////////////////////////////////////////////////////////////////////////
function OnDownScroll()
{
	table = window.scrollElement.menu;

	info = GetItemsInfo(table);
	
	if (info.last + 1 < table.rows.length - 1)
	{
		if (table.hasUpButton)
			table.rows[info.last + 1].style.display = "";
		table.rows[info.first].style.display = "none";
	}
	
	if (info.last + 1 == table.rows.length - 2)
	{
		table.rows[info.first].style.display = "";
		ShowDownScrollButton(table, false);
	}
	
	ShowUpScrollButton(table, true);
}

////////////////////////////////////////////////////////////////////////////////
//	Prepare for menu hiding.
////////////////////////////////////////////////////////////////////////////////
function HideMenu(num, obj, classOut)
{	
	g_startTime = new Date();
	g_menuTimer = setTimeout("DoHide(" + num + ", true)", 1000);
	
	if (obj != null)
	{
		g_mainObj = obj;
		g_mainObj.classOut = classOut;
	}	
}

////////////////////////////////////////////////////////////////////////////////
//	Hide menu.
////////////////////////////////////////////////////////////////////////////////
function DoHide(num, hideMain)
{
	for (i = 10; i >= num; i--)	
		if (g_activeMenus[i] != null)
		{
			g_activeMenus[i].style.display = "none";			
			g_activeMenus[i] = null;
		}
	
	if (g_mainObj != null && hideMain)
		g_mainObj.className = g_mainObj.classOut;	
}

////////////////////////////////////////////////////////////////////////////////
//	Process the "onmouseover" event for item of menu.
////////////////////////////////////////////////////////////////////////////////
function OnItemOver(obj, classOver, subMenu)
{	
	if (g_menuTimer)
		clearTimeout(g_menuTimer);
	g_menuTimer = null;	
		
	if (obj.tagName == null && classOver == null)
	{
		obj = event.srcElement;
		
		if (obj.tagName == "IMG")
			obj = obj.parentElement;
			
		classOver = obj.classOver;
	}		
				
	if (obj.className == classOver)
	{
		return;
	}
	
	level = GetItemLevel(obj);
	DoHide(level + 1, false);
	
	if (subMenu != null)
		ShowMenu(subMenu, obj, 1);			
		
	obj.className = classOver;	
}

////////////////////////////////////////////////////////////////////////////////
//	Get menu level.
////////////////////////////////////////////////////////////////////////////////
function GetItemLevel(obj)
{
	while (obj.parentElement.tagName != "DIV" && obj.parentElement.tagName != "BODY")
		obj = obj.parentElement;
		
	if (obj.parentElement.Level == null)
		return 0;
	
	return parseInt(obj.parentElement.Level);
}

////////////////////////////////////////////////////////////////////////////////
//	Process the "onmouseout" event of menu item.
////////////////////////////////////////////////////////////////////////////////
function OnItemOut(obj, classOut)
{
	if (obj.tagName == null && classOut == null)
	{
		obj = event.srcElement;
		
		if (obj.tagName == "IMG")
			obj = obj.parentElement;
			
		classOut = obj.classOut;
	}
	
	HideMenu(1, null, null);	
	if (g_selectedObj == null || g_selectedObj != obj)
		obj.className = classOut;
}

////////////////////////////////////////////////////////////////////////////////
//	Process the "onmouseover" event for item of main menu.
////////////////////////////////////////////////////////////////////////////////
function OnMainMenuOver(obj, classOver, divMenu)
{
	bShowMenu = false;
	if (g_menuTimer)
		clearTimeout(g_menuTimer);
		
	if (g_mainObj != null && g_mainObj != obj)
	{
		if (g_activeMenus[1] != null && g_activeMenus[1].style.display == "" && divMenu != null)
			bShowMenu = true;
			
		DoHide(0, true);
		g_mainObj.className = g_mainObj.classOut;
		g_mainObj = obj;		
	}
	
	if (bShowMenu)
		ShowMenu(divMenu, obj)

	obj.className = classOver;
}

////////////////////////////////////////////////////////////////////////////////
//	Process the "onclick" event for item of menu.
////////////////////////////////////////////////////////////////////////////////
function OnItemClick(obj)
{		
	if (g_selectedObj != null)
	{
		g_selectedObj.className = classOut;
		g_selectedObj = obj;
		g_selectedObj.className = classOver;
	}

	if (obj.href != "-1")
		DoHide(1, true);
	else
		return;
	
	if (obj.href != null)
		eval(obj.href);
		
	if (obj.getElementsByTagName("A").length != 0)
		obj.getElementsByTagName("A")[0].click();	
}
