/*
 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 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 initFancyMenu()
{	
	if($('top_nav'))
	{
		$('top_nav').getElements('li').getLast().addClass('last');		
		fancyMenu = new MorphList($("top_nav"),'current', {transition: Fx.Transitions.quadOut, duration: 500, onClick: function(ev, item) { ev.stop(); }});		
	}
}

// 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');	
}
function getWindowHeight() {
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	}
	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		}
		else {
			if (document.body && document.body.clientHeight) {
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}
function setFooter() {
	if (document.getElementById) {
		var windowHeight = getWindowHeight();
		if (windowHeight > 0) {
			var headerHeight = 0;
			var contentHeight = document.getElementById('container').offsetHeight;
			var footerElement = document.getElementById('footer');
			var footerHeight  = footerElement.offsetHeight;
			if (windowHeight - (headerHeight + contentHeight + footerHeight) >= 0) {
				footerElement.style.height = (windowHeight-contentHeight) + 'px';
			}
			else {
				footerElement.style.height = 200 + 'px';
			}
		}
	}
}

window.onresize = function() {
	setFooter();
}

window.addEvent('domready', initFancyMenu);
window.addEvent('domready', initBottomNav);
window.addEvent('domready', initExternalLinks);
window.addEvent('domready', setFooter);
