function regEventHandler(eventName,elem,eventHandler)
{
	if(typeof addEventListener != "undefined")
		elem.addEventListener(eventName,eventHandler, false);
	else if(typeof attachEvent != "undefined")
		elem.attachEvent("on" + eventName,eventHandler);
}

function getEventTarget(e)
{
	if(typeof e.target == "object")
		return e.target;
	else if(typeof e.srcElement == "object")
		return e.srcElement;
}

function preventDefault(e)
{
	if(typeof e.preventDefault != "undefined")
		e.preventDefault();
	else if(e.type != "mouseover")
		e.returnValue = false;
	else
		e.returnValue = true;
}

regEventHandler("click",document,handleClick);

function handleClick(e)
{
		var target = getEventTarget(e);
		var nextelem;
		if(target == null || target.nodeName != "A")
			return;
		if(target.parentNode == null)
			return;
		nextelem = target.parentNode.nextSibling;
		if(nextelem.nodeType == 3)
			nextelem = nextelem.nextSibling;		// ošetření situace v FF
		if(nextelem == null || nextelem.nodeName != "LI")
			return;
		if(nextelem.className.slice(0,5) != "inbox")
			return;									// inbox je následně prověřen
		if(typeof inboxItemSelected != "undefined" && inboxItemSelected == true)
			return;								//  klik na boxroot + vybraná položka inboxu  -> předá PHP
		var inbox = nextelem;						// LI obsahující inbox, zpracuje kliky na kořenové odkazy
		var strs = target.pathname.split("/");
		var string = strs.pop();
		var nextBoxID = string.slice(0,string.lastIndexOf("."));
		if(unwrappedBoxID != "" && unwrappedBoxID != nextBoxID)	//  klik na jiný root
			wrappBox(unwrappedBoxID);				// zavře jiný rozbalený inbox	
		var wrappstate = inbox.className.slice(6);
		if(wrappstate == "unwrapped"){
			var newclass = "inbox wrapped";
			unwrappedBoxID = "";
		}else if(wrappstate == "wrapped"){
			var newclass = "inbox unwrapped";
			unwrappedBoxID = inbox.id;
		}else
			return;	// chyba, ignoruje
		inbox.className = newclass;
		preventDefault(e);
}

function wrappBox(menuID)
{
	var menu = document.getElementById(menuID);
	if(menu != null)
		menu.className = "inbox wrapped";
}
