/* www.extreme-divers.lt; 04/07/23; dj <domas at surf dot lt>; */

var d = document;
var w = window;
var m = 'm/';

var openWidget = null;
var openChild = null;

function e(id){
	return d.getElementById(id);
}

function r(url){
	d.location.href = url;
}

function swapClass(itemId, newClass){
	
	if (e(itemId))
		e(itemId).className = newClass;
	
}

function showMenuItem(itemId, evt){
	cancelBubling(evt);
	
	var itemFulllId = 'topMenuGroup'+itemId;
	
	if (openWidget && openWidget != itemFulllId){
		hideChild();
		hideLevel2Child();
	}
	
	var parent = getEventElement(evt);
		
	if (parent && parent.tagName == 'A')
			parent = parent.parentElement;
	
	if (e(itemFulllId)){
		
		d.body.onmouseover= closeMenu;
		d.body.onclick = closeMenu;
		var coords = findPosition(parent);
		
		coords[0] +=8;
		coords[1] += parseInt(parent.height);
		showChild(itemFulllId, coords);
		
		openWidget = itemFulllId;
	}
}

function showMenuItemChild(itemId, evt){
	cancelBubling(evt);

	if (openChild && openChild != 'topMenuGroup'+itemId){
		hideLevel2Child();
	}
	
	if (e('topMenuGroup'+itemId)){
		var parent = getEventElement(evt);
		if (parent && parent.tagName == 'A')
				parent = parent.parentElement;
		
		var coords = findPosition(parent);
		coords[0] +=parseInt(parent.width)-32;
		showChild('topMenuGroup'+itemId, coords, parent);
		openChild = 'topMenuGroup'+itemId;
		
		var itemTable = e('topMenuGroup'+itemId);
		
		for (i = 0; i < itemTable.rows.length; i++)
				itemTable.rows[i].cells[0].onmouseover = cancelBubling;
		
	}
}

function findPosition( oLink ) {
  if( oLink && oLink.offsetParent ) {
    for( var posX = 0, posY = 0; oLink.offsetParent; oLink = oLink.offsetParent ) {
      posX += oLink.offsetLeft;
      posY += oLink.offsetTop;
    }
    return [ posX, posY ];
  } else {
    return [ oLink.x, oLink.y ];
  }
}

function getEventElement(evt) {
	evt = (evt) ? evt : ((window.event) ? window.event : null);
	if (evt) {
		evt.cancelBubble = true
		return (evt.target) ? evt.target : evt.srcElement;
	}
		
}


function showChild(childWidgetId, coords, parent){
	
	var child = e(childWidgetId);
	
	if (child)	{
		child.style.left = coords[0];
		child.style.top = coords[1]
		
		child.style.visibility = 'visible';
		if (parent)
			child.style.zIndex = parent.zIndex + 1;
		
	}
}

function hideLevel2Child(){
	var child = e(openChild);
	
	if (child)	{
		child.style.visibility = 'hidden';
	}
}

function hideChild(){
	var child = e(openWidget);
	
	if (child)	{
		child.style.visibility = 'hidden';
	}
	openWidget = null;
}

function closeMenu(evt){
	d.body.onmouseup = null;
	hideChild();
	hideLevel2Child();
}

function cancelBubling(evt){
	
	evt = (evt) ? evt : ((window.event) ? window.event : null);
	if (evt) {
		evt.cancelBubble = true;
	}
}

