var ua = navigator.userAgent.toLowerCase();
var isIE = (ua.indexOf('msie') != -1 && !this.isOpera && (ua.indexOf('webtv') == -1) );  


// obecne funkce
function appendChild(tag, attributes, parent) {
	try{
		newnode = document.createElement(tag);
		for(i in attributes) {
			newnode[i] = attributes[i];
		}
	
		parent.appendChild(newnode);

		return newnode;
	}
	catch(e) {
	}
}

// object MenuLink
function MenuLink(name, url) {
	this.name = name;
	this.url = url;
}

MenuLink.prototype.writeMenu = function () {
	var out = '';
	out += '<li><a href="'+this.url+'">'+this.name+'</a>';
	document.write(out);
}

// link nemuze byt menu -> vzdycky vraci null
MenuLink.prototype.findMenu = function (name) {
	return null;
}

// object Menu
function Menu(name, url, id) {
	this.name = name;
	this.url = url;
	this.links = new Array();
	this.id = id;
	this.noHide = false
}

// prida podmenu
Menu.prototype.addMenu = function(menu) {
	this.links[this.links.length] = menu;
	menu.parent = this
}

// prida odkaz
Menu.prototype.addLink = function(name, url) {
	var link = new MenuLink(name, url)
	link.parent = this
	this.links[this.links.length] = link;

}

// najde objekt menu podle id
Menu.prototype.findMenu = function(id) {
	if(this.id == id)
		return this;
		
	var i;
	var ret = null;
	for(i=0;i<this.links.length;i++) {
		if((ret = this.links[i].findMenu(id)) != null)
			return ret;
	}
	
	return null;
}

Menu.prototype.show = function(link)  {
	try {
		menuroot.out = false
	
		var i,newdiv,newul,newli,newlink;
		var id = parseInt(link.id.substring(5))
		var submenu = this.findMenu(id);
		
		if (submenu == false) {
			return false;
		}
	
		if (link.parentNode.lastChild.tagName == 'DIV') {
			return false;
		}
	
		if (isIE) {
			var selects = document.getElementsByTagName('select');
			if (selects.length > 0) {
				for(i = 0; i < selects.length; i++) {
					selects[i].style.visibility = 'hidden';
				}
			}
		}
		
		var old_menu = current_menu;
		current_menu = submenu.id;
		
		this.hide(old_menu); // skryjeme aktualne zobrazene menu
		
		link.parentNode.className += ' rozb';
	
		// obalujici div
		newdiv = document.createElement('DIV');
	
		// ul noveho submenu
		newul = appendChild('ul', {}, newdiv);
	
		for(i=0;i<submenu.links.length;i++) {
			newli = appendChild('li', {}, newul);
	
			if(submenu.links[i].id)
				newlink = appendChild('a', {href : submenu.links[i].url, innerHTML : submenu.links[i].name, id : "hmenu" + submenu.links[i].id, className : "rozb", onclick : function(event) { window.location=this.href; }, onmouseover : function(event) { try{menuroot.show(this); return false;} catch(e){} }, onmouseout : function(event) { try{menuroot.hideAll(); return false;} catch(e){} }}, newli);
			else
				newlink = appendChild('a', {href : submenu.links[i].url, innerHTML : submenu.links[i].name, onmouseover : function(event) { try{menuroot.remain(this); return false;} catch(e){} }, onmouseout : function(event) { try{menuroot.noHide = false; menuroot.hideAll(); return false;} catch(e){} }}, newli);
	
		}
	
		link.parentNode.appendChild(newdiv);
	}
	catch(e) {
	}
	
}

Menu.prototype.hideAll = function() {
	try {
		this.out = true;
		setTimeout('menuroot.hideAll2(' + current_menu + ')', 20);
	}
	catch(e) {
	}
}

Menu.prototype.hideAll2 = function(id) {
	try {
		if (this.out == true && id == current_menu && !menuroot.noHide) {
			current_menu = null;
			menuroot.hide(id);
		}
	}
	catch(e) {
	}
}

Menu.prototype.hide = function(id) {
	try {
		var menu = menuroot.findMenu(id);
		
		if (menu == null) {
			return false;
		}
	
		var path = new Array();
		while (menu.parent != undefined) {
			path.unshift(menu.id);
			menu = menu.parent;
		}
	
		var menu2 = menuroot.findMenu(current_menu);
		var path2 = new Array();
	
		if (current_menu == null && isIE) {
			var selects = document.getElementsByTagName('select');
			if (selects.length > 0) {
				for(i = 0; i < selects.length; i++) {
					selects[i].style.visibility = '';
				}
			}
		}
	
		if (menu2 != null) {
			while (menu2.parent != undefined) {
				path2.unshift(menu2.id);
				menu2 = menu2.parent;
			}
		}
	
		while (path.length > 0 && path2.length > 0 && path[0] == path2[0]) {
			path.shift();
			path2.shift();
		}
		
	
		if (path.length > 0) {
			var nodeFromToHide = document.getElementById('hmenu' + path[0]).parentNode;
			var kids = nodeFromToHide.childNodes;
			nodeFromToHide.className = nodeFromToHide.className.replace(/rozb/, '');
			for (var i = kids.length - 1; i >= 0; i--) {
				if (kids[i].tagName == 'DIV') {
					nodeFromToHide.removeChild(kids[i]);
				} else {
					break;
				}
			}
		}
	
		return true;
	}
	catch(e) {
	}
}

Menu.prototype.getMenuPath = function() {
	if (!current_menu)
		return false;

	var path = array();

	var node = document.getElementById('hmenu' + current_menu);
	while (node.parentNode != null) {
		node = node.parentNode;
		
	}
}

Menu.prototype.remain = function(obj) {
	try {
		var parLi = obj.parentNode.parentNode.parentNode.parentNode;
		for (var i = 0; i < parLi.childNodes.length; i++) {
			if (parLi.childNodes[i].tagName == 'A') {
				id = parseInt(parLi.childNodes[i].id.substring(5));
				var old_menu = current_menu;
				current_menu = id;
				this.hide(old_menu);
			}
		}
		menuroot.noHide = true;
	}
	catch(e) {
	}
}

//var menu_id = 1;
var current_menu = null;
