

/*--------------------------------------------------------------/Begin MooMenu  /--------------------------------------------------------------*/	
/**
 * MooMenu beta
 * @author Jason J. Jaeger | greengeckodesign.com
 * @version 0.6
 * 
 *  Feb 14th 2008
 *  JJJ - Added stretchMainMenu option to simulate a table based layout for the main menu when the orienation is horizontal
 **/

var MooMenu = new Class({
	options: {
        id: 'nav',//the id of the main menu (ul or ol)
		effect: null,//slide, fade, slide & fade
		duration: 600,//of the effect
		transition: Fx.Transitions.Bounce.easeOut,//how the effect behaves
		orientation: 'horizontal',//horizontal or vertical
		xDirection: 'right',//direction submenus appear relative to the parent button
		yDirection: 'down',//direction submenus appear relative to the parent button
		hideDelay: 1000,//in milliseconds, how long you have after moving you mouse off of the submenus before they dissapear
		opacity: 100,//of the submenus
		initialSubMenuXoffset: 0,//if you need to tweak the placement of the initial submenus
		initialSubMenuYoffset: 0,//if you need to tweak the placement of the initial submenus
		subsequentSubMenuXoffset: 0,//if you need to tweak the placement of the subsequent submenus
		subsequentSubMenuYoffset: 0,//if you need to tweak the placement of the subsequent submenus
		stretchMainMenu:false,
		debug:false
    },
	
	colors:['#3D6CB2','#DA081C','#29C42B'],
	currentColor:0,
	currentPadding:0,
	classElHash: new Hash(),//index = childMenu element id, item = submenu class
	hideAllMenusTimeout:null,
	parentBtns: new Hash(),
	
	initialize: function(options){
        this.setOptions(options);
		this.options.opacity = this.options.opacity /100;
		var btnArray = $(this.options.id).getElements('a');
		var tempObjArray= [];
		
		//create temp objects
		btnArray.each(function(x){
			$(x).title = "";
			var tempObj = new Object();		   
		   	if ( $(x).getNext('ul')  || $(x).getNext('ol') ) {
				var theSubMenuType = 'subsequent';
				if($(x).parentNode.parentNode.id && $(x).parentNode.parentNode.id == this.options.id){theSubMenuType ='initial';	}
				tempObj.btn=$(x);
				this.parentBtns.set(this.getOrSetId(tempObj.btn), tempObj.btn);
				if(theSubMenuType === 'initial'){
					tempObj.btn.addClass('mainMenuParentBtn');
				}else{
					tempObj.btn.addClass('subMenuParentBtn');
				}
				tempObj.childMenu=$(x).getNext();
				tempObj.parentSubMenus= this.getParents($(x), this.options.id, 'ul,ol');
				tempObj.subMenuType=theSubMenuType;
				tempObjArray.push(tempObj);
			}
		}.bind(this));

		//rip the submenus apart into separate divs
		//var subMenusContainer = new Element('div', { 'id': 'subMenusContainer'	}).inject($(this.options.id),'after');
		var subMenusContainer = new Element('div', { 'id': 'subMenusContainer'	}).inject(document.body,'inside');
		//var subMenusContainer = new Element('div', { 'id': 'subMenusContainer'	}).inject($('innerWrapper'),'inside');
		
		tempObjArray.each(function(item,index){	
			var subMenuOuterWrapper = new Element('div', {	'class': 'smOW'	}).inject(subMenusContainer)
			item.childMenu.inject(subMenuOuterWrapper);
			item.childMenu = subMenuOuterWrapper;
			this.classElHash.set(this.getOrSetId(subMenuOuterWrapper), null);
		}.bind(this));
		
		//update the IDs in the parentSubMenus Arrays in the temp objects
		for (var index in tempObjArray) {
			if (tempObjArray.hasOwnProperty(index)) {
				var tempParentSubMenusHash = new Hash();
				for(var x in tempObjArray[index].parentSubMenus){
					if(tempObjArray[index].parentSubMenus.hasOwnProperty(x)){
						var currentParentId = $(tempObjArray[index].parentSubMenus[x]).getParent().id;
						tempParentSubMenusHash.set(currentParentId,null);
					}
				}
				tempObjArray[index].parentSubMenus = tempParentSubMenusHash;
			}
		}
		
		subMenusContainer.getElements('a').each(function(item,index){ item.set('tabindex','-1'); });
		
		//now create the MooSubMenu class instances from the temp objects
		for (var index in tempObjArray) {
			if (tempObjArray.hasOwnProperty(index)) {
				var aSubMenu = new MooSubMenu(this.options,{
					root:this,
					btn:tempObjArray[index].btn,
					childMenu:tempObjArray[index].childMenu,
					parentSubMenus: tempObjArray[index].parentSubMenus,
					subMenuType:tempObjArray[index].subMenuType
				});
				aSubMenu.setOptions(this.options);//so the submenus inherit the current option values instead of the default option values
				this.classElHash[tempObjArray[index].childMenu.id] = aSubMenu;
			}
		}

		//add class references to the parentSubMenus Hashes
		this.classElHash.each(function(item,index){	
			var parentArr =[];
			item.parentSubMenus.each(function(item2,index2){ 
				item.parentSubMenus[index2] = this.classElHash[index2]; 
				parentArr.push(this.classElHash[index2]);
			}.bind(this));
			item.parentSubMenu = parentArr[0];
		}.bind(this));
		
		//attach event handlers to non-parent main menu buttons
		var nonParentBtns = $(this.options.id).getElements('a').filter(function(item, index){ return !this.parentBtns.contains(item); }.bind(this));
		nonParentBtns.each(function(item, index){
			item.addEvents({
				'mouseenter': function(e){
					e = new Event(e).stop();
					this.hideAllSubMenusNow();			
				}.bind(this),
				
				'focus': function(e){
					e = new Event(e).stop();
					this.hideAllSubMenusNow();	
				}.bind(this),
				
				'keydown' : function(e){
				    var event = new Event(e);
					if (e.key === 'up' || e.key === 'down' || e.key === 'left' || e.key === 'right') {	e.stop();	}
					
					if(e.key === 'left'){
						
						if(item.getParent('li').getPrevious('li')){
							item.getParent('li').getPrevious('li').getFirst('a').focus();
						}else{
							item.getParent('li').getParent().getLast('li').getFirst('a').focus();
						}
						
					
					}else if(e.key === 'right'){
						if(item.getParent('li').getNext('li')){
							item.getParent('li').getNext('li').getFirst('a').focus();
						}else{
							item.getParent('li').getParent().getFirst('li').getFirst('a').focus();
						}
						
					}
				}.bind(this)
			});
		}, this);


		//stretch main menu btn widths to fit withing the width of the parent UL or OL
		if(this.options.stretchMainMenu && this.options.orientation === 'horizontal'){
			var targetWidth = $(this.options.id).getCoordinates().width ;
			var totalBtnWidth = 0;
			var mainBtns = $(this.options.id).getElements('a');
			var tolerance = 0 + parseFloat($(mainBtns[0]).getStyle('border-left-width')) + parseFloat( $(mainBtns[mainBtns.length-1]).getStyle('border-right-width'));
			var limit = 600;
			var currentIndex = 0;
			mainBtns.each(function(item,index){ item.setStyles({'padding-left':0,'padding-right':0}) }.bind(this));
			while((totalBtnWidth+tolerance) < targetWidth && limit >0 ){
				limit--;
				totalBtnWidth = 0;
				mainBtns[currentIndex].setStyle('width', mainBtns[currentIndex].getCoordinates().width + 1);
				mainBtns.each(function(item,index){ totalBtnWidth+= item.getCoordinates().width; }.bind(this));
				currentIndex++;
				if(currentIndex>mainBtns.length-1){currentIndex = 0;	}
			}
			//alert(totalBtnWidth +" | "+ targetWidth);
		}
		
		
		//this.colorize();
		//this.curve();
		
		
		
		this.classElHash.each(function(item,index){
			item.childMenu.setStyles({	'opacity':0	});
			
			item.showSubMenu();
			item.hideSubMenu();
			
			//item.childMenu.setStyles({	'opacity':this.options.opacity	});
			
		}.bind(this));
		
		(function(){ 		
			this.classElHash.each(function(item,index){
				item.childMenu.setStyles({	'opacity':this.options.opacity	});
			}.bind(this));
		 }.bind(this)).delay(500);
		
		
		
		
		
    },

	curve:function(){
		var mainBtns = $(this.options.id).getElements('a');
		mainBtns.each(function(item,index){
			if(index >5){
				var newLeftPadding = this.currentPadding + parseFloat(item.getStyle('padding-left'));
				item.setStyle('padding-left',newLeftPadding);
				this.currentPadding = this.currentPadding + 6.5;
			}
			
		}.bind(this));
	},
	colorize:function(){
		var mainBtns = $(this.options.id).getElements('a');
		mainBtns.each(function(item,index){
			item.setStyle('color',this.colors[this.currentColor]);
			this.currentColor++;
			if(this.currentColor >= this.colors.length){this.currentColor = 0;}
		}.bind(this));
	},

	getParents: function(obj, wrapperId, tagNames){
		var parents = [];
		if(tagNames){
			tagNames = tagNames.toString().split(",");
			tagNames.each(function(item, index){tagNames[index] = tagNames[index].toLowerCase();}); 
		}
		currentObj = obj;
		var limit = 200;
		var counter = 0;
		while(currentObj.parentNode && currentObj.parentNode.id != wrapperId){
			counter++;
			if(counter > limit){break;}
			if( !tagNames || tagNames.contains(currentObj.parentNode.tagName.toLowerCase())  ){
				parents.push(currentObj.parentNode);
			}
			currentObj = currentObj.parentNode;
		}
		return parents;
	},

	getOrSetId: function(obj){
		if(!obj.id){
			var d = new Date();
			var milli = d.getMilliseconds().toString();
			var randomNumber = Math.floor(Math.random()*1000);
			if(obj.nodeName){
				obj.id = obj.nodeName.toString() + '-' + milli + randomNumber.toString();
			}else{
				obj.id = "element" + milli + randomNumber.toString();	
			}
		}
		return obj.id;
	},
	
	hideAllSubMenusNow: function(){
		$clear(this.hideAllMenusTimeout);
		this.classElHash.each( function(item, index){	item.hideSubMenu();	}.bind(this));
	}  
	
});
MooMenu.implement(new Options);
MooMenu.implement(new Events);

var MooSubMenu = new Class({
	Extends: MooMenu,
    options: {},
	root:null,
	btn:null,
	childMenu:null,
	subMenuType:null,
	parentSubMenus:new Hash(),
	parentSubMenu: null,
	hidden:true,
	myEffect:null,
	width:false,
	height:null,
		
	initialize: function(options,uniqueVars){
       
	    this.setOptions(options);
		for(var x in uniqueVars){	if(uniqueVars.hasOwnProperty(x)){ this[x] = uniqueVars[x];	}	}
		
		if(this.options.effect){
			this.myEffect = new Fx.Morph(
				$(this.childMenu).getFirst(), 
				{
					duration: this.options.duration, 
					transition: this.options.transition,  
					link: 'cancel' 
				} 
			);
		}
		
		
		
		if(this.options.effect === 'slide' || this.options.effect === 'slide & fade'){
			if (this.subMenuType == 'initial' && this.options.orientation === 'horizontal') {
				this.childMenu.getFirst().setStyle('margin-top','0' );
				//this.myEffect.start({ 'margin-top': -this.childMenu.getOffsetSize().y });
			}else{
				this.childMenu.getFirst().setStyle('margin-left','0' );
				//this.myEffect.start({ 'margin-left': -this.childMenu.getOffsetSize().y });
			}
			
		}else if (this.options.effect === 'fade' || this.options.effect === 'slide & fade'){
			this.childMenu.getFirst().setStyle('opacity',0 );
		}
		
		if (this.options.effect != 'fade' && this.options.effect != 'slide & fade') {
			this.childMenu.getFirst().setStyle('opacity',this.options.opacity);
		}
		
		$(this.childMenu).addEvents({
			'mouseenter': function(e){
				//e = new Event(e).stop();
				//console.log('hehe');
				//this.cancellHideAllSubMenus();
				//this.hideOtherSubMenus();
				//this.showSubMenu();				
			}.bind(this)
		});
		
	
		var nonParentBtns = $(this.childMenu).getElements('a').filter(function(item, index){ return !this.parentBtns.contains(item); }.bind(this));
		nonParentBtns.each(function(item, index){
			$(item).addClass('subMenuBtn');
			
			item.addEvents({
				'mouseenter': function(e){
					e = new Event(e).stop();
					this.cancellHideAllSubMenus();
					this.hideOtherSubMenus();				
				}.bind(this),
				
				'focus': function(e){
					e = new Event(e).stop();
					this.cancellHideAllSubMenus();
					this.hideOtherSubMenus();				
				}.bind(this),
				
				'mouseleave': function(e){
					e = new Event(e).stop();
					this.cancellHideAllSubMenus();
					this.hideAllSubMenus();
					
				}.bind(this),
				
				'keydown' : function(e){
				    var event = new Event(e);
					if (e.key === 'up' || e.key === 'down' || e.key === 'left' || e.key === 'right') {	e.stop();	}
					
					if(e.key === 'up'){
						
						if(item.getParent('li').getPrevious('li')){
							item.getParent('li').getPrevious('li').getFirst('a').focus();
						}else{
							this.btn.focus();
						}
						
					
					}else if(e.key === 'down'){
						if(item.getParent('li').getNext('li')){
							item.getParent('li').getNext('li').getFirst('a').focus();
						}else{
							item.getParent('li').getParent().getFirst('li').getFirst('a').focus();
						}
						
					}else if(e.key === 'left'){
						this.btn.focus();
					}
				   
				   
				}.bind(this)
			});
			
		}, this);
		
		$(this.btn).removeClass('subMenuBtn');
		
		if (this.subMenuType == 'initial') {
			this.btn.addClass('mainParentBtn');	
		}else{	
			this.btn.addClass('subParentBtn');	
		}
		
		$(this.btn).addEvents({
			'mouseenter' : function(e){
				e = new Event(e).stop();
				this.cancellHideAllSubMenus();
				this.hideOtherSubMenus();
			
				this.showSubMenu();
				
				
				

			}.bind(this),
			
			'focus' : function(e){
				e = new Event(e).stop();
				this.cancellHideAllSubMenus();
				this.hideOtherSubMenus();
			
				this.showSubMenu();

			}.bind(this),
				
			'mouseleave': function(e){
				e = new Event(e).stop();
				this.cancellHideAllSubMenus();
				this.hideAllSubMenus();
				
			}.bind(this),
			
			'blur': function(e){
				e = new Event(e).stop();
				this.cancellHideAllSubMenus();
				this.hideAllSubMenus();
				
			}.bind(this),
			
			'keydown' : function(e){
			  e = new Event(e)
				
				
				if (e.key === 'up' || e.key === 'down' || e.key === 'left' || e.key === 'right') {	e.stop();	}
				
				
				
				if(!this.parentSubMenu){
					//initial
					if(e.key === 'down'){
						this.childMenu.getFirst().getFirst().getFirst().focus();
					}else if(e.key === 'left'){
						if(this.btn.getParent().getPrevious()){
							this.btn.getParent().getPrevious().getFirst().focus();
						}else{
							this.btn.getParent().getParent().getLast().getFirst().focus();
						}
					}else if(e.key === 'right'){
						if (this.btn.getParent().getNext()) {
							this.btn.getParent().getNext().getFirst().focus();
						}else{
							this.btn.getParent().getParent().getFirst().getFirst().focus();
						}
					}
				}else{
					//subsequent
					if (e.key === 'up') {
						if (this.btn.getParent('li').getPrevious('li')) {
							this.btn.getParent('li').getPrevious('li').getFirst('a').focus();
						}else{
							this.parentSubMenu.btn.focus();
							//this.btn.getParent('li').getParent().getLast('li').getFirst('a').focus();
						}
					}else if(e.key === 'down'){
						
						if(this.btn.getParent('li').getNext('li')){
							this.btn.getParent('li').getNext('li').getFirst('a').focus();
						}else{
							this.btn.getParent('li').getParent().getFirst('li').getFirst('a').focus();
						}
						
						
					}else if(e.key === 'left'){
						this.parentSubMenu.btn.focus();
					}else if(e.key === 'right'){
						this.childMenu.getFirst().getFirst('li').getFirst('a').focus();
					}
				}
				
				
				
				
			}.bind(this)
				
		});
		
    },
	
	hideSubMenu: function() {
		if(this.hidden){return;}
		
		if (this.subMenuType == 'initial') {
			$(this.btn).removeClass('mainMenuParentBtnFocused');
			$(this.btn).addClass('mainMenuParentBtn');
			//var myMorph = new Fx.Morph(this.btn, { 'duration':10 });
			//myMorph.start('#nav a, #subMenusContainer a');
			
		}else{
			$(this.btn).removeClass('subMenuParentBtnFocused');
			$(this.btn).addClass('subMenuParentBtn');
			//var myMorph = new Fx.Morph(this.btn, { 'duration':10 });
			//myMorph.start('#nav a, #subMenusContainer a');
		}
		
		if(this.options.effect == 'slide'){
			if (this.subMenuType == 'initial' && this.options.orientation === 'horizontal') {
				
				this.myEffect.start({ 'margin-top': -this.height }).chain(function(){	this.childMenu.style.display = "none";	}.bind(this));
			}else{
				this.myEffect.start({ 'margin-left': -this.width }).chain(function(){	this.childMenu.style.display = "none";	}.bind(this));
			}
		}else if(this.options.effect == 'fade'){
			this.myEffect.start({ 'opacity': 0 }).chain(function(){	this.childMenu.style.display = "none";	}.bind(this));
		
		}else if(this.options.effect == 'slide & fade'){
			
			if (this.subMenuType == 'initial' && this.options.orientation === 'horizontal') {
				
				this.myEffect.start({ 'margin-top': -this.height, opacity:0 }).chain(function(){	this.childMenu.style.display = "none";	}.bind(this));
			}else{
				if(this.options.xDirection === 'left'){
					this.myEffect.start({ 'margin-left': this.width, opacity:0 }).chain(function(){	this.childMenu.style.display = "none";	}.bind(this));
				}else{
					this.myEffect.start({ 'margin-left': -this.width, opacity:0 }).chain(function(){	this.childMenu.style.display = "none";	}.bind(this));
				}
				
			}
			
		}else{
			this.childMenu.style.display = "none";
			
			//this.childMenu.effect('opacity', {duration: 500, transition: Fx.Transitions.linear}).start(1,0.5);
		}
		this.hidden = true;
		
	},
	
	hideOtherSubMenus: function() {
		/*var subMenusToHide = (this.root.classElHash).filter(function(item, index){  
			return (!this.parentSubMenus.contains(item) && this.childMenu.id != index  ) ; 
		}.bind(this));
		subMenusToHide.each(function(item,index){	
			item.hideSubMenu();
		});*/
		
		var showArr = [];
		
		
		(this.root.classElHash).each(function(item,index){	
			if(!this.parentSubMenus.contains(item) && this.childMenu.id != index  ){
				item.hideSubMenu();
			}else{
				//item.showSubMenu();
				showArr.push(item);
			}
		}.bind(this));
		
		for(i = (showArr.length-1); i>=0 ;i--){
			
			showArr[i].showSubMenu();
		}
		
		
	},
	
	hideAllSubMenus: function(){
		$clear(this.root.hideAllMenusTimeout);
		this.root.hideAllMenusTimeout = (function(){
			$clear(this.hideAllMenusTimeout);
			this.root.classElHash.each( function(item, index){	item.hideSubMenu();	}.bind(this));
		}).bind(this).delay(this.options.hideDelay);		
	},

	cancellHideAllSubMenus: function(){ 
		$clear(this.root.hideAllMenusTimeout);	
	},
	
	showSubMenu: function(now){
		if(!this.btn || !this.hidden){return;}
		
		//this.childMenu.style.opacity = "1.0";
		
		if (this.subMenuType == 'initial') {
			$(this.btn).removeClass('mainMenuParentBtn');
			$(this.btn).addClass('mainMenuParentBtnFocused');	
			//var myMorph = new Fx.Morph(this.btn, { 'duration':600 });
			//myMorph.start('.mainMenuParentBtnFocused');
			//myMorph.start('#subMenusContainer a:hover, #nav li a:hover, #nav li:hover, #nav li.sfhover');
			//myMorph.start({ 'color': '#c03', 'background-color':'#000' });
			
		}else{
			$(this.btn).removeClass('subMenuParentBtn');
			$(this.btn).addClass('subMenuParentBtnFocused');
			//var myMorph = new Fx.Morph(this.btn, { 'duration': 10 });
			//myMorph.start('#nav a:hover, #nav a:focus, #subMenusContainer a:hover, #subMenusContainer a:focus');
		}
		
		this.childMenu.setStyles({'display':'block','visibility':'hidden'});
		
		if(!this.width || !this.height ){
			//this.width = this.childMenu.getFirst().getOffsetSize().x;	
			//this.height = this.childMenu.getFirst().getOffsetSize().y;
			this.width = this.childMenu.getFirst().getCoordinates().width;
			this.height = this.childMenu.getFirst().getCoordinates().height;
			
			if(this.options.effect === 'slide' || this.options.effect === 'slide & fade'){
				//console.log(this.width+ ' | '+this.height);
				if (this.subMenuType == 'initial' && this.options.orientation === 'horizontal') {
					this.childMenu.getFirst().setStyle('margin-top','0' );
					this.myEffect.set({ 'margin-top': - this.height });
				}else{
					if(this.options.xDirection === 'left'){
						this.childMenu.getFirst().setStyle('margin-left','0' );
						this.myEffect.set({ 'margin-left': this.width });
					}else{
						this.childMenu.getFirst().setStyle('margin-left','0' );
						this.myEffect.set({ 'margin-left': -this.width });
					}
					
				}
			}
		}
		
		
		
		//position subMenu
		this.positionSubMenu();
		

			
		if(this.options.effect === 'slide' ){
			
			this.childMenu.setStyles({'display':'block','visibility':'visible'});
			
			if (this.subMenuType === 'initial' && this.options.orientation === 'horizontal') {
				if(now){
					this.myEffect.set({ 'margin-top': 0 });
				}else{
					this.myEffect.start({ 'margin-top': 0 });
				}
				
		
			}else{
				if (now) {
					this.myEffect.set({ 'margin-left': 0 });
				}else{
					this.myEffect.start({ 'margin-left': 0 });
				}
				
			}
			
		}else if(this.options.effect === 'fade' ){
			if (now) {
				this.myEffect.set({'opacity': this.options.opacity});
			}else{
				this.myEffect.start({'opacity': this.options.opacity});
			}
		
		}else if(this.options.effect == 'slide & fade'){
		
			this.childMenu.setStyles({'display':'block','visibility':'visible'});
			
			if (this.subMenuType === 'initial' && this.options.orientation === 'horizontal') {
				if (now) {
					this.myEffect.set({ 'margin-top': 0, 'opacity': this.options.opacity });
				}else{
					this.myEffect.start({ 'margin-top': 0, 'opacity': this.options.opacity });
				}
				
		
			}else{
				if (now) {
					this.myEffect.set({ 'margin-left': 0, 'opacity': this.options.opacity });
				}else{
					this.myEffect.start({ 'margin-left': 0, 'opacity': this.options.opacity });
				}
				

			}
			
		}else{
			this.childMenu.setStyles({'display':'block','visibility':'visible'});
			
			//this.childMenu.effect('opacity', {duration: 0, transition: Fx.Transitions.linear}).start(0.5,1);
		}
		this.hidden = false;
		
	},
	
	positionSubMenu: function(){
		//set the width so it does does get squeezed but goes off the edge of the document so it can be detected & redirected		
		//this.childMenu.setStyle('width',this.childMenu.getOffsetSize().x) ;
		//this.childMenu.getFirst().setStyle('width',this.childMenu.getFirst().getOffsetSize().x) ;
		//console.log(this.width);
		
		this.childMenu.setStyle('width',this.width) ;
		this.childMenu.getFirst().setStyle('width',this.width) ;
				
		//if any parent has bounced off a viewport edge, inherit that new direction
		if (this.subMenuType === 'subsequent') {
			if(this.options.xDirection != this.parentSubMenu.options.xDirection){
				if(this.parentSubMenu.options.xDirection === 'left'){this.myEffect.set({ 'margin-left': this.width, 'opacity': this.options.opacity });	}
			}
			this.options.xDirection = this.parentSubMenu.options.xDirection;
			this.options.yDirection = this.parentSubMenu.options.yDirection;
		}
		
		
				
		
		if(this.subMenuType == 'initial'){
			if(	this.options.yDirection == 'up'){
				if(this.options.orientation == 'vertical'){
					this.childMenu.style.bottom = this.btn.getPosition().y- this.btn.getOffsetSize().y + this.options.initialSubMenuYoffset + 'px';
				}else{
					this.childMenu.style.bottom = this.btn.getPosition().y + this.options.initialSubMenuYoffset + 'px';
				}
			}else if(this.options.orientation == 'horizontal'){
			
				//alert(this.btn.getParent('li').getParent('ol').getParent('td').getParent('tr').getParent('table').getCoordinates().top);
				
				//this.childMenu.style.top = this.btn.getParent('li').getParent('ol').getParent('td').getParent('tr').getParent('table').getCoordinates().top + this.options.initialSubMenuYoffset + 'px';
				this.childMenu.style.top = this.btn.getCoordinates().bottom + this.options.initialSubMenuYoffset + 'px';
			
			}else if(this.options.orientation == 'vertical'){
				
				this.childMenu.style.top = this.btn.getPosition().y + this.options.initialSubMenuYoffset + 'px';	
			
			}
			
			if(	this.options.orientation == 'horizontal'){
				//this.childMenu.style.left = btnValues.left + this.options.initialSubMenuXoffset + 'px';
				
				
				
				//alert(this.btn.getPosition().x + this.options.initialSubMenuXoffset);
				this.childMenu.style.left = this.btn.getCoordinates().left + this.options.initialSubMenuXoffset + 'px';
				
				
				
			}else if(this.options.xDirection == 'left'){
				this.childMenu.style.left = this.btn.getPosition().x - this.childMenu.getOffsetSize().x + this.options.initialSubMenuXoffset + 'px';
			}else if(this.options.xDirection == 'right'){
			
				this.childMenu.style.left = this.btn.getCoordinates().right + this.options.initialSubMenuXoffset + 'px';
				//this.childMenu.style.left = btnValues.right + this.options.initialSubMenuXoffset + 'px';
			}
			
		}else if(this.subMenuType == 'subsequent'){
			
			this.childMenu.style.top = this.btn.getPosition().y + this.options.subsequentSubMenuYoffset + 'px';	
			
			
			if(this.options.xDirection == 'left'){
				this.childMenu.style.left = this.btn.getCoordinates().left - this.childMenu.getCoordinates().width + this.options.subsequentSubMenuXoffset + 'px';
				
				if( this.childMenu.getPosition().x < 0){
					this.options.xDirection = 'right';
					this.childMenu.style.left = this.btn.getPosition().x + this.btn.getOffsetSize().x + this.options.subsequentSubMenuXoffset + 'px';
					
					if(this.options.effect === 'slide' || this.options.effect === 'slide & fade'){
						this.myEffect.set({ 'margin-left': -this.width, 'opacity': this.options.opacity });
					}
					
				}
				
			}else if(this.options.xDirection == 'right'){
				
				this.childMenu.style.left = this.btn.getCoordinates().right + this.options.subsequentSubMenuXoffset + 'px';
				
				//this.childMenu.getParent().setStyles({'display':'block','visibility':'visible', 'opacity':'1'});
				//console.log(this.childMenu);
				
				var smRight = this.childMenu.getCoordinates().right;
				//console.log('sm right= '+ smRight + ' | '+ 'viewport width= '+ document.getOffsetSize().x + " | " + 'viewport scroll width= '+ document.getScrollSize().x );		
			
				if( smRight > document.getCoordinates().width ){
					this.options.xDirection = 'left';
					this.childMenu.style.left = this.btn.getCoordinates().left - this.childMenu.getCoordinates().width + this.options.subsequentSubMenuXoffset + 'px'
					if (this.options.effect === 'slide' || this.options.effect === 'slide & fade') {
						this.myEffect.set({	'margin-left': this.width,	'opacity': this.options.opacity	});
					}
				}
				
			}
			
		}
	}	

});

MooSubMenu.implement(new Options);
MooSubMenu.implement(new Events);

/*--------------------------------------------------------------/End MooMenu  /--------------------------------------------------------------*/

/*--------------------------------------------------------------/Begin MooScroll  /--------------------------------------------------------------*/
/**
 * @author Jason Jaeger
 */

var MooScroll = new Class({
	options: {
        id: 'content',
		increment:30,
		upBtnClass:'upBtn',
		downBtnClass:'downBtn',
		scrollBarClass:'scrollBar',
		scrollHandleClass:'scrollHandle'
    },
	
	container:null,
	downInterval:null,
	upInterval:null,
	overHang:null,
	handleHeight:null,
	slider:null,
	
	initialize: function(options){
        this.setOptions(options);
		this.container = $(this.options.id);
		
		
		var upBtn = new Element('div', {	'class': this.options.upBtnClass	}).inject(this.container,'after');
		var downBtn = new Element('div', {	'class': this.options.downBtnClass	}).inject(this.container,'after');
		var scrollBar = new Element('div', {	'class': this.options.scrollBarClass	}).inject(this.container,'after');
		var scrollHandle = new Element('div', {	'class': this.options.scrollHandleClass	}).inject(scrollBar,'inside');
	
		
		this.container.setStyles({'overflow':'hidden', 'padding-right':parseFloat(this.container.getStyle('padding-right'))+upBtn.getCoordinates().width});
		//scrollBar.setStyles({'top':upBtn.getCoordinates().bottom,'height':this.container.getCoordinates().height - upBtn.getCoordinates().height - downBtn.getCoordinates().height});
		//upBtn.setStyles({'top':this.container.getCoordinates().top, 'left':this.container.getCoordinates().right});
		
		
		//calculate scrollHandle height
		this.overHang = this.container.getScrollSize().y - this.container.getCoordinates().height;
		var handleHeightPercent = 100 - ((this.overHang*100)/this.container.getScrollSize().y);
		this.handleHeight = (handleHeightPercent*this.container.getCoordinates().height)/100;
		if(scrollHandle.getStyle('min-height') && this.handleHeight < parseFloat(scrollHandle.getStyle('min-height'))){
			this.handleHeight = parseFloat(scrollHandle.getStyle('min-height'));
		}
		scrollHandle.setStyles({'height':this.handleHeight});

		if(this.overHang <=0){
			scrollHandle.setStyles({'display':'none'});
			upBtn.setStyles({'opacity':0.5});
			downBtn.setStyles({'opacity':0.5});
			scrollBar.setStyles({'opacity':0.5});
			return;
		}

		this.slider = new Slider(scrollBar, scrollHandle, {	
			steps: this.overHang,	
			mode: 'vertical',	
			onChange: function(step){
				this.container.scrollTo(0, step);
			}.bind(this)
		}).set(0);
		
		this.container.addEvent('mousewheel', function(e){
			e = new Event(e).stop();
			
			/* Mousewheel UP */
			if (e.wheel > 0) { this.scrollUp(); } 
			
			/* Mousewheel DOWN*/
			else if (e.wheel < 0) { this.scrollDown(); }
			
		}.bind(this));
		
		
		upBtn.addEvents({
				'mousedown': function(e){
					e = new Event(e).stop();
					$clear(this.upInterval);
					$clear(this.downInterval);
					this.upInterval = this.scrollUp.periodical(50,this);
				}.bind(this),
				
				'mouseup': function(e){
					e = new Event(e).stop();
					$clear(this.upInterval);
					$clear(this.downInterval);
				}.bind(this),
				
				'mouseout': function(e){
					e = new Event(e).stop();
					$clear(this.upInterval);
					$clear(this.downInterval);
				}.bind(this)
		});
			
		downBtn.addEvents({
				'mousedown': function(e){
					e = new Event(e).stop();
					$clear(this.upInterval);
					$clear(this.downInterval);
					this.downInterval = this.scrollDown.periodical(50,this);
				}.bind(this),
				
				'mouseup': function(e){
					e = new Event(e).stop();
					$clear(this.upInterval);
					$clear(this.downInterval);
				}.bind(this),
				
				'mouseout': function(e){
					e = new Event(e).stop();
					$clear(this.upInterval);
					$clear(this.downInterval);
				}.bind(this)
		});

    },

	scrollUp:function(){
		var target = this.container.getScroll().y - this.options.increment;
		this.container.scrollTo(0,target );
		this.slider.set(target);
	},
	
	scrollDown:function(){
		var target = this.container.getScroll().y + this.options.increment;
		this.container.scrollTo(0, target);
		this.slider.set(target);
	}
 
});
MooScroll.implement(new Options);
MooScroll.implement(new Events);
/*--------------------------------------------------------------/End MooScroll  /--------------------------------------------------------------*/

window.addEvent('load', function() { 
	//initTimeout = setTimeout("playFlash()",(500));
	loadMotuFlash.delay(3000);
});

window.addEvent('domready', function() { 

	var menu = new MooMenu({ 
		orientation:'horizontal', 
		effect:'slide & fade', 
		duration:250, 
		transition:Fx.Transitions.Quad, 
		opacity:99, 
		subsequentSubMenuXoffset:-2,
		initialSubMenuYoffset:0,
		initialSubMenuXoffset:-5//,
		//phpFile:'http://massagemotu.com/resources/MooFont/fontReplacement.php'  
	});	
	
	var contentScrollArea = new MooScroll();
	
	var presentFont = new MooFont({
		selector:'#nav a', 
		size:14, 
		color:'#CC3232',
		bgColor:'#DFF6F9',
		color_hover:'#213C36',
		bgColor_hover:'#BFF8FF'//,
		//phpFile:'http://massagemotu.com/resources/MooFont/fontReplacement.php'  
	});
	
	var abadiFont = new MooFont({
		selector:'h1,h2', 
		color:'#253F39', 
		color_hover:'#253F39', 
		bgColor:'#DFF6F9', 
		bgColor_hover:'#DFF6F9' 
	});
	

});

function loadMotuFlash(){
	if (Swiff.getVersion() > 6) {
		var soundObj = new Swiff('resources/volume_final.swf', {
			container: $('flashBox'),
			id: 'soundControlPanel',
			width: 20,
			height: 110
		});
	}
}