/**
 * UniMenu - JavaScript, zajistujici dynamicke akce s jiz vytvorenym HTML stromovym menu
 *
 * @author Daniel Felix Hrouzek
 * @version 2005-04-28
 */

/**
 * Pole, uchovavajici seznam objektu menu (na strance jich muze byt vic)
 */
UM_list = [];

/**
 * Trida pro dynamicke akce s HTML menu
 *
 * Vytvoreni je kompletne generovano na strane PHP - objekt prebira vsechny parametry
 */
function UniMenu(id) {
	
	this.evtProcessed = navigator.userAgent.indexOf('Safari') > -1 ? 'safRtnVal' : 'returnValue';

	this.root = null;
	this.id = id;
	this.timeWatch = null;

	this.autoCollapseSiblings = false;
	this.selfParam = true;
	this.globalParam = '';
	this.timeout = 0;
	this.onOver = false;

	this.collapseAll = UM_collapseAll;
	this.expandAll = UM_expandAll;
	this.collapseAllButMe = UM_collapseAllButMe;

	this.expandByID = UM_expandByID;
	this.collapseByID = UM_collapseByID;
	this.switchByID = UM_switchByID;

	this.hideByID = UM_hideByID;
	this.showByID = UM_showByID;

	this.openURL = UM_openURL;
	this.setProperties = UM_setProperties;
	this.setAutoCollapseSiblings = UM_setAutoCollapseSiblings;

	this.addClassLast = UM_addClassLast;
	this.findRoot = UM_findRoot;
	this.findParentLI = UM_findParentLI;

	UM_list[id] = this;
}



/**
 * Inicializacni metoda
 *
 * Najde prislusny prvek podle predaneho id, projde vsechny jeho uzly a upravi
 * jejich styly, pak nastavi eventy
 */
UniMenu.prototype.init = function() { with (this) {
	if (!document.getElementById) return;

	root = document.getElementById(id);

	if (root) {
		addClassLast(root);

		if (onOver) {
			if (root.addEventListener && navigator.vendor != 'Apple Computer, Inc.') {
				root.addEventListener('mouseover', new Function('e', id + '.mouseover(e)'), false);
			}
			else root.onmouseover = new Function('e', id + '.mouseover(e)');

			if (root.addEventListener && navigator.vendor != 'Apple Computer, Inc.') {
				root.addEventListener('mouseout', new Function('e', id + '.mouseout(e)'), false);
			}
			else root.onmouseout = new Function('e', id + '.mouseout(e)');
		}
		else {
			if (root.addEventListener && navigator.vendor != 'Apple Computer, Inc.') {
				root.addEventListener('click', new Function('e', id + '.click(e)'), false);
			}
			else root.onclick = new Function('e', id + '.click(e)');
		}
	}
}}



/**
 * Metoda pri onclick eventu
 *
 * Pokud je puvodce eventu vetev, sbali ji nebo rozbali (podle predchoziho stavu)
 */
UniMenu.prototype.click = function(e) { with (this) {
	e = e || window.event;
	element = e.srcElement || e.target;

	switchByID(findParentLI(element).id);
}}



/**
 * Metoda pri onmouseover eventu
 *
 * Pokud je puvodce eventu vetev, pokusi se ji rozbalit
 */
UniMenu.prototype.mouseover = function(e) { with (this) {
	e = e || window.event;
	element = e.srcElement || e.target;

	if (!element.tagName.match(/^ul/i)) {
		var parentElement = findParentLI(element);
		if (parentElement) {
			if (timeWatch) {
				clearTimeout(timeWatch);
				timeWatch = null;
			}

			expandByID(parentElement.id);
		}
	}
}}



/**
 * Metoda pri onmouseout eventu
 *
 * Pokud je puvodce eventu vetev, nastavi timeout pro jeji sbaleni
 */
UniMenu.prototype.mouseout = function(e) { with (this) {
	e = e || window.event;
	element = e.srcElement || e.target;

	if (!element.tagName.match(/^ul/i)) {
		var parentElement = findParentLI(element);
		if (!(parentElement.parentNode.tagName.match(/^ul/i) && parentElement.parentNode.id == id)) {
			parentElement = findParentLI(findParentLI(element).parentNode);
		}
		if (parentElement) {
			timeWatch = setTimeout(id + ".collapseByID('" + parentElement.id + "');", timeout);
		}
	}
}}



/**
 * Projde vsechny <<li>> potomky zadaneho elementu a poslednimu nastavi class na 'last'
 */
function UM_addClassLast(element) {
	if (element) {
		var kids = element.childNodes;
		var pravda = false;
		for (var i = kids.length - 1; i >= 0; i--) {
			if (kids[i].tagName && kids[i].tagName.match(/^li/i)) {
				if (!pravda) {
					kids[i].className = 'last';
					pravda = true;
				}
				this.addClassLast(kids[i]);
			}
			if (kids[i].tagName && kids[i].tagName.match(/^ul/i)) {
				this.addClassLast(kids[i]);
			}
		}
	}
}



/**
 * Sbali vechny <<ul>> potomky zadaneho elementu
 */
function UM_collapseAll(element) {
	var lists = element.getElementsByTagName('ul');
	var pomNode;
	for (var ul = 0; ul < lists.length; ul++) {
		lists[ul].style.display = 'none';
   
		pomNode = lists[ul].parentNode;
		if (pomNode) {
			pomNode.className = (pomNode.className.match(/last$/i) ? 'plus-last' : 'plus');
		}
	}
}



/**
 * Rozbali vechny <<ul>> potomky zadaneho elementu
 */
function UM_expandAll(element) {
	var lists = element.getElementsByTagName('ul');
	var pomNode;

	for (var ul = 0; ul < lists.length; ul++) {
		lists[ul].style.display = 'block';
		pomNode = lists[ul].parentNode;
		if (pomNode) {
			pomNode.className = (pomNode.className.match(/last$/i) ? 'minus-last' : 'minus');
		}
	}
}



/**
 * Sbali vechny vetve menu krome te, ve ktere se nachazi zadany element
 */
function UM_collapseAllButMe(element) {
	var pomNode = element;
	var x;

	while (pomNode.parentNode.tagName.match(/^ul/i) || pomNode.parentNode.tagName.match(/^li/i)) {
		if (pomNode.tagName && pomNode.tagName.match(/^li/i)) {
			x = pomNode.nextSibling;
			while (x) {
				if (x.nodeType == 1) this.collapseAll(x);
				x = x.nextSibling;
			}
			
			x = pomNode.previousSibling;
			while (x) {
				if (x.nodeType == 1) this.collapseAll(x);
				x = x.previousSibling;
			}
		}
		pomNode = pomNode.parentNode;
	}
};



/**
 * Najde nejvyssi uroven vetve, ve ktere se nachazi zadany element
 */
function UM_findRoot(element) {
	var pomNode = element.parentNode;
	var x;

	while (pomNode.parentNode.tagName.match(/^ul/i) || pomNode.parentNode.tagName.match(/^li/i)) {
		x = pomNode;
		pomNode = pomNode.parentNode;
	}

	return x;
}



/**
 * Najde nejblizsiho <<li>> rodice zadaneho prvku
 */
function UM_findParentLI(element) {
	var pomNode = element.parentNode;
	var x;

	if (element.tagName.match(/^li/i) && element.id) {
		x = element;
	}
	else {
		while (pomNode.tagName.match(/^div/i) || (pomNode.tagName.match(/^li/i) && pomNode.id)) {
			x = pomNode;
			pomNode = pomNode.parentNode;
		}
	}

	return x;
}



/**
 * Skryje vetev <<li>> zadaneho id
 */
function UM_hideByID(element_id) {
	var elm = document.getElementById(element_id);

	if (elm) {
		if (elm.tagName.match(/^(li)/i)) {
			elm.style.display = 'none';
		}
		else alert('Na uzel se zadanym id (' + element_id +') nelze aplikovat hideByID!');
	}
	else alert('Uzel se zadanym id (' + element_id +') neexistuje!');
}



/**
 * Ukaze vetev <<li>> zadaneho id
 */
function UM_showByID(element_id) {
	var elm = document.getElementById(element_id);
	if (elm) {
		if (elm.tagName.match(/^(li)/i)) {
			elm.style.display = 'block';
		}
		else alert('Na uzel se zadanym id (' + element_id +') nelze aplikovat showByID!');
	}
	else alert('Uzel se zadanym id (' + element_id +') neexistuje!');
}



/**
 * Sbali vetev <<li>> zadaneho id
 */
function UM_expandByID(element_id) {
	var elm = document.getElementById(element_id);
;
	if (elm) {
		
		while (elm) {
			if (elm.tagName.match(/^(input|ul)/i)) break;
	
			if (elm.tagName.match(/^(span|div)/i)) {
				var elm_o = elm.parentNode;
			}
			else {
				var elm_o = elm;
			}
	
			if (this.autoCollapseSiblings) this.collapseAllButMe(elm);

			var uly = elm_o.getElementsByTagName('ul');
	
			
			if (uly.length > 0) {
				var targ = uly[0];
				if (targ.style && targ.style.display == 'none') {
					if (targ.filters && targ.filters[0]){
						targ.filters[0].apply();
						targ.style.visibility = 'visible';
						targ.style.display = 'block';
						targ.filters[0].play();
					}else{
						targ.style.display = 'block';
					}
					elm_o.className = elm_o.className.replace('minus', 'plus');
				}
			}
			break;
	
			elm = elm.parentNode;
		}
	}
	else alert('Uzel se zadanym id (' + my_id +') neexistuje!');
}



/**
 * Rozbali vetev <<li>> zadaneho id
 */
function UM_collapseByID(element_id) {
	var elm = document.getElementById(element_id);

	if (elm) {
		while (elm) {
	/*		if (elm.tagName.match(/^(input|ul)/i)) break; */

			if (elm.tagName.match(/^(span|div)/i)) {
				var elm_o = elm.parentNode;
			}
			else {
				var elm_o = elm;
			}

			var uly = elm_o.getElementsByTagName('ul');
			
					
			if (uly.length > 0) {
				var targ = uly[0];
				if (targ.style) {
					targ.style.display = 'none';
					elm_o.className = elm_o.className.replace('plus', 'minus');
/*					if (this.autoCollapseSiblings && col) this.collapseAllButMe(elm); */
				}
			}
			break;

			elm = elm.parentNode;
		}
	}
	else alert('Uzel se zadanym id (' + my_id +') neexistuje!');
}



/**
 * Sbali nebo rozbali vetev <<li>> zadaneho id (podle predchoziho stavu)
 */
function UM_switchByID(element_id) {
	var elm = document.getElementById(element_id);

	while (elm) {
/*		if (elm.tagName.match(/^(input|ul)/i)) break; */
  
		if (elm.tagName.match(/^(span|div)/i)) {
			var elm_o = elm.parentNode;
		}
		else {
			var elm_o = elm;
		}

		var uly = elm_o.getElementsByTagName('ul');
	
		if (uly.length > 0) {
			var targ = uly[0];
			if (targ.style) {
				var col = (targ.style.display == 'none');
				targ.style.display = col ? 'block' : 'none';
				elm_o.className = elm_o.className.replace(col ? 'plus' : 'minus', col ? 'minus' : 'plus');
				if (this.autoCollapseSiblings && col) this.collapseAllButMe(elm);
			}
		}
		break;
  
		elm = elm.parentNode;
	}
}



/**
 * Otevre zadane URL podle parametru target
 */
function UM_openURL(adresa, target) {
	var parametr;

	if (this.globalParam != '')
		parametr = this.globalParam;

	if (adresa.match(/\?/) && parametr != '') {
		adresa = adresa + '&' + parametr;
	}
	else if (!adresa.match(/\?/) && parametr != '') {
		adresa = adresa + '?' + parametr;
	}

	if (target == '_blank')
		window.open(adresa);
	else if (target == '_self')
		window.location = adresa;
	else if (target == '_top')
		top.location = adresa;
	else if (target == 'parent')
		parent.location = adresa;
	else
		eval('parent'.target+'.location='+adresa);
}



/**
 * Zmeni paranetry prvku se zadanym id
 */
function UM_setProperties(element_id, link, target, title, className) {
	var elm = document.getElementById(element_id);

	if (elm && elm.tagName && elm.tagName.match(/^li/i)) {
		var linky = elm.childNodes;
		for (var i = 0; i < linky.length; i++) {
			if (linky[i].tagName && linky[i].tagName.match(/^a/i)) {
				if (link != undefined)
					linky[i].href = link;
				if (target != undefined)
					linky[i].target = target;
				if (title != undefined) {
					linky[i].firstChild.data = title;
				}
				if (className != undefined)
					linky[i].className = className;

				break;
			}
		}
	}
	else alert('Chyba: metoda setProperties nedostala jako parametr id tagu <li>!');
}



function UM_setAutoCollapseSiblings(param) {
	this.autoCollapseSiblings = param;
}



var chtOldOL = window.onload;

window.onload = function() {
	if (chtOldOL) chtOldOL();
	for (var i in UM_list) UM_list[i].init();
}



