//requires prototype & moo.pack.fx
subMenu = Class.create();
subMenu.prototype = {
	
	setOptions: function(options) {
		this.options = {
			delay: 100,
			opacity: false
		}
		Object.extend(this.options, options || {});
	},
	
	initialize: function(openId,buttons,menus,options) {
		this.setOptions(options);
		var options = options || '';
		this.openId = $(openId);
		this.zCount=1;
		elCount = 0;
		this.buttons = buttons;
		this.menus = menus;
		this.efx = [];
		
		if (options && options.onComplete) options.onFinish = options.onComplete;

		this.menus.each(function(el, i){
			el=$(el);
			if (el.style.display == 'none' || el.style.display == '') el.style.display = 'block';
			if(el == this.openId ){
				this.openSection = i;
				el.style.zIndex = 1;
			}
			options.onComplete = function(){
				//if (el.offsetHeight > 0) el.style.height = '1%';
				if (options.onFinish) options.onFinish(el);
			}
			this.efx[i] = new fx.Combo(el, options);
			if(i!=this.openSection)
				this.efx[i].hide();	
			el.style.visibility = 'visible';

			el.onmouseout = this.checkMouseOut.bindAsEventListener(this);

		}.bind(this));

		this.buttons.each(function(btn, i){
			if (typeof btn.onmouseover == 'function') var exFunc = btn.onmouseover;
			
			btn.onmouseover = function(){
				if (exFunc) exFunc();
				this.showThisHideOpen(this.menus[i]);
			}.bind(this);
			

			
		}.bind(this));
	},

	checkMouseOut: function(e){
		if (!e) var e = window.event;
		var targ = (window.event) ? e.srcElement : e.target;
		if (targ.nodeName == 'A') return;
		//if (targ.className == 'siteMenuButton') return;
		var relTarg = (e.relatedTarget) ? e.relatedTarget : e.toElement;

		while (relTarg != targ && relTarg.nodeName != 'BODY'){
			relTarg = relTarg.parentNode
			//console.log(relTarg);
		}
		if (relTarg == targ) return;
		
		
		//console.log(e);
		this.showThisHideOpen(this.menus[this.openSection]);
	},
	
	showThisHideOpen: function(toShow){
		
		this.menus.each(function(el, j){
								// console.log(el+' : '+this.openId);
			if (el.offsetHeight > 0 && el != toShow  && el!=this.openId){
				el.style.zIndex = 2;
				this.clearAndToggle(el, j);
			}
			if (el == toShow && toShow.offsetHeight == 0 && el!=this.openId){
				toShow.style.zIndex = 3;
				setTimeout(function(){this.clearAndToggle(toShow, j);}.bind(this), this.options.delay);
			}
			//console.log(el.id+' : z- '+el.style.zIndex)
		}.bind(this));

		/*this.menus.each(function(menu,i){
			if(menu == el){
				this.show(el);
			}else{
				this.hide();	
			}
		}.bind(this));*/
	},
	
	clearAndToggle: function(el, i){
		this.efx[i].clearTimer();
		this.efx[i].toggle();
	}
};


//if JS is enabled, hide all left menus.  Prevents them being hidden from the paranoid with JS off.
//quick and dirty, but does the job.
document.write('<style type="text/css">.menuExpanded{height:0px;overflow:hidden;}</style>');