/**
 * Třída obstarávající hlavní menu
 * @author LD
 */
function MainMenu() {
	/**
	 * Jestli je mainmenu v pořádku
	 * @var boolean
	 */
	this.ok = true;
	this.version = "alpha 1";
	/**
	 * ID objektu menu
	 * @var object
	 */
	this.container_id = 'mainmenu';
	/**
	 * Objekt menu
	 * @var string
	 */
	this.container_element = null;
	/**
	 * Uchovává Id otevřených položek
	 * @var array
	 */
	this.open_items = new Array();
	/**
	 * Určuje, jestli je kurzor nad submenu
	 * @var boolean
	 */
	this.on_submenu = false;
	/**
	 * Čas pro zavření submenu
	 * @var integer (ms)
	 */
	this.timeout = 600;
	/**
	 * Časovač pro zavření submenu
	 * @var object
	 */
	this.timer = new Array();
	
	this.init();
}
/**
 * Inicializace menu
 * @access private
 */
MainMenu.prototype.init = function() {
	this.container_element = document.getElementById(this.container_id);
	if(!this.container_element) {
		alert("Chyba hlavního menu!\n\nMenu container not found :(");
		this.ok = false;
	}
	else {
		children = this.container_element.childNodes;
		for(i in children) {
			if(children[i].tagName == "LI") {
				children[i].onmouseout = "";
			}
		}
	}
}
/**
 * Zavolání otevření menu
 * @access public
 * @param object
 */
MainMenu.prototype.show = function(li_el) {
	if(!this.ok) return;
	//return;
	var children = null;
	var ul_el = null;
	try {
		var id = li_el.id.substr(li_el.id.lastIndexOf('_')+1);
		// najdeme vnořené elementy
		children = li_el.childNodes;
		// projdeme je
		for(c = 0; c < children.length; c++) {
			// pokud existuje submenu
			if(children[c].tagName == "UL") {
				// zobrazíme podmenu
				return this.showSubmenu(children[c].id);
			}
		}
	}
	catch(e) {
		alert("NASTALA NEOČEKÁVANÁ CHYBA V HLAVNÍM MENU!\n\n"+e.message);
	}
}
/**
 * Vyvolání otevření menu
 * @access private
 * @param string
 */
MainMenu.prototype.showSubmenu = function(ul_id) {
	try {
		el = document.getElementById(ul_id);
		el.style.display = 'block';
		clearTimeout(this.timer[ul_id]);
		return true;
	}
	catch(e) {
		alert("NEZDAŘILO SE OTEVŘÍT SUBMENU.\n\n"+e.messsage);
		return false;
	}
}
/**
 * Zavolání zavření menu
 * @access public
 * @param object
 */
MainMenu.prototype.hide = function(ul_el) {
	if(!this.ok) return;
	this.timer[ul_el.id] = window.setTimeout("MainMenuInstance.hideSubmenu('"+ul_el.id+"')", this.timeout);
}
/**
 * Vyvolání zavření menu
 * @access private
 * @param string
 */
MainMenu.prototype.hideSubmenu = function(ul_id) {
	try {
		el = document.getElementById(ul_id);
		el.style.display = 'none';
		return true;
	}
	catch(e) {
		alert("NEZDAŘILO SE ZAVŘÍT SUBMENU.\n\n"+e.messsage);
		return false;
	}
}
/**
 * Přidá do indexu otevřenou položku
 * @access private
 * @param string
 */
MainMenu.prototype.addOpenItemToCache = function(id) {
	exists = false;
	for(i in this.open_items) {
		if(this.open_items[i] == id) exists = true;
	}
	if(!exists) this.open_items.push(id);
}
/**
 * Odstraní zaindexovanou otevřenou položku
 * @access private
 * @param string
 */
MainMenu.prototype.removeOpenItemFromCache = function(id) {
	var tmp = new Array();
	for(i in this.open_items) {
		if(this.open_items[i] != id) {
			tmp.push(id);
		}
	}
	this.open_items = tmp;
}
