// Trieda DHTMLmenu()
function DHTMLmenu() {
	// Public
	this.timeout = 1000;
	this.IDprefix = 'MENU';
	this.menuItems = new Array();
	this.motherID = 0;
	
	// Private
	this._timeoutID = null;
	this._zIndex = 100
	this._getWindowSizeArray = new Array();
	this._activeMenu = new Array();
	//this._displayedMenu = new Array();
	
	// Constructor
	//DOM.aE(window, 'resize', this._getWindowSize, false);
	//this._getWindowSize(true);
	//this.createMenu(0);
}

// Vytvorenie skrytych menu divov
DHTMLmenu.prototype.createMenu = function(motherID) {
	if(window.document.getElementById(this.IDprefix + '_ID_' + motherID)) {
		return true;
	}
	var content = '';
	
	for(var i in this.menuItems) {		
		if(this.menuItems[i].motherID == motherID) {
			var haveSubmenu = false;
			for(var y in this.menuItems) {
				if(this.menuItems[y].motherID == this.menuItems[i].ID) {
					haveSubmenu = true;
					break;
				}
			}
			if(haveSubmenu) {
				content += '<tr><td onmouseover="MENU.displayMenu(this, ' + this.menuItems[i].ID + ', \'vertical\', true);" class="menuRowHaveSubmenu" id="' + this.IDprefix + '_slinkID_' + this.menuItems[i].ID + '"><a href="' + this.menuItems[i].url + '" target="' + this.menuItems[i].target + '">' + this.menuItems[i].name + '</a></td></tr>';
			} else {
				content += '<tr><td onmouseover="MENU.displayMenu(this, ' + this.menuItems[i].ID + ', \'vertical\', false);" class="menuRow" id="' + this.IDprefix + '_linkID_' + this.menuItems[i].ID + '"><a href="' + this.menuItems[i].url + '" target="' + this.menuItems[i].target + '">' + this.menuItems[i].name + '</a></td></tr>';
			}
		}
	}

	if(content) {
		content = '<table border="0" cellspacing="0" cellpadding="0">' + content + '</table>';
		alert('<div class="menuBoxHidden" onmouseover="javascript:MENU.clearTimeoutID();" onmouseout="javascript:MENU.hideMenu(' + motherID + ', false);" id="' + this.IDprefix + '_ID_' + motherID + '" style="z-index: ' + this._zIndex + ';">' + content + '</div>');
		try {
			window.document.getElementById('menuDiv').innerHTML += '<div class="menuBoxHidden" onmouseover="javascript:MENU.clearTimeoutID();" onmouseout="javascript:MENU.hideMenu(' + motherID + ', false);" id="' + this.IDprefix + '_ID_' + motherID + '" style="z-index: ' + this._zIndex + ';">' + content + '</div>';
		} catch(e) {
			alert(e);
		}
		this._zIndex = this._zIndex + 1;
		return true;
	} else {
		return false;
	}
}
	
// Zobrazenie a umiestnenie menu divu
DHTMLmenu.prototype.displayMenu = function(fromElement, ID, menuType, haveSubmenu) {
	// Vynuluje casovac
	this.clearTimeoutID;
	
	// Zistenie materskeho ID
	for(var i in this.menuItems) {
		if(this.menuItems[i].ID == ID) {
			var motherID = this.menuItems[i].motherID;
			if(window.document.getElementById(this.IDprefix + '_slinkID_' + motherID)) {
				window.document.getElementById(this.IDprefix + '_slinkID_' + motherID).className = 'menuRowHaveSubmenuHover';
			}
		}
	}
	
	// Skrytie HOVER aktivneho menu
	if(this._activeMenu[motherID]) {
		if(this._activeMenu[motherID]['haveSubmenu']) {
			if(window.document.getElementById(this.IDprefix + '_slinkID_' + this._activeMenu[motherID]['ID'])) {
				window.document.getElementById(this.IDprefix + '_slinkID_' + this._activeMenu[motherID]['ID']).className = 'menuRowHaveSubmenu';
			}
		} else {
			if(window.document.getElementById(this.IDprefix + '_linkID_' + this._activeMenu[motherID]['ID'])) {
				window.document.getElementById(this.IDprefix + '_linkID_' + this._activeMenu[motherID]['ID']).className = 'menuRow';
			}
		}
	}
	
	// Ulozenie aktivneho menu ID do pamate
	this._activeMenu[motherID] = new Array();
	this._activeMenu[motherID]['ID'] = ID;
	this._activeMenu[motherID]['haveSubmenu'] = haveSubmenu;
	
	// Skrytie HOVER aktivneho submenu
	if(this._activeMenu[ID]) {
		if(this._activeMenu[ID]['haveSubmenu']) {
			if(window.document.getElementById(this.IDprefix + '_slinkID_' + this._activeMenu[ID]['ID'])) {
				window.document.getElementById(this.IDprefix + '_slinkID_' + this._activeMenu[ID]['ID']).className = 'menuRowHaveSubmenu';
			}
		} else {
			if(window.document.getElementById(this.IDprefix + '_linkID_' + this._activeMenu[ID]['ID'])) {
				window.document.getElementById(this.IDprefix + '_linkID_' + this._activeMenu[ID]['ID']).className = 'menuRow';
			}
		}
	}
	
	// Zmena stylu elementu, nad ktorym je prave mys
	if(haveSubmenu) {
		fromElement.className = 'menuRowHaveSubmenuHover';
	} else {
		fromElement.className = 'menuRowHover';
	}
	
	// Skrytie vsetkych submenu, ktore maju rovnake materske ID, cize su bratia k aktualnemu elementu
	for(var i in this.menuItems) {
		if((this.menuItems[i].motherID == motherID) && (this.menuItems[i].ID != ID)) {			
			this.hideChildMenu(this.menuItems[i].ID);
		}
	}
	
	if(!window.document.getElementById(this.IDprefix + '_ID_' + ID)) {
		this.createMenu(ID);
	}
	if(window.document.getElementById(this.IDprefix + '_ID_' + ID)) {
		//alert(window.document.getElementById(this.IDprefix + '_ID_' + ID).offsetHeight);
		//alert(fromElement + ' ' + ID + ' ' + menuType);
		var position = this._getAbsolutePosition(fromElement);
		var element = window.document.getElementById(this.IDprefix + '_ID_' + ID);
		
		// X a Y - ova pozicia laveho horneho rohu submenu
		var Xposition = position.left	+ (menuType == 'vertical' ? fromElement.offsetWidth : 0);
		var Yposition = position.top	+ (menuType == 'vertical' ? 0 : fromElement.offsetHeight);
		// Setnutie novej pozicie submenu
		element.style.left 	= Xposition	+ 'px';
		element.style.top 	= Yposition	+ 'px';
		element.className 	= 'menuBox';
		
		if((Xposition + element.offsetWidth) > (this._getWindowSizeArray['width'] - 10)) {
			Xposition = position.left - element.offsetWidth;
			Xposition > 0 ? element.style.left = Xposition + 'px' : element.style.left = 10 + 'px';
		}
		
		// Odkomentovat pri malom pocte submenu - skript pouziva dolne obmedzenie prehliadaca
		/*if((Yposition + element.offsetHeight) > (this._getWindowSizeArray['height'] + window.document.body.scrollTop - 10)) {
			Yposition = this._getWindowSizeArray['height'] + window.document.body.scrollTop - element.offsetHeight;
			Yposition > window.document.body.scrollTop ? element.style.top = Yposition + 'px' : element.style.top = window.document.body.scrollTop + 10 + 'px';
		}*/
	}
}

// Zavretie menu pri opusteni kurzora mysi
DHTMLmenu.prototype.hideMenu = function(ID, now) {
	if(window.document.getElementById(this.IDprefix + '_ID_' + ID)) {
		if(now) {
			window.document.getElementById(this.IDprefix + '_ID_' + ID).className = 'menuBoxHidden';
			this.hideParentMenu(ID);
			this.hideChildMenu(ID);
		} else {
			this._timeoutID = window.setTimeout('MENU.hideMenu(' + ID + ', true)', this.timeout);
		}
	}
}

// Skrytie rodicovskeho menu
DHTMLmenu.prototype.hideParentMenu = function(ID) {
	if(window.document.getElementById(this.IDprefix + '_ID_' + ID)) {
		// Zmazanie rodica
		window.document.getElementById(this.IDprefix + '_ID_' + ID).className = 'menuBoxHidden';
		for(var i = 0; i < this.menuItems.length; i++) {
			if(this.menuItems[i].ID == ID) {
				this.hideParentMenu(this.menuItems[i].motherID);
			}
		}
	}
}

// Skrytie potomka
DHTMLmenu.prototype.hideChildMenu = function(ID) {
	if(window.document.getElementById(this.IDprefix + '_ID_' + ID)) {
		// Zmazanie potomka
		window.document.getElementById(this.IDprefix + '_ID_' + ID).className = 'menuBoxHidden';
		
		for(var i in this.menuItems) {
			if(this.menuItems[i].motherID == ID) {
				// Zrusenie CSS vlastnosti hover na riadku, ktory ma potomka
				//var element = window.document.getElementById(this.IDprefix + '_slinkID_' + this.menuItems[i].ID);
				//if(element && element.className != 'menuRowHaveSubmenu') {
				/*if(window.document.getElementById(this.IDprefix + '_slinkID_' + this.menuItems[i].ID)) {
					window.document.getElementById(this.IDprefix + '_slinkID_' + this.menuItems[i].ID).className = 'menuRowHaveSubmenu';
				}
				
				// Zrusenie CSS vlastnosti hover na riadku, ktory nema potomka
				//var element = window.document.getElementById(this.IDprefix + '_linkID_' + this.menuItems[i].ID);
				//if(element && element.className != 'menuRow') {
				if(window.document.getElementById(this.IDprefix + '_linkID_' + this.menuItems[i].ID))
					window.document.getElementById(this.IDprefix + '_linkID_' + this.menuItems[i].ID).className = 'menuRow';
				//}*/
					
				// Skrytie potomka
				this.hideChildMenu(this.menuItems[i].ID);
			}
		}
	}
}

// Ziskanie absolutnej pozicie elementu, bez ohladu na to ci je absolutne, relativne alebo staticky pozicovany
DHTMLmenu.prototype._getAbsolutePosition = function(fromElement) {
	var position = new Array();
	position.left	= fromElement.offsetLeft;
	position.top	= fromElement.offsetTop;
	try {
		//alert(fromElement.toString());
		if(fromElement.offsetParent) {
			var addPosition = this._getAbsolutePosition(fromElement.offsetParent);
			position.left	+= addPosition.left;
			position.top	+= addPosition.top;
		}
	} catch(e) {
		alert(e);
	}
	return position;
}

// Velkost aktualneho okna
DHTMLmenu.prototype._getWindowSize = function(debug) {
	var myWidth = 785, myHeight = 550;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth - 1;
		myHeight = window.innerHeight - 1;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	
	MENU._getWindowSizeArray['width'] = myWidth;
	MENU._getWindowSizeArray['height'] = myHeight;
	
	//if(debug == true) {
	//	window.document.getElementById('debugDiv').innerHTML = MENU._getWindowSizeArray['width'] + ' x ' + MENU._getWindowSizeArray['height'];
	//}
}

// Resetovanie timeoutu
DHTMLmenu.prototype.clearTimeoutID = function()	{
	window.clearTimeout(this._timeoutID);
}

var DOM = {
	gI : function (el) {
		return document.getElementById(el);
	},
	cT : function(txt) {
		return document.createTextNode(txt);
	},
	cE : function (el) {
		return document.createElement(el);
	},
	aC : function (p,c) {
		return p.appendChild(c);
	},
	aE : function (elm,evtType,evtFn,set) {
		if (document.addEventListener) {
			if ((elm == window) && window.opera){
				elm == document;
			} 
			elm.addEventListener(evtType,evtFn,set);
		} else {
			elm.attachEvent('on' + evtType,evtFn);
		}
	},
	rE : function (elm,evtType,evtFn,set) {
		if (document.addEventListener) {
			if ((elm == window) && window.opera) elm == document;
			elm.removeEventListener(evtType,evtFn,set);
		} else {
			elm.detachEvent('on' + evtType,evtFn);
		}
	},
	fE : function(e) {
		if (typeof e == 'undefined') e = window.event;
		return e;
	}
}

// Inicializacia JavaScriptov pri udalosti onload okna
var MENU = null;
function initMenu() {
  // Vytvorenie objektu MENU
	MENU = new DHTMLmenu();
	MENU.IDprefix = 'MENU_DIVS';
	MENU.menuItems = menuItems;
	//MENU.createMenu(0);
	MENU._getWindowSize(true);
	DOM.aE(window, 'resize', MENU._getWindowSize, false);
};

DOM.aE(window, 'load', initMenu, false);
