var isDOM = (document.getElementById ? true : false); 
var isIE4 = ((document.all && !isDOM) ? true : false);

var ActiveMenu = null;
var TimeoutID = null;
var MenuPath = '';
var ProductMenu = null;
var ProductMenuParent;

var xMousePos = 0;
var yMousePos = 0;

if (document.layers) {
	document.captureEvents(Event.MOUSEMOVE);
	document.onmousemove = captureMousePosition;
} else if (document.all) {
	document.onmousemove = captureMousePosition;
} else if (document.getElementById) {
	document.onmousemove = captureMousePosition;
}

function getRef(id)
{
 if (isDOM) return document.getElementById(id);
 if (isIE4) return document.all[id];
}

function captureMousePosition(e) {
	if (document.layers) {
		xMousePos = e.pageX;
		yMousePos = e.pageY;
	} else if (document.all) {
		xMousePos = window.event.x + document.body.scrollLeft;
		yMousePos = window.event.y + document.body.scrollTop;
	} else if (document.getElementById) {
		xMousePos = e.pageX;
		yMousePos = e.pageY;
	}
}

function AddToMenuPath(path) {
	if (MenuPath == '')
		MenuPath += path;
	else
		MenuPath += '.' + path;
}

function ProcessMenuPath(path) {
	if (TimeoutID != null) {
		clearTimeout(TimeoutID);
		TimeoutID = null;
	}
	if (MenuPath != '') {
		arrMenuPath = MenuPath.split('.');
		for (i = (arrMenuPath.length - 1); i >= 0; i--) {
			if (arrMenuPath[i] != path)
				HideMenuNow(arrMenuPath[i]);
			else
				break;
		}
	}
}

function findPosition( oLink ) {
  if( 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 ShowMenu(mnu, obj, chld) {
	if (mnu == '') {
		HideAllNow();
		return;
	}
	menu = getRef(mnu);
	child = getRef(chld);
	if (child == null) {
		ProcessMenuPath(menu.id);
		return;
	}
	
	ProcessMenuPath(menu.id);
	ActiveMenu = child;
	AddToMenuPath(child.id);
	
	if (ActiveMenu.style.visibility == 'visible')
		return;
	if (mnu == '0') {
		child.style.left = (findPosition(obj)[0] + 140) + 'px';
		tmp = getRef('mnu');
		//child.style.top = (findPosition(tmp)[1] + tmp.offsetHeight - 1) + 'px';
		child.style.top = (findPosition(obj)[1] + 1) + 'px';
	} else {
		/*child.style.left = (findPosition(obj)[0] + menu.offsetWidth - 10) + 'px';*/
		child.style.left = (findPosition(obj)[0] + menu.offsetWidth - 1) + 'px';
		child.style.top = (findPosition(obj)[1]) + 'px';
	}
	child.style.visibility = 'visible';
}

function ShowMainMenu() {
	menu = getRef('0');
	var dock = getRef('dock');
	menu.style.left = findPosition(dock)[0] + dock.offsetWidth;
	menu.style.top = yMousePos-10;//findPosition(dock)[1];
	menu.style.visibility = 'visible';
}

function ShowProducts(pid, parent) {
	var prod;
	if (pid == '')
		return;
	if (ProductMenu != null) {
		ProductMenu.style.visibility = 'hidden';
		ProductMenuParent.style.color = '#0063a1';
	}
	ProductMenu = getRef(pid);
	ProductMenuParent = parent;
	var $vyrdock = findPosition(getRef('vyrdock'));
	parent.style.color = '#e85836';
	ProductMenu.style.top = ((findPosition(parent)[1] + ProductMenu.offsetHeight) <= ($vyrdock[1] + 285)) ? findPosition(parent)[1] : $vyrdock[1] + 285 - ProductMenu.offsetHeight;
	ProductMenu.style.visibility = 'visible';
	
	
	
}

function HideMainMenu() {
	if (document.getElementById('0').style.visibility == 'hidden') return;
	if (TimeoutID != null)
		clearTimeout(TimeoutID);
	TimeoutID = window.setTimeout('HideMainMenuNow();', 250);
}

function HideMainMenuNow() {
	HideAllNow();
	menu = getRef('0');
	menu.style.visibility = 'hidden';
}

function StayOpen(mnu) {
	/*if (ActiveMenu == mnu) {*/
		clearTimeout(TimeoutID);
		TimeoutID = null;
	/*}*/
}

function HideMenu(mnu) {
	if (TimeoutID != null)
		clearTimeout(TimeoutID);
	TimeoutID = window.setTimeout('HideMenuNow(' + mnu + ');',250);
}

function HideAllMenus(mnu) {
	if (TimeoutID != null)
		clearTimeout(TimeoutID);
	TimeoutID = window.setTimeout('HideAllNow();',250);
}

function HideAllNow() {
	if (MenuPath != '') {
		var arrMenuPath = MenuPath.split('.');
		for (i = (arrMenuPath.length - 1); i >= 0; i--)
			HideMenuNow(arrMenuPath[i]);
		MenuPath = '';
	}
	ActiveMenu = null;
}


function HideMenuNow(mnu) {
	manu = getRef(mnu)
	manu.style.visibility = 'hidden';
	var last = MenuPath.lastIndexOf('.');
	if (last > 0)
		MenuPath = MenuPath.substr(0, last);
	else
		MenuPath = '';
}

function email()
    {
	document.write('<a href="mailto:spacek@colortechnik.cz">spacek@colortechnik.cz</a>');
    }
