var prevMenu = false;

Menu = new Class
({
	Implements: [Options],
	
	options: { },
	
	initialize : function(menuId) 
	{
		this.menuId = 'r_'+menuId;
		
		this.sousMenu = $('child_r_'+menuId);
		
		if($(this.sousMenu))
		{
			$(this.menuId).addEvent('mouseenter', this.show.bind(this) );
			$(this.menuId).addEvent('mouseleave', this.startHide.bind(this) );
			$(this.sousMenu).addEvent('mouseenter', this.show.bind(this) );
			$(this.sousMenu).addEvent('mouseleave', this.startHide.bind(this) );
		}
	},
	
	show : function()
	{
		this.mouseIsOn = true;
		
		// On ferme tous le menu précédent
		if(prevMenu)
			prevMenu.hide();
		
		this.sousMenu.setStyle('visibility', 'visible');
		
		$(this.menuId).getElement('a').addClass('selected');
		
		prevMenu = this;
	},
	
	startHide : function()
	{
		this.mouseIsOn = false;
	
		(this.hide.bind(this)).delay(400);
	},
	
	hide : function()
	{
		if(!this.mouseIsOn)
		{
			this.sousMenu.setStyle('visibility', 'hidden');
			
			$(this.menuId).getElement('a').removeClass('selected');
		}
	}
	
});


PopUp = new Class
({
	Implements: [Options],
	
	options: 
	{ 
		popUpWidth : 700
	},
	
	initialize : function(e, popUpContent, popUpClass, popUpWidth) 
	{
		if(e)
			new Event(e).stop();
		
		if($('popUp'))
			$('popUp').dispose();
			
		if($('popUpOverlay'))
			$('popUpOverlay').dispose();
			
		var div = new Element('div', {'id' : 'popUp', 'class' : popUpClass}).set('html', popUpContent);
		
		this.options.popUpWidth = popUpWidth;
		
		div.setStyles(
		{
			'width'		: this.options.popUpWidth+'px',
			'position'	: 'absolute',
			'left'		: (document.getSize().x - this.options.popUpWidth) / 2,
			'top'		: (50 + window.getScroll().y)+'px',
			'opacity'	: 0
		});
		
		var overlay = new Element('div', { 'id':'popUpOverlay','styles':{ 'opacity':0,'visibility':'visible','height':0,'overflow':'hidden' }}).inject($(document.body));
		
		overlay.setStyles({ 'top': -$(window).getScroll().y,'height':$(window).getScrollSize().y+$(window).getScroll().y });
		
		overlay.fade(0.3);
		
		$(document.body).adopt(div);
		
		$('popUp').fade(1);
	},
	
	close : function(e)
	{
		new Event(e).stop();
	
		$('popUp').fade(0);
	
		$('popUpOverlay').fade(0);
	}
});

Banner = new Class
({
	Implements: [Options],
	
	options: { },
	
	openedImg 	: false,
	nbImg		: 0,
	fader 		: false,
	
	fadeDuration	: 6000,
	
	timer		: false,
	
	zIndex		: 0,
	
	initialize : function() 
	{
		$$('#banner_imgs div').each(function(item, index)
		{
			item.setStyles
			({
				'opacity' : 0,
				'visibility' : 'visible'
			});
		});
		
		if(!this.openedImg)
		{
			var imgs = $('banner_imgs').getElements('div');
			
			this.nbImg = imgs.length;
			
			this.openedImg = Math.floor(Math.random() * (imgs.length)).toInt();

			
			// On masque l'image affichée
			this.fader = new Fx.Tween('img_'+this.openedImg, {duration: 'long'});

			this.fader.start('opacity', 1);
			
			this.zIndex++;
			
			$('img_'+this.openedImg).setStyle('z-index', this.zIndex);
		}
		
		$$('#banner_imgs div').addEvent('mouseenter', this.clearTimer.bind(this) );
		
		$$('#banner_imgs div').addEvent('mouseleave', this.setTimer.bind(this) );
		
		$('ban_nav_next').addEvent('click', this.showNext.bind(this) );
		
		$('ban_nav_prev').addEvent('click', this.showPrev.bind(this) );
		
		if(!this.timer && this.nbImg > 1)
			this.timer = this.alter.periodical(this.fadeDuration, this);
	},
	
	clearTimer : function()
	{
		if(this.timer)
			$clear(this.timer);
		
		this.timer = false;
	},
	
	setTimer : function()
	{
		if(!this.timer && this.nbImg > 1)
			this.timer = this.alter.periodical(this.fadeDuration, this);
	},
	
	alter : function()
	{
		// On masque l'image affichée
		this.fader = new Fx.Tween('img_'+this.openedImg, {duration: 'long'});
		
		this.fader.start('opacity', 0);
		
		
		// Images suivante à afficher
		this.openedImg++;
		
		if(this.openedImg >= this.nbImg)
			this.openedImg = 0;
			
			
		this.fader = new Fx.Tween('img_'+this.openedImg, {duration: 'long'});
		
		this.fader.start('opacity', 1);
		
		this.zIndex++;
		
		$('img_'+this.openedImg).setStyle('z-index', this.zIndex);
	},
	
	showNext : function()
	{
		if(this.nbImg < 2)
			return;
			
		this.clearTimer();
		
		// On masque l'image affichée
		this.fader = new Fx.Tween('img_'+this.openedImg, {duration: 'long'});
		
		this.fader.start('opacity', 0);
		
		
		// Images suivante à afficher
		this.openedImg++;
		
		if(this.openedImg >= this.nbImg)
			this.openedImg = 0;
			
			
		this.fader = new Fx.Tween('img_'+this.openedImg, {duration: 'long'});
		
		this.fader.start('opacity', 1);
		
		this.zIndex++;
		
		$('img_'+this.openedImg).setStyle('z-index', this.zIndex);
		
		this.setTimer();
	},
	
	showPrev : function()
	{
		if(this.nbImg < 2)
			return;
			
		this.clearTimer();
		
		// On masque l'image affichée
		this.fader = new Fx.Tween('img_'+this.openedImg, {duration: 'long'});
		
		this.fader.start('opacity', 0);
		
		
		// Images précédente à afficher
		this.openedImg--;
		
		if(this.openedImg < 0)
			this.openedImg = this.nbImg - 1;
			
			
		this.fader = new Fx.Tween('img_'+this.openedImg, {duration: 'long'});
		
		this.fader.start('opacity', 1);
		
		this.zIndex++;
		
		$('img_'+this.openedImg).setStyle('z-index', this.zIndex);
		
		this.setTimer();
	}
});


var config = {'base' : ''};

window.addEvent('domready', function()
{	
	// Les rubriques
	new Menu(1);
	new Menu(2);
	new Menu(3);
	new Menu(4);
	new Menu(5);
	
	// Bannière
	if($('banner'))
		new Banner();
});
