// TODO: Add functionality for key presses.

var timer=0;		// Timer id for menu fold.

is_DOM = (document.getElementById) ? true : false;
is_IE = (document.all) ? true : false;
is_IE4 = is_IE && !is_DOM;

var topMenu; //The menubar
var selects; //All select boxes
var hasMenuOutSideWindow = false;
var MenuTopsNotOverrided = 0;

if (is_IE4) {
		topMenu = document.all("tbl0");
		selects = document.all.tags("select");
} else {
		topMenu=document.getElementById("tbl0");
		selects = document.getElementsByTagName("select");
}

function init(ref)
{
	var table = topMenu;
		
	table.NextTable=null;
	table.CurrentItem=null;
	table.onmouseout=outMenu;
	var tds;
	if (is_IE4)
		tds=table.all.tags("td");
	else
		tds=table.getElementsByTagName("td");
		
	for(var i=0;i<tds.length;i++)
	{
		tds[i].table=table;
		tds[i].onmouseover=overItemMenu;		
		var menu=tds[i].getAttribute('name');
		if(menu)
		{
			MenuTopsNotOverrided = DL_GetElementTop(tds[i])+tds[i].offsetHeight;
			tds[i].nextTable=recur(menu,DL_GetElementTop(tds[i])+tds[i].offsetHeight,DL_GetElementLeft(tds[i]),table);			
		}
		else
		{
			// Special event when there are no sub-menus.
			tds[i].onclick=clickItem;
		}
	}

	return table;
}

function recur(callingOjectName,top,left,prev)
{
	var table;
	
	// Opera bug in getElementById had to add "_" before all name attribute 
	// so it could se a diffrence between name and id
	callingOjectName = callingOjectName.replace("_",'');	
	//
	
	if (is_IE4){
		table=document.all(callingOjectName);
	} else {
		table=document.getElementById(callingOjectName);
	}
			
	table.style.visibility='hidden';
	table.style.top = top;
	table.style.left = left;
	table.NextTable=null;
	table.CurrentItem=null;
	table.onmouseout=outMenu;
	var trs;
	if (is_IE4)
		trs=table.all.tags("tr");
	else
		trs=table.getElementsByTagName("tr");
	KeepInWindow(table);
	var topString = table.style.top;
	if (topString.indexOf("px") > -1){
		topString = topString.substring(0, topString.indexOf("px"));
	}
	top = eval(topString);
	
	for(var i=0;i<trs.length;i++)
	{
		trs[i].table=table;
		var menu=trs[i].getAttribute('name');
		
		// Opera bug in getElementById had to add "_" before all name attribute 
		// so it could se a diffrence between name and id
		if(menu != null)
			menu = menu.replace("_",'');
		//

		var menuitem=trs[i].getAttribute('id');
		
		// Safari fix, cant look at table rows to determin the hight and width it must look at each cell.
		var offsetHeight = trs[i].offsetHeight;
		if(offsetHeight == 0) {
			// Just look at the first cell all others shall have the same height.
			offsetHeight = trs[i].getElementsByTagName("td")[0].offsetHeight; 
		}
		
		var offsetWidth = trs[i].offsetWidth;		
		if(offsetWidth == 0) {
			var cells = trs[i].getElementsByTagName("td");
			for(var j = 0; j < cells.length ; j++) {
				offsetWidth += cells[j].offsetWidth;
			}
		}
		// End Safari fix.
		
		if(menu)
		{
			trs[i].nextTable=recur(menu,top,left+offsetWidth);
		}
		else if (menuitem)
		{
			// Set special events when there are no sub-menus.
			trs[i].onclick=clickItem;
		}
		
		if (menu || menuitem)
		{
			trs[i].onmouseover=overItemMenu;
		}
		else
		{
			trs[i].onmouseover=overDivider;
		}
		top += offsetHeight;
	}	
	return table;
}

function KeepInWindow(ref) {  // DOM!!
    var ExtraSpace     = 10;
		var WindowTopEdge  = (is_IE) ? document.body.scrollTop    : window.pageYOffset;
		var WindowHeight   = (is_IE) ? document.body.clientHeight : window.innerHeight;
		var WindowBottomEdge = (WindowTopEdge + WindowHeight) - ExtraSpace;
		var dif = 0;
		
		var topString = ref.style.top;
		if (topString.indexOf("px") > -1){
			topString = topString.substring(0, topString.indexOf("px"));
		}
		var theTop = eval(topString);

		var MenuBottomEdge = theTop + ref.offsetHeight;
		if (MenuBottomEdge > WindowBottomEdge) {
			dif = MenuBottomEdge - WindowBottomEdge;
			ref.style.top = Math.max((theTop - dif), MenuTopsNotOverrided);
			hasMenuOutSideWindow = true;
		}		      
}

function outMenu()
{
	if(timer!=0)
		clearTimeout(timer);
	timer=setTimeout("closeAll()",500);
}

function closeAll()
{
	ref = topMenu;
	
	if(ref.CurrentItem!=null)
	{
		ref.CurrentItem.style.backgroundColor=bgCol;
		ref.CurrentItem.style.color=txtCol;
		ref.CurrentItem.style.backgroundImage='url(images/mb.gif)';				
	}
	eraseNextMenus(ref.NextTable);
	timer=0;

	// Show all dropdown-boxes.
	for(var i = 0; i < selects.length; i++)
		selects[i].style.visibility = 'visible';
}

function overItemMenu()
{
	overMenu(this);
	
	this.table.CurrentItem=this;

	this.style.backgroundColor=bgHoCol;
	this.style.color=txtHoCol;
	this.style.backgroundImage='url()';
	
	
	if(this.nextTable)
	{
		this.nextTable.style.visibility='visible';
		this.table.NextTable = this.nextTable;
	}

	// Hide all dropdown-boxes.
	for(var i = 0; i < selects.length; i++)
		selects[i].style.visibility = 'hidden';
}

function overDivider()
{
		overMenu(this);
}

function overMenu(item)
{
	if(timer!=0)
	{
		clearTimeout(timer);
		timer=0;
	}	
	
	if(item.table.CurrentItem!=null)
	{
		item.table.CurrentItem.style.backgroundColor=bgCol;
		item.table.CurrentItem.style.color=txtCol;
		if (item.table == topMenu)
			item.table.CurrentItem.style.backgroundImage='url(images/mb.gif)';							
	}
	eraseNextMenus(item.table.NextTable);
}

function clickItem()
{
		if (this.trgt == "_blank")
			window.open(this.id);			
		else
			window.location.href = this.id;
		
}

function eraseNextMenus(ref)
{
	while(ref != null)
	{
		if(ref.CurrentItem!=null)
		{
			ref.CurrentItem.style.backgroundColor=bgCol;
			ref.CurrentItem.style.color=txtCol;
			ref.CurrentItem.style.backgroundImage='url()';						
		}
		ref.style.visibility='hidden';
		tmp=ref.NextTable;
		ref.NextTable=null;
		ref=tmp;
	}
}

function accessKeyAction(newHref)
{
	window.location.href = newHref;	
}

function DL_GetElementLeft(eElement)
{
   if (!eElement && this)                    // if argument is invalid
   {                                         // (not specified, is null or is 0)
      eElement = this;                       // and function is a method
   }                                         // identify the element as the method owner

   var nLeftPos = eElement.offsetLeft;       // initialize var to store calculations
   var eParElement = eElement.offsetParent;  // identify first offset parent element

   while (eParElement != null)
   {                                         // move up through element hierarchy
      if(is_IE)                             // if browser is IE, then...
      {
         if(eParElement.tagName == "TD")     // if parent a table cell, then...
         {
            nLeftPos += eParElement.clientLeft; // append cell border width to calcs
         }
      }
      else                                   // if browser is Gecko, then...
      {
         if(eParElement.tagName == "TABLE")  // if parent is a table, then...
         {                                   // get its border as a number
            var nParBorder = parseInt(eParElement.border);
            if(isNaN(nParBorder))            // if no valid border attribute, then...
            {                                // check the table's frame attribute
               var nParFrame = eParElement.getAttribute('frame');
               if(nParFrame != null)         // if frame has ANY value, then...
               {
                  nLeftPos += 1;             // append one pixel to counter
               }
            }
            else if(nParBorder > 0)          // if a border width is specified, then...
            {
               nLeftPos += nParBorder;       // append the border width to counter
            }
         }
      }

      nLeftPos += eParElement.offsetLeft;    // append left offset of parent
      eParElement = eParElement.offsetParent; // and move up the element hierarchy
   }                                         // until no more offset parents exist
   return nLeftPos;                          // return the number calculated
}

function DL_GetElementTop(eElement)
{
   if (!eElement && this)                    // if argument is invalid
   {                                         // (not specified, is null or is 0)
      eElement = this;                       // and function is a method
   }                                         // identify the element as the method owner

   var nTopPos = eElement.offsetTop;         // initialize var to store calculations
   var eParElement = eElement.offsetParent;  // identify first offset parent element

   while (eParElement != null)
   {                                         // move up through element hierarchy
      if(is_IE)                             // if browser is IE, then...
      {
         if(eParElement.tagName == "TD")     // if parent a table cell, then...
         {
            nTopPos += eParElement.clientTop; // append cell border width to calcs
         }
      }
      else                                   // if browser is Gecko, then...
      {
         if(eParElement.tagName == "TABLE")  // if parent is a table, then...
         {                                   // get its border as a number
            var nParBorder = parseInt(eParElement.border);
            if(isNaN(nParBorder))            // if no valid border attribute, then...
            {                                // check the table's frame attribute
               var nParFrame = eParElement.getAttribute('frame');
               if(nParFrame != null)         // if frame has ANY value, then...
               {
                  nTopPos += 1;              // append one pixel to counter
               }
            }
            else if(nParBorder > 0)          // if a border width is specified, then...
            {
               nTopPos += nParBorder;        // append the border width to counter
            }
         }
      }

      nTopPos += eParElement.offsetTop;      // append top offset of parent
      eParElement = eParElement.offsetParent; // and move up the element hierarchy
   }                                         // until no more offset parents exist
   return nTopPos;                           // return the number calculated
}

