
/* ***************************************************************************** */

// Copyright (c) 2007 Luca Reghellin - http://www.reghellin.com - email@reghellin.com

// This software is freely distributable under the terms of an MIT-style license.

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

//http://www.opensource.org/licenses/mit-license.php

/* ***************************************************************************** */

// Class Menu v2.3

var Menu = Class.create();

Menu.outProps = 'outProps';
Menu.overProps = 'overProps';
Menu.activeProps = 'activeProps';
Menu.subProps = 'subProps';

Menu.prototype = {
	initialize:function(menuId,
						mainButtonClass,
						type,
						props
						){
						
		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 = props[Menu.outProps];
		this.overProps = props[Menu.overProps];
		this.activeProps = props[Menu.activeProps];
		
		this.subProps = props[Menu.subProps];
		
		this.fxDuration = 0.7;
		this.fxFunction = this.routeFx();
		this.fxtype = 'default';
		
		this.utils = new Utils();
		
		this.initMenu();
	},
	
	initMenu:function(){
		if(this.type == 'bar'){ return; }
		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); }
											}.bindAsEventListener(this),true);
											
				Event.observe(el,'mouseout',function(){
												if(el == this.activeButton){ return false; }
												el.setStyle(this.outProps);
											}.bindAsEventListener(this),true);
			}.bind(this)
		);
		
	},
	
	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;
	},

	
	openMenu:function(el){
		if(this.activeButton != null){ this.closeMenu(this.activeButton); }
		
		if(this.type == 'bar'){
			return;
			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; }
		
		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;
			}
		}
	},
	
	setActiveButton:function(mainIndex,open,subIndex){
		
		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

/*
function initMenu1(){ 
	var menu1 = new Menu('level1','mainButton','bar',lm1Props.overProps,lm1Props.activeProps,lm1Props.outProps,lm1Props.subProps);
	menu1.setFx('fade',0.5);
	menu1.setActiveButton(1,true,1);
}

Event.observe(window, 'load', initMenu1, false);
*/