document.addEvent('domready', function(){
	
	if($('right')){
		if($('right').getChildren().length == 0) $('right').destroy();
	}

	$$('.h').addEvents({
		'mouseenter' : function() {
			this.addClass('hover');
		
			if(this.hasClass('m')){
				var tmp_position = this.position();
				var tmp_menu 	 = this.children('.sub');
				tmp_menu.css('left', tmp_position.left);
			}
		},
		'mouseleave' : function() {
			$(this).removeClass('hover');
		}
	});


	// Le champ de la newsletter
	//
	$$('.mymail').addEvents({
		'focus' : function(){	
			if(this.value == 'Mon mail') this.value = '';
		},
		'blur' : function(){
			if(this.value == '') this.value = 'Mon mail';
		}
	});



	// Le champ de la recherche
	//
	if($('mysearch')){
		$('mysearch').addEvents({
			'focus' : function(){	
				if(this.value == 'Nom du produit / Materiaux / Ville') this.value = '';
			},
			'blur' : function(){
				if(this.value == '') this.value = 'Nom du produit / Materiaux / Ville';
			}
		});
	}


	// Simu
	//
	$$('.simuSelect').addEvents({
		'mouseenter' : function(){
			$(this).addClass('hover');
		},
		'mouseleave' : function(){
			$(this).removeClass('hover');
		}
	});


	// Carou
	//
	$$('.carouWrapp').each(function(value, index){
		new myCarou({
			element: value
		});
	});


	// Flowplayer
	//
	flowplayer("a.player", "/media/ui/flowplayer/flowplayer-3.2.7.swf");

});

function mySearch(){
	if($('mysearch').value == defautSearch) $('mysearch').value = '';
	$('fs').submit();
}

/* + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - 
 + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - */
myCarou = new Class({

    initialize: function(opt){

    	this.element	= opt.element;
		this.ul			= this.element.getElement('ul');
		this.li			= this.element.getElements('li');
		this.width		= $(this.li[0]).getStyle('width').toInt()
		this.total		= this.li.length;
		this.cur		= 0;
		this.visible	= Math.round($(this.element.getElement('.w')).getStyle('width').toInt() / this.width);
		this.left		= this.element.getElement('.left');
		this.right		= this.element.getElement('.right');

		this.left.addEvent('click',		this.goLeft.bind(this));
		this.right.addEvent('click',	this.goRight.bind(this));
	},

	move: function(){
		var togo = this.cur  * this.width * -1;

		$(this.ul).morph({
			'margin-left': togo
		});
	},

	goLeft: function(){
		this.cur = (this.cur - this.visible <= 0)
			? 0
			: (this.cur - this.visible);

		this.move();
	},

	goRight: function(){

		if(this.cur == this.total - this.visible){
			this.cur = 0;
		}else{
			this.cur = (this.cur + this.visible >= this.total - this.visible)
				? (this.total - this.visible)
				: (this.cur + this.visible);
		}

		this.move();
	}


});


/* + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - 
 + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - */
mySlide = new Class({
    initialize: function(opt){

    	this.element	= opt.element;
    	this.items		= this.element.getElements('li.is');
    	this.numero		= this.element.getElement('.numero');
    	this.max		= this.items.length - 1;
    	this.current	= 0;
    	this.play		= 'play';
    	this.timer		= null;

		this.items.setStyle('opacity', 0);
		this.items[0].setStyle('opacity', 1);

		this.items.set('morph', {
			'duration'  : '500',
			'onComplete': function(){
				if(this.element.getStyle('opacity') == 0) this.element.setStyle('display', 'none');
			}
		});
		
		this.items.addEvents({
			'mouseenter' : this.mouseEnter.bind(this),
			'mouseleave' : this.mouseLeave.bind(this)
		});

    	for(i=0; i<this.items.length; i++){
    		new Element('a', {
    			'html' : (i+1),
    			'class' : ((i == 0) ? 'me' : ''),
    			'events' : {
    				'click' : this.setCurrent.bind(this, i)
    			}
    		}).addClass('a'+i).inject(this.numero, 'bottom');
    	}


		this.start();
    },
    
    start: function(){
    	this.play	= 'play';
		this.timer	= this.goNext.periodical(4000, this);
	},

	stop : function(){
    	this.play	= 'stop';
    	clearInterval(this.timer);
	},
    
    mouseEnter: function(){
    	this.stop();
    },
    
    toggle: function(){
		(this.play == 'stop' ) ? this.start() : this.stop();
    },
   
    mouseLeave: function(){
    	if(this.play == 'stop') this.start();
    },
  
    goNext: function(){
		var next = ((this.current + 1) > this.max) ? 0 : (this.current + 1);
		this.setCurrent(next);
    },

    goPrevious: function(manuel){
    	if(manuel) this.stop();
		var previous = ((this.current -1 < 0)) ? (this.max) : (this.current - 1);
		this.setCurrent(previous);
    },

    other: function(){
    	var a = [];
    	for(i=0; i<this.items.length; i++){
    		if(i != this.current) a.push(this.items[i]);
    	}
    	return a;
    },
    
    setCurrent: function(n){
		this.current = n;

		this.other().each(function(m){
			m.setStyle('display', 'block');
			m.morph({'opacity' : 0});
		});
		
		this.numero.getElements('a').removeClass('me');
		this.numero.getElements('a')[this.current].addClass('me');

		this.items[this.current].setStyle('display', 'block').morph({'opacity' : 1});
    }

});




