/*
 Moogets - MorphList 0.5 (formerly SlideList, aka Fancy Menu)
	- MooTools version required: 1.2
	- MooTools components required: 
		Core: Fx.Tween, Fx.Morph, Selectors, Element.Event and dependencies
		More: -

	Changelog:
		- 0.1: First release
		- 0.2: MooTools 1.2 compatible
		- 0.3: Now alters morphs width/height/top properties by default
		- 0.4: Morphing code now part of the function and not of the event.
		- 0.5: setOpacity changed to fade('show') so that visibility css is altered.
*/

/* Copyright: Guillermo Rauch <http://devthought.com/> - Distributed under MIT - Keep this message! */
var currentActiveMenu;
var currentActiveMenuId;
var currentMenu;
var currentMenuId;
var myTimer;
var isWaiting				= false;
var fancyMenu;
var top_sub_nav				= [];
var prevOnglet 				= -1;
var currentOnglet 			= -1;
var isSubOver 				= false;
var myCheckSubOverTimer;
var topNavCoord 			= [];


var MorphList = new Class({   
	
	Implements: [Events, Options],
	
	options: {/*             
		onClick: $empty,
		onMorph: $empty,*/
		morph: { 'link': 'cancel' }
	},	
	
	initialize: function(menu,className, options) {

		var that = this;
		this.setOptions(options);
		this.menu = $(menu);
		this.menuitems = this.menu.getChildren();
		this.menuitems.addEvents({
			//mouseenter: function(){ that.morphTo(this); },
			//mouseleave: function(){ that.morphTo(that.current); },
			click: function(ev){ that.click(ev, this); }
		});       
		this.bg = new Element('li', {'class': 'background'}).adopt(new Element('div', {'class': 'left'}));		
		/* saturnino autre version 
		this.bg = new Element('li', {'class': 'background'});
		var left = new Element('div', {'class': 'left'});
		left.inject(this.bg);
		*/
		this.bg.inject(this.menu).set('morph', this.options.morph);
		this.setCurrent(this.menu.getElement('.current'));
	},          

	click: function(ev, item) {		
		this.setCurrent(item, true);
		//this.fireEvent('onClick', [ev, item]);		
	},
	
	setCurrent: function(el, effect){  
		if(el && ! this.current) {
			this.bg.set('styles', { left: el.offsetLeft, width: el.offsetWidth, height: el.offsetHeight, top: el.offsetTop });
			(effect) ? this.bg.fade('hide').fade('in') : this.bg.fade('show');
		}
		if(this.current) this.current.removeClass('current');
		if(el) this.current = el.addClass('current');    
	},         
         
	morphTo: function(to) {
		if(! this.current) return; 
		this.bg.morph({
			left: to.offsetLeft, top: to.offsetTop,
			width: to.offsetWidth, height: to.offsetHeight
		});
		this.fireEvent('onMorph', to);
	}

});





// JavaScript Document
// Author Christophe Iaïchouchen AKA saturnino pour IMAG'IN productions
// Copyright : aircalin Internationnal

function showFancySubMenu(n)
{	
	fancyMenu.morphTo($('nav_top_'+n));		
	
	for( i=0; i<top_sub_nav.length; i++)
	{
		if(top_sub_nav[i] != 'none')
		{
			top_sub_nav[i].removeClass('current_sub_nav');
		}		
	}
	
	currentActiveMenuId = n;
	if(top_sub_nav[currentActiveMenuId] != 'none')
		top_sub_nav[currentActiveMenuId].addClass('current_sub_nav');			
}
function getLink(client)
{
	var x 		= client.x;
	var space;
	for(i=0; i<topNavCoord.length; i++)
	{
		space = Number(topNavCoord[i][0].left + topNavCoord[i][1].x) ;
		if(x <= space && x>= topNavCoord[i][0].left)
		{
			return( $('nav_top_'+i).getElements('a').getProperty('href') );
		}
	}
	return '';
}

function checkSubOverTimer()
{
	if(isSubOver == false)
	{
		showMask();
		myTimer = hideMask.delay(800);
	}
}
function showMask(){
	if(currentOnglet != prevOnglet)
	{
		$('mask').setStyles({'display':'block'});
	}
	$('mask').setStyles({'display':'block'});
	isWaiting = true;
}

function hideMask(){
	$('mask').setStyles({'display':'none'});
	isWaiting = false;	
}
function initFancyMenu()
{
	
	if($('mask'))
		$('mask').setStyles({'opacity':.01});
	
	
	if($('top_nav'))
	{
		fancyMenu = new MorphList($("top_nav"),'current', {transition: Fx.Transitions.quadOut, duration: 500, onClick: function(ev, item) { ev.stop(); }});	
		
		$('top').addEvent('mouseleave', function(event) { 	
				currentPossibleMenuId = currentMenuId;
				showFancySubMenu(currentPossibleMenuId);
				isWaiting = false;			
		}.bind(this));
		
		$('top').addEvent('click', function(event) { 
				
				var href	=	getLink(event.client);
				if( href != '')				
					window.location = href;	
					
		}.bind(this));
		
		
		$('top_nav').getElements('li').each(function(item,i)
		{ 													   
			
			if(Browser.Engine.gecko && Browser.Platform.mac)
			{
				item.getElements('a').setStyle('font-weight','normal');
				item.getElements('a').setStyle('width','auto');
			}
			
			if(item.hasClass('current'))
			{
				currentActiveMenu = item;	
				currentActiveMenuId = i;
				currentMenu = item;	
				currentMenuId = i;
			}
			
			var myValues = item.getCoordinates();
			topNavCoord[i] = [];
			topNavCoord[i].push( myValues);
			myValues = item.getSize();
			topNavCoord[i].push( myValues);
			
			//correction du bug firefox 3 / MAC
			if(Browser.Engine.gecko )
			{
				//item.getElements('a').setStyle('font-weight','normal');
				//item.getElements('a').setStyle('width','auto');
			}else{
				item.setStyle('width',myValues.x);
			}
			
			item.addEvent('click', function(event) { 	
					event = new Event(event);
					event.stop();
				}.bind(this));

			
			if($('top_sub_nav_'+i))
			{					
				top_sub_nav[i] = $('top_sub_nav_'+i);
				
				top_sub_nav[i].addEvent('mouseenter', function(event) { 	
					isSubOver = true;							   
					hideMask();
							
				}.bind(this));
				
				top_sub_nav[i].addEvent('mouseover', function(event) { 	
					isSubOver = true;							   
					hideMask();
							
				}.bind(this));
				
				top_sub_nav[i].addEvent('mouseleave', function(event) { 	
					isSubOver = false;		
					hideMask();							
				}.bind(this));
				
				
				
				$('top_sub_nav_'+i).getElements('li').getLast().getElements('a').addClass('last');
				
				
			}else{
				top_sub_nav[i] = 'none';
			}
			
			
			item.addEvent('mouseover', function(event) { 
												 
				prevOnglet = currentOnglet;
				currentOnglet = i;
					
				if(isWaiting == false)
				{					
					myTimer = $clear(myTimer);
					showMask();	
					myTimer = hideMask.delay(800);
					currentPossibleMenuId = i;
					showFancySubMenu(currentPossibleMenuId);
					myCheckSubOverTimer = checkSubOverTimer.delay(850);
				}
						
			}.bind(this));	
			
			
		}.bind(this));
	
	}
}

// gestion des liens externes
function initExternalLinks()
{
	$each(document.links, function(item){
		item = new Element(item);
		if(item.hasClass('external'))
		{
		item.setProperty('target', '_blank');
		}		
	}.bind(this));
}

function initBottomNav()
{
	if($('bottom_nav'))
		$('bottom_nav').getElements('li').getLast().addClass('last');	
}
window.addEvent('domready', initFancyMenu);
window.addEvent('domready', initBottomNav);
window.addEvent('domready', initExternalLinks);