/*
 * subMenu v1.1
 *
 * rem
 * - mainmenu containter musi byt IHNED nad polozkami hlavniho menu
 * - polozky hlavniho menu nesmeji byt uz v nicem uzavrene
 *
 * change log
 * v1.1
 * - 'nopos' moznost nepozicovat submenu
 * - nazvy pozic do lowercase
 * - position a bindEvents do konstruktoru
 * - pridany hideTimeout na maimenu mouseout
 *
 * v1.2
 * - 'topright'
 * - korekce
 * - shadow
 * - callbacky mo hlavnich a sub menicek
 *  - registerTabOverCallback
 *  - registerTabOutCallback
 *  - menuOverCallBack
 *  - menuOutCallBack
 * 
 * napozicuje a ridi submenu
 *
 * 2do
 *  - dalsi tabsPos
 *  - detekovat onresize a prepozicovat menu
 *  - v prototypu procitat instance a window.subMenuReference na ne vztahnout
 *   - takle se to prepisuje
 *
*/
var subMenu = Class.create();

subMenu.prototype = {
	initialize: function(mainmenuContainer, submenuFlag, tabsPos) {
		
		this.mainMenu = $(mainmenuContainer);
		this.flag = submenuFlag;
		this.tabsPos = tabsPos;
		this.over = false;
		this.timeout = false;
		this.hideInterval = 300;
		
		this.menuOverCallBack = function (main, tab) {};
		this.menuOutCallBack = function (main, tab) {};
		this.tabOverCallBack = function (main, tab) {};
		this.tabOutCallBack = function (main, tab) {};
		
		this.lCor = 0;
		this.tCor = 0;
		
		this.roots = new Array();
		this.mainMenus = new Array();
		this.subMenus = new Array();
		this.otherEles = new Array();
		this.shadows = new Array();
		
		this.positioned = false;
		this.menuShadow = '';
		
		mainItems = this.mainMenu.descendants();
		
		topId = this.flag+'-root-';
		
		for(var x = 0; x < mainItems.length; ++x) {
			if(mainItems[x].rel) {
				if(mainItems[x].rel.search(topId) != -1) {
					//mame root, zjisti kterej
					rootId = parseInt(mainItems[x].rel.replace(topId, ''));
					
					//uloz ho
					this.roots.push(rootId);
					
					//uloz odkazy na mainmenu a submenu
					this.mainMenus[rootId] = $(mainItems[x]);
					this.mainMenus[rootId].ref = this;
					this.mainMenus[rootId].subMenuId = rootId;
					
					this.subMenus[rootId] = $(this.flag+'-'+rootId);
					this.subMenus[rootId].ref = this;
					this.subMenus[rootId].subMenuId = rootId;
				}
			} else {
				mainItems[x].ref = this;
				this.otherEles.push(mainItems[x]);
			}
		}
		this.bindEvents();
	},
	
	setCor: function (lCor, tCor) {
		this.lCor = lCor;
		this.tCor = tCor;
	},
	
	setMenuShadow: function(cssClass) {
		this.menuShadow = cssClass;
	},
	
	registerTabOverCallback: function(f) {
		this.tabOverCallBack = f;
	},
	
	registerTabOutCallback: function(f) {
		this.tabOutCallBack = f;
	},
	
	registerMenuOverCallback: function(f) {
		this.menuOverCallBack = f;
	},
	
	registerMenuOutCallback: function(f) {
		this.menuOutCallBack = f;
	},
	
	position: function () {
		this.positioned = true;
		
		if(this.tabsPos != 'nopos') {
			for(var x = 0; x < this.roots.length; ++x) {
				//napozicuj submenu
				tmain = $(this.mainMenus[this.roots[x]]);
				tsub = $(this.subMenus[this.roots[x]]);
						
				pos = Position.cumulativeOffset(tmain);			
				pLeft = pos[0];
				pTop = pos[1];
				
				if(this.tabsPos == 'lowerleft') {
					pTop += tmain.getHeight();
				} else if(this.tabsPos == 'topright') {
					pLeft += tmain.getWidth();
				}
				
				pLeft += this.lCor;
				pTop += this.tCor;
				
				/*
				kvuli pozicovani stinu, opera pak vraci top a left null
				*/
				tsub.fallBackTop = pTop;
				tsub.fallBackLeft = pLeft;
				
				Element.setStyle(tsub, {
					position: 'absolute',
					display: 'none',
					top: pTop+'px',
					left: pLeft+'px',
					overflow: 'hidden',
					zIndex: 99
				});
			}
		}
		
		if(this.menuShadow != '')this.createShadow();
	},
	
	createShadow: function() {
		for(x = 0; x < this.subMenus.length; ++x) {
			shadow = document.createElement('DIV');
			sid = this.flag+'-shadow-'+x;
			shadow.id = sid;
			shadow.className = this.menuShadow;
			
			document.body.appendChild(shadow);
			shadow = $(sid);
			shadow.innerHTML = '&nbsp;';

			pTop = this.subMenus[x].fallBackTop;
			pLeft = this.subMenus[x].fallBackLeft;
			
			pWidth = this.subMenus[x].getWidth() + parseInt(this.subMenus[x].getStyle('borderLeftWidth')) + parseInt(this.subMenus[x].getStyle('borderRightWidth'));
			pHeight = this.subMenus[x].getHeight() + parseInt(this.subMenus[x].getStyle('borderTopWidth')) + parseInt(this.subMenus[x].getStyle('borderBottomWidth'));
			
			Element.setStyle(shadow, {
				position: 'absolute',
				display: 'none',
				top: pTop+'px',
				left: pLeft+'px',
				width: pWidth+'px',
				height: pHeight+'px',
				overflow: 'hidden',
				zIndex: 98
			});
			
			this.shadows[x] = shadow;
		}
	},
	
	bindEvents: function () {
		//eventy hlavniho menu
		for(var x = 0; x < this.roots.length; ++x) {
			tmain = $(this.mainMenus[this.roots[x]]);
					
			Event.observe(tmain, 'mouseover', function(event) {
				ele = Event.element(event);
				if(!ele.ref.positioned)ele.ref.position();
				ele.ref.over = true;
				ele.ref.mainOver(ele.subMenuId);
				ele.ref.menuOverCallBack($(ele.ref.mainMenus[ele.subMenuId]), ele);
			});
			
			Event.observe(tmain, 'mouseout', function(event) {
				ele = Event.element(event);
				if(!ele.ref.positioned)ele.ref.position();
				ele.ref.over = false;
				ele.ref.setHideTimeout();
				ele.ref.menuOutCallBack($(ele.ref.mainMenus[ele.subMenuId]), ele);
			});
		}
		
		//eventy submenu tabulek
		for(var x = 0; x < this.roots.length; ++x) {
			tsub = $(this.subMenus[this.roots[x]]);
					
			Event.observe(tsub, 'mouseover', function(event) {
				ele = Event.findElement(event, 'DIV');
				if(!ele.ref.positioned)ele.ref.position();
				ele.ref.over = true;
				
				ele.ref.tabOverCallBack($(ele.ref.mainMenus[ele.subMenuId]), ele);
			});
			
			Event.observe(tsub, 'mouseout', function(event) {
				ele = Event.findElement(event, 'DIV');
				if(!ele.ref.positioned)ele.ref.position();
				ele.ref.over = false;
				ele.ref.setHideTimeout();

				ele.ref.tabOutCallBack($(ele.ref.mainMenus[ele.subMenuId]), ele);
			});
		}
		
		//eventy ostatnich polozek hlavniho menu - na mo schovavat vsechna menu
		for(var x = 0; x < this.otherEles.length; ++x) {
			Event.observe(this.otherEles[x], 'mouseover', function(event) {
				ele = Event.element(event);
				if(!ele.ref.positioned)ele.ref.position();
				ele.ref.hideAll();
			});
		}
	},
	
	mainOver: function (id) {
		this.hideAll();
		
		//zobrazit vlastni submenu
		Element.setStyle(this.subMenus[id], {display: 'block'});
		if(this.menuShadow != '')Element.setStyle(this.shadows[id], {display: 'block'});
	},
	
	hideOne: function (id) {
		Element.setStyle(this.subMenus[id], {display: 'none'});
		if(this.menuShadow != '')Element.setStyle(this.shadows[id], {display: 'none'});
	},
	
	hideAll: function() {
		for(var x = 0; x < this.roots.length; ++x) {
			tsub = $(this.subMenus[this.roots[x]]);
			Element.setStyle(tsub, {display: 'none'});
			if(this.menuShadow != '')Element.setStyle(this.shadows[x], {display: 'none'});
		}
	},
	
	setHideTimeout: function () {
		window.subMenuReference = this;
		this.timeout = window.setTimeout('window.subMenuReference.overHide()', this.hideInterval);
	},
	
	overHide: function () {
		if(this.over == false) {
			window.subMenuReference = this;
			this.timeout = window.setTimeout('window.subMenuReference.finaleHide()', this.hideInterval);
		}
	},
	
	finaleHide: function () {
		if(this.over == false) {
			this.hideAll();
			window.clearTimeout(this.timeout);
		}
	}
}