/**
 * vypis debug hlasok
 */ 
var debug_enabled = false;

var ie;
if(document.all)
	ie = true;
else
	ie = false;

/**
 * Pomocna funkcia urcena na debug
 * vyzaduje mat niekde na stranke nejaky objekt
 * co ma id debug_div a donho vypisuje logy
 */
function _log(text) {
	if (debug_enabled && document.getElementById("debug_div")) {
		var out = document.getElementById("debug_div");
		out.innerHTML = out.innerHTML + text + "<br/>";
	}
}

/*
 * zistiju sirku nejakeho html objektu
 * kompenzuje rozdiely medzi ie a ff
 */ 
function _setObjectWidth(obj,value) {
	if(ie)
		obj.style.pixelWidth = value;
	else
		obj.style.width = value + "px;"; 
}

/*
 * zistiju vysku nejakeho html objektu
 * kompenzuje rozdiely medzi ie a ff
 */	
function _setObjectHeight(obj,value) {
	if(ie)
		obj.style.pixelHeight = value;
	else
		obj.style.height = value + "px;"; 
}
/*
 * trieda, ktora spravuje jedno menu
 */	
function Popup(meno,place) {
	// vlastny div zo stranky
	var div = document.createElement("div");
	var children = new Array();
	
	// pozicia
	var left = 0;
	var top = 0;
	var width = 0;
	var height = 0;
	
	// za kolko sa automaticky schova ak sa spusti hiding
	var hide_timeout = 1000;
	
	// viditelnost
	var visible = false;
	var always_visible = false;
	
	// ci ma zobrazovat oramovanie okolo
	var frameVisible = true;
	
	// jednoznacny identifikator
	var _meno = meno;
	
	// vyska riadka
	this.rowHeight = "13";
	
	// setter pre frameVisible
	this.setFrameVisible = function(val) {
	   frameVisible = val;
	};
	
	// getter pre frameVisible
	this.getFrameVisible = function() {
	   return frameVisible;
	};
	
	this.setAlwaysVisible = function(value) { 
		div.style.position = "relative";
		always_visible = value; 
	};
	this.getAlwaysVisible = function() { return always_visible; }; 
	
	this.getLeft = function() { return div.offsetLeft; };
	this.getTop = function() { return div.offsetTop; }; 
	this.getWidth = function() { return div.offsetWidth; };
	this.getHeight = function() { return div.offsetHeight; };
	
	this.getName = function() { return _meno; };
	
	// pridam ho do dokumentu ale ako skryty;
	_loadMyDimension();
	_setVisible(visible);
	//_setTop(top);
	//_setLeft(left);
	
	div.style.position = "absolute";
	div.className = "MenuPopup";
	
	var _previousOnBodyHandler = null;
	function _addMeToBody() {
	   place.appendChild(div);
	   if(_previousOnBodyHandler)
	       _previousOnBodyHandler();
	}
	
	if(ie) { // ak je to ie tak musim pridavat do body az ked sa nahra stranka
	   _previousOnBodyHandler = document.body.onload;
	   document.body.onload = _addMeToBody;
	} else {
	  _addMeToBody(); 
	}

	
	function _logme(text) {
		_log(_meno + ". " + text);
	}
	
	/*
	 * nahra rozmery tohto menu 
	 */
	function _loadMyDimension() {
		left = div.offsetLeft;
		top = div.offsetTop;
		width = div.offsetWidth;
		height = div.offsetHeight;
	}
	
	function _setLeft(value) { 
		left = value;
		if(ie) 
			div.style.pixelLeft = left;
		else
			div.style.left = left + "px;";
	}
	function _setTop(value) {  
		top = value;
		if(ie)
			div.style.pixelTop = top ;//+ "px;"; 
		else
			div.style.top = top + "px;"; 
	}
	function _setWidth(value) { 
		width= value;
		if(ie)
			div.style.pixelWidth = width;
		else
			div.style.width = width + "px;"; 
	}
	
	
	function _setHeight(value) { 
		height= value;
		if(ie)
			div.style.pixelHeight = height;
		else
			div.style.height = height + "px;";
	}
	
	this.getClientWidth = function() {
		return width-21;
	};
	
	this.setLeft = _setLeft;
	this.setTop = _setTop; 
	this.setWidth = _setWidth;
	this.setHeight = _setHeight;
	
	
	/* zobrazi dany popup window
	 * 
	 */
	this.show = function() {
		_setVisible(true);
	};
	
	/* skryje dany popup window
	 */
	this.hide = function() {
		_setVisible(false);
	};
	
	/* vrati objekt div
	 */
	this.getDiv = function() { return div; }
	
	/* nastavi visibilitu objektu */
	function _setVisible(state) {
		if(state)
			div.style.display = "";
		else
			div.style.display = "none";
		visible = state;
	}
	
	this.setVisibility = _setVisible; 
	this.getVisibility = function() { return visible; };
	
	
	// Pridaj MenuItem do do tohto menu
	function _addMenuItem(item) {
//		item.setParent(this);
		children[children.length] = item;
	}
	
	this.addMenuItem = _addMenuItem;
	
	
	var drawn = false;
	this.isDrawn = function () { return drawn; };
	
	// vytvori jedno menu a nastavi jeho vzhlad
	// ak chcem menit vzhlad tu je to najlepsie
	//TODO presunut vacsinu do potomkov tejto triedy
	//     alebo do stylu
	
	function _draw() {
		drawn = true;
		table = document.createElement("table");
		table.style.borderSpacing = "0";
		table.style.borderCollapse = "collapse";		
		body = document.createElement("tbody");
		table.appendChild(body);
		// pridam vrch
		if(frameVisible) {
		    top_tr = document.createElement("tr");
			var td_top_left = document.createElement("td");
				td_top_left.className = "MenuFrame";
			var img_top_left = document.createElement("img");
			
			_setObjectWidth(td_top_left,8);
			_setObjectHeight(td_top_left,8);
			img_top_left.src = "htmlplugins/menu/images/top_left.gif";
			td_top_left.style.paddingLeft = "0";
    		td_top_left.style.paddingRight = "0";
			td_top_left.style.paddingBottom = "0";
			td_top_left.style.paddingLeft = "0";
			td_top_left.appendChild(img_top_left);
			top_tr.appendChild(td_top_left);
			
			var td_top_line = document.createElement("td");
				td_top_line.className = "MenuFrame";
			td_top_line.style.padding = "0";
			_setObjectWidth(td_top_line,width-21);
			_setObjectHeight(td_top_line,8);
			td_top_line.style.backgroundImage = "url(htmlplugins/menu/images/top_line.gif)";
			td_top_line.style.backgroundRepeat = "repeat-x";
			//td_top_line.innerHTML = "&nbsp;";
			
			top_tr.appendChild(td_top_line);
			
			var td_top_right = document.createElement("td");
				td_top_right.className = "MenuFrame";
			var img_top_right = document.createElement("img");
			
			_setObjectWidth(td_top_right,13);
			_setObjectHeight(td_top_right,8);
			img_top_right.src = "htmlplugins/menu/images/top_right.gif";
			
			td_top_right.appendChild(img_top_right);
			top_tr.appendChild(td_top_right);
    		body.appendChild(top_tr);
    	}
		for(i=0;i<children.length;i++) {
			var ptr = children[i].getTR(frameVisible);
			_setObjectHeight(ptr,this.rowHeight);
			body.appendChild(ptr);
		}
		div.appendChild(table);
		
		if(frameVisible) {
		bottom_tr = document.createElement("tr");
			var td_bottom_left = document.createElement("td");
				td_bottom_left.className = "MenuFrame";
			var img_bottom_left = document.createElement("img");
			
			_setObjectWidth(td_bottom_left,8);
			_setObjectHeight(td_bottom_left,13);
			img_bottom_left.src = "htmlplugins/menu/images/bottom_left.gif";
			td_bottom_left.style.paddingLeft = "0";
    		td_bottom_left.style.paddingRight = "0";
			td_bottom_left.style.paddingBottom = "0";
			td_bottom_left.style.paddingLeft = "0";
			td_bottom_left.appendChild(img_bottom_left);
			bottom_tr.appendChild(td_bottom_left);
			
			var td_bottom_line = document.createElement("td");
			td_bottom_line.className = "MenuFrame";
			td_bottom_line.style.padding = "0";
			_setObjectWidth(td_bottom_line,width-21);
			_setObjectHeight(td_bottom_line,13);
			td_bottom_line.style.backgroundImage = "url(htmlplugins/menu/images/bottom_line.gif)";
			td_bottom_line.style.backgroundRepeat = "repeat-x";
			//td_bottom_line.innerHTML = "&nbsp;";
			
			bottom_tr.appendChild(td_bottom_line);
			
			var td_bottom_right = document.createElement("td");
				td_bottom_right.className = "MenuFrame";
			var img_bottom_right = document.createElement("img");
			
			_setObjectWidth(td_bottom_right,13);
			_setObjectHeight(td_bottom_right,8);
			img_bottom_right.src = "htmlplugins/menu/images/bottom_right.gif";
			
			td_bottom_right.appendChild(img_bottom_right);
			bottom_tr.appendChild(td_bottom_right);
		body.appendChild(bottom_tr);
		}
	}
	
	this.draw = _draw;
	
	// skryje vsetky podmenu
	// ktore su otvorene od tohto menu dalej
	// toto menu zostane otvorene
	function _hideAllSubMenus() {
		var i;
		for(i=0;i<children.length;i++)
			children[i].hideSubMenu();
	}
	
	this.hideAllSubMenus = _hideAllSubMenus;
	
	// zisti ci na nejakom z jeho podmenu je myska
	function _hasmouse() {
		var i=0;
		for(i=0;i<children.length;i++) {
			if(children[i].hasmouse()) {
				return true;
			}

		}
		return false;
	}
	
	this.hasmouse = _hasmouse;
	
	var hide_timer;
	function _onmouseout() {
		var handler = function() {
			  if(!_hasmouse()) {
				_hideAllSubMenus();
				if (!always_visible) _setVisible(false);
			  } else {
			  	_onmouseout();
			  }
		};
		hide_timer = setTimeout(handler,hide_timeout);
	}
	
	function _onmouseover() {
		// naco to zbytocne natahovat;
		clearTimeout(hide_timer);
	}
	
	div.onmouseover = _onmouseover;
	div.onmouseout = _onmouseout;
				
}

function MenuItem(meno,cont,par) {
	// v akom menu sa nachadzam;
	var parent = par;
	// obsah
	var content;
	// Menu
	var submenu = null;
	// vlastna polozka
	var my_tr,my_cell,my_arrow,img_arrow;
	var _meno = meno
	this.name = _meno;
	
	_createTags(); // vytvorim HTML strukturu
	_setContent(cont);
	_setSubMenu(submenu);
	
	function _logme(text) {
		_log(parent.getName() + "." +_meno + ". " + text);
	}
	
	function _getContent() {
		return content;
	}
	
	function _setContent(value) {
		content = value;
	}
	
	this.OnSetContent = null;
	this.setContent = _setContent;
	this.getContent = _getContent;
	
	function _draw() {
		my_cell.innerHTML = content;
	}
	
	function _createTags() {
		my_tr = document.createElement("tr");
		if(parent.getFrameVisible()) {
    		// vytvorim lave oramovanie
    		left_vert_td = document.createElement("td");
    		left_vert_td.style.backgroundImage = "url(htmlplugins/menu/images/left_vert.gif)";
    		left_vert_td.style.backgroundRepeat = "repeat-y";
    		_setObjectWidth(left_vert_td,8);
    		my_tr.appendChild(left_vert_td);	
    		left_vert_td.className = "MenuFrame";
    	}
		// vytvorim vlastny obsah
		my_cont_table = document.createElement("table");
//		my_cont_table.style.borderCollapse = "collapse";
		my_cont_table.style.borderSpacing = "0";
		my_cont_tbody = document.createElement("tbody");
		my_cont_table.appendChild(my_cont_tbody);
		my_cont_tr = document.createElement("tr");
		my_cont_tbody.appendChild(my_cont_tr);
		my_cont_table.className = "MenuItemContent";
		
		my_cell = document.createElement("td");
		my_cell.style.padding = "0px";
		_setObjectWidth(my_cell,parent.getClientWidth()-7);
		my_arrow = document.createElement("td");
		_setObjectWidth(my_arrow,7);
		my_arrow.setClassName = "MenuItem";

		my_cont_tr.appendChild(my_cell);
		my_cont_tr.appendChild(my_arrow);
		
		// nastavim pozadie
		my_cont_tr.className = "MenuItem";
//		my_cont_tr.style.backgroundImage = "url(box_menu02.png)";
//		my_cont_tr.style.backgroundRepeat = "repeat-y";
		
		img_arrow = document.createElement("img");
		img_arrow.src = "htmlplugins/menu/images/arrow.gif";
		my_arrow.appendChild(img_arrow);
		
		content_td = document.createElement("td");
		content_td.className = "MenuItem";
		content_td.appendChild(my_cont_table);
		my_tr.appendChild(content_td);
		if(parent.getFrameVisible()) {
    		// vytovrim prave oramovanie
    		right_vert_td = document.createElement("td");
    		right_vert_td.style.backgroundImage = "url(htmlplugins/menu/images/right_vert.gif)";
    		right_vert_td.style.backgroundRepeat = "repeat-y";
    		right_vert_td.className = "MenuFrame";
    		_setObjectWidth(right_vert_td,13);
    		my_tr.appendChild(right_vert_td);
        }
	}
	
	function _MouseOver() {
		img_arrow.src = "htmlplugins/menu/images/arrow_sel.gif";
	}
	
	function _MouseOut() {
		img_arrow.src = "htmlplugins/menu/images/arrow.gif";
	}
	
	function _getTR() {
		_draw();
		return my_tr;
	}
	
	this.getTR = _getTR;
	
	function _setParent(par) {
		parent = par;
	}
	
	function _getParent() {
		return parent;
	}
	
	this.getParent = _getParent;
//	this.setParent = _setParent;
	
	function _setSubMenu(value) {
		submenu = value;
		if(submenu==null)
			img_arrow.style.display="none";
		else
			img_arrow.style.display="";
	}
	
	function _getSubMenu() {
		return submenu;
	}
	
	this.getSubMenu = _getSubMenu;
	this.setSubMenu = _setSubMenu;
	
	function _getTop() {
		return parent.getTop() + my_tr.offsetTop;
	}
	
	function _getLeft() {
		return parent.getLeft();
	}
	
	function _getWidth() {
		return parent.getWidth();
	}
	
	// ci ma reagovat na hideSubMenu
	var care_about_hide = true;
	
	// skryje ostatne podmenu
	// a zobrazi priradene podmenu
	function _showSubMenu() {
		care_about_hide = false;
		parent.hideAllSubMenus();
		care_about_hide = true;
		submenu_state = "showing";
		if(submenu!=null) {
			submenu.setLeft(_getLeft()+_getWidth());
			submenu.setTop(_getTop());
			submenu.show();
		}
		submenu_state = "showed";
	}
	
	// skryje vsetky podmenu svojho podmenu a
	// ak je nastavena care_about_hide=false
	// tak skryje aj submenu
	function _hideSubMenu() {
		submenu_state = "hidden";
		if(submenu!=null) {
			_logme("Hiding submenu");	
			submenu.hideAllSubMenus();
			if(care_about_hide) {
				submenu.setVisibility(false);
			}
		}
	}
	
	this.showSubMenu = _showSubMenu;
	this.hideSubMenu = _hideSubMenu;
	
	function _onclick() {
		// nechcem aby to zobrazovalo este raz ked to uz zobrazim teraz
		clearTimeout(show_timer);
		_showSubMenu();
	}
	
	var mouse;
	var show_timer;
	var show_timeout = 145;
	
	
	// hovori, v akom stave je submenu 
	var submenu_state = "hidden";
	
	
	// zobrazi submenu ale cez timer
	// nastavi submenu_state na showing 
	// a ked ho zobrazi  tak na showed.
	function _doShow() {
		_logme("submenu state:"+submenu_state);
		// zobrazovat mozem iba skryte submenu
		if(submenu_state!="hidden") 
			return;
		
		// chceme tam dat nejaky delay
		var handler = function() { 
			_showSubMenu(); 
			submenu_state="showed";
		};
		
		// prepnem stav na showing
		submenu_state="showing";
		show_timer = setTimeout(handler,show_timeout);
	}
	
	// zavola sa ked ma byt prerusene zobrazovanie submenu
	function _cancelShowing() {
		clearTimeout(show_timer);
		submenu_state = "hidden";
	}
	
	function _onmouseover() {
		mouse = "in";
		_MouseOver(); // nieco urobi s obsahom
		_doShow();
	}
	
	function _onmouseout() {
		
		// dolezity priznak
		mouse="out";
				
		_MouseOut(); // nieco urobi s obsahom
		 
		if(submenu_state=="hidden") // nie je co robit
			return;
		if(submenu_state=="showing") { // prave ju zobrazujem
			_cancelShowing();
			return;
		}
	}
	
	function _hasmouse() {
		var ret;
		if(mouse=="in")
			ret = true;
		else if(submenu!=null) {
			ret = submenu.hasmouse();
		} else
			ret = false;
		return ret;
	}
	
	this.hasmouse = _hasmouse;
	
	my_tr.onclick = _onclick;
	my_tr.onmouseover = _onmouseover;
	my_tr.onmouseout = _onmouseout;	
}

/**
 * classa na vytvorenie menu stylom
 *
 * var main_menu = new Menu(sirka,objekt_do_ktoreho_sa_vlozi_menu);
 * main_menu.add(id,parent_id,"content");
 *
 * a zobrazovanie na mieste
 * main_menu.show();
 */
    
function Menu(w,place) {
	var main_popup = new Popup(0,place); // hlavne menu
	var items = new Array();
	
	main_popup.setAlwaysVisible(true);
	main_popup.setFrameVisible(false);
	main_popup.setWidth(w);
	
	this.add = function(id,parent_id,content) {
		var popup;
		if(parent_id==0) {
			popup = main_popup;
		} else {
			popup = items[parent_id].getSubMenu();
			if(popup==null) {
				popup = new Popup(parent_id,document.body);
				items[parent_id].setSubMenu(popup);
			} else {
				popup = items[parent_id].getSubMenu();
			}
		}
		var n_item = new MenuItem(id,content,popup);
		popup.addMenuItem(n_item);
		items[id] = n_item;
	}
	
	this.show = function() {
		main_popup.draw();
		main_popup.setVisibility(true);

		var i;
		for(i in items) 
			if(items[i]!=null) {
				var submenu = items[i].getSubMenu();
				if(submenu && submenu.isDrawn()==false)
					submenu.draw();
			}
		
	}
		
}



