// Menu v2.2
// 05.09.2007


/* /// CLASSE MENU /// */

var Menu = Class.create();

Menu.prototype = {
	initialize:function(menuId,
						mainButtonClass,
						type,
						overProps,
						activeProps,
						outProps,
						subProps
						){
						
		this.menuId = menuId;
		this.intId1 = null;
		this.mainButtons = null;
		this.mainButtonClass = mainButtonClass;
		this.activeButton = null;
		this.busy = null;
		
		this.type = (type == 'bar' || type == 'accordion') ? type : 'bar';
		
		this.locked = true; 
		this.original = null;
		this.open = false;
		
		this.outProps = outProps;
		this.overProps = overProps;
		this.activeProps = activeProps;
		
		this.subProps = subProps;
		
		this.fxDuration = 0.7;
		this.fxFunction = this.routeFx();
		this.fxtype = 'default';
		
		this.utils = new Utils();
		
		this.initMenu();
	},
	
	initMenu:function(){
		var mainMenu = $(this.menuId);
		this.mainButtons = $A(mainMenu.getElementsByClassName(this.mainButtonClass));
		
		this.mainButtons.each(
			function(el,i){
				el.i = i;
				el.subMenu = null;
				var parent = el.parentNode;
				var sub;
				
				sub = $A(parent.getElementsByTagName('div'))[0];
				if(!sub){ sub = $A(parent.getElementsByTagName('ul'))[0]; }
				
				if(sub != undefined){ el.subMenu = sub; el.onclick = function(){return false;}; } 
				
				Event.observe(el,'click',function(){
											
											if(this.busy == true){ return false; }
											
											if(el == this.activeButton && this.locked == false){ 
												this.closeMenu(el); 
												this.locked = true;
											}
											
											else{ 
												this.openMenu(el); 
												this.locked = false;
											}
											
										}.bindAsEventListener(this));
										
										
				Event.observe(el,'mouseover',function(){
												if(el == this.activeButton){ return false; }
												el.setStyle(this.overProps);
												if(!this.locked && this.type == 'bar'){ this.openMenu(el); } //N
											}.bindAsEventListener(this),true);
											
				Event.observe(el,'mouseout',function(){
												if(el == this.activeButton){ return false; }
												el.setStyle(this.outProps);
											}.bindAsEventListener(this),true);
				
			}.bind(this)//FINE LETTERALE FUNZIONE
		);//FINE EACH
		
	},//FINE INITMENU
	
	//EFFETTI
	defaultFx:function(el,type){
		if(type == 1){ el.setStyle({display:'block'}); }
		else{  el.setStyle({display:'none'}); }
	},
	
	fadeFx:function(el,type){
		if(type == 1){  new Effect.Appear(el,{ duration:this.fxDuration }); }
		else{ new Effect.Fade(el,{ duration:this.fxDuration }); }
	},
	
	slideFx:function(el,type){
		if(type == 1){ new Effect.SlideDown(el,{ duration:this.fxDuration }); }
		else{ new Effect.SlideUp(el,{ duration:this.fxDuration }); }
	},
	
	blindFx:function(el,type){
		if(type == 1){ new Effect.BlindDown(el,{ duration:this.fxDuration }); }
		else{ new Effect.BlindUp(el,{ duration:this.fxDuration }); }
	},
	
	routeFx:function(fxtype){
		this.fxtype = fxtype;
		switch(this.fxtype){
			case 'fade':
				this.fxDuration = 0.5;
				return this.fadeFx;
				break;
			case 'slide':
				this.fxDuration = 0.7;
				return this.slideFx;
				break;
			case 'blind':
				this.fxDuration = 0.7;
				return this.blindFx;
				break;
			default:
				this.fxtype = 'default';
				return this.defaultFx;
		}
	},
	
	setFx:function(fxtype,duration){
		this.fxFunction = this.routeFx(fxtype);
		this.fxDuration = (duration) ? duration : this.fxDuration;
	},
	
	setFxDuration:function(duration){
		this.fxDuration = (duration) ? duration : this.fxDuration;
	},

	//GESTIONE MENU
	
	openMenu:function(el){
		if(this.activeButton != null){ this.closeMenu(this.activeButton); }
		
		if(this.type == 'bar'){
			if(this.intId1 != null){ clearInterval(this.intId1); }
			this.intId1 = this.utils.setTimeout(this,this.closeMenu,5,[el,true]);
		}
		
		if(el.subMenu != null){
			this.fxFunction(el.subMenu,1);
			el.setStyle(this.activeProps);
			this.activeButton = el;
		}
	},
	
	closeMenu:function(el,locked){
		if(el == this.original && locked){ return; }

		el.setStyle(this.outProps);
		if(el.subMenu != null){
			
			if(this.fxtype != 'default'){
				this.utils.setTimeout(this,this.defaultFx,0.01,[el.subMenu,0]);
			}else{
				this.defaultFx(el.subMenu,0);
			}
			
			this.activeButton = null;	
		}
		
		if( this.type == 'accordion'){ return; }
		
		//BAR
		if(locked){
			this.locked = true; 
			if(this.original){
				if(this.open){ this.openMenu(this.original); }
				else{ this.original.setStyle(this.overProps) }
				this.activeButton = this.original;
			}
		}
		//BAR
	},
	
	setActiveButton:function(mainIndex,open,subIndex){//open:Boolean
		
		var el=this.mainButtons[mainIndex];
		this.original = el;
		
		var subIndex = (subIndex != undefined) ? subIndex : 0;
		
		if(el.subMenu && subIndex != undefined){
			var sub = $A(el.subMenu.getElementsByTagName('a'))[subIndex];
			$(sub).setStyle(this.subProps);
		}
		
		if(open){
			this.openMenu(el);
			this.open = true;
		}
		
		else{ el.setStyle(this.overProps); }
		
		this.activeButton = el;
	}
	///////
}//END MENU
