/**
 * Класс, который выводит горизонтальное или вертикальное меню
 * Переделан с класса dropdown.co.uk
 * Работает в IE, Mozilla, Opera
 * @author Ilya Rudenko <rudenko@delta-x.com.ua>
 * @copyright Delta-X 2008
 *
 * Стили для слоёв с меню .menu {position:absolute;visibility:hidden;display:none;filter:revealTrans(duration=.5,transition=3);}
 * 
 * Вызов конструктора:
 * <body onload="DropDown.init();">
 *
 * Слой с меню:
 * <div class="menu" id="menu1" onmouseout="DropDown.killMenu('menu1')" onmouseover="DropDown.checkMenu();">123</div>
 * 
 * Вызов меню:
 * <table>
 *   <td id="nav_menu1">
 *      <a href="jaavscript:void(0);" onmouseover="DropDown.toggle('menu1', 'nav_menu1'); return false;">Menu 1</a>
 *   </td>
 *  </table>
 * 
 * Объекты:
 * menu1 - id слоя с меню
 * nav_menu1 - id слоя по которому будет выравниватся выпадающее меню
 */
(DropDown = {
	// Отступ
	cellpad : 2,
	
	// Эффект открытия 0-24
	effectopen : 5,
	
	// Эффект исчезновения меню  0-24
	effectclose : 4,
	
	// Для горизонтального меню должно быть равно 1, для вертикального - 0
	is_horizontal : 1,
	
	
	// ----- Дальше - ничего не навтраиваем  ----- //
	opera : 0,
	msie : 0,
	moz : 0,
	timerID : null,
	timerID2 : null,
	current_menu : "none",
	use_effects : 0,
	counter : 0,
	
	init : function() {
		this.opera = (navigator.userAgent.indexOf('Opera')!=-1);
		this.msie = (navigator.userAgent.indexOf('MSIE')!=-1);
		this.moz = (navigator.userAgent.indexOf('Gecko')!=-1);
		this.use_effects = (this.effectopen!=-1 && this.effectclose!=1 && this.msie) ? 1 : 0;
	},
	toggle : function(selected_menu, nav_menu) {
		if (!document.getElementById(selected_menu)) {
			return;
		}
		if (this.timerID) clearTimeout(this.timerID);
		if (this.current_menu == "none"){
			this.showHide(selected_menu, nav_menu);
		} else if (this.current_menu != selected_menu) {
			this.showHide(this.current_menu, '');
			this.showHide(selected_menu, nav_menu);
		}
		if (this.timerID2) clearTimeout(this.timerID2)
		
		this.timerID2 = setTimeout("DropDown.showHide('"+selected_menu+"', '');", 3000);
	},
	showHide : function(selected_menu, nav_menu) {
		var menu = document.getElementById(selected_menu);
		if (this.use_effects) menu.filters(0).Apply();
		if (menu.style.visibility == 'visible') {
			menu.style.visibility = 'hidden';
			if (this.use_effects) menu.filters[0].transition = this.effectclose;
			this.current_menu = "none";
		} else {
			menu.style.left = this.getLeft(nav_menu)+"px";
			menu.style.top = this.getTop(nav_menu)+"px";
			menu.style.visibility = 'visible';
			menu.style.display = 'block';
			if (this.use_effects) menu.filters[0].transition = this.effectopen;
			this.current_menu = selected_menu;
		}
		if (this.use_effects) menu.filters(0).Play();
	},
	getTop : function (nav_menu) {
		var obj = document.getElementById(nav_menu);
		var menuheight = obj.offsetHeight;
		var menutop = 0;
		if (obj.offsetParent) {
			while (obj.offsetParent) {
				menutop += obj.offsetTop;
				obj = obj.offsetParent;
			}
		} else if (obj.y) {
			menutop += obj.y;
		}
		menutop += (this.is_horizontal) ? menuheight + 2 : this.cellpad;
		return menutop;
	},
	getLeft : function (nav_menu) {
		var obj = document.getElementById(nav_menu);
		var menuwidth = obj.offsetWidth;
		var menuleft = 0;
		if (obj.offsetParent) {
			while (obj.offsetParent) {
				menuleft += obj.offsetLeft;
				obj = obj.offsetParent;
			}
		} else if (obj.x) {
			menuleft += obj.x;
		}
		menuleft += (!this.is_horizontal) ? menuwidth + 5 : this.cellpad;
		return menuleft;
	},
	killMenu : function (whatwait) {
		if (this.timerID) clearTimeout(this.timerID)
		if (this.timerID2) clearTimeout(this.timerID2)
		this.timerID = setTimeout("DropDown.showHide('"+whatwait+"','');", 50);
	},
	checkMenu : function() {
		if(this.timerID) clearTimeout(this.timerID)
		if(this.timerID2) clearTimeout(this.timerID2)
	}
});
