$.fn.carrouselCfi = function(time, duree){

    var time = time || 1000,
    duree = duree || 7000;

    this.each(function() {

        var self = $(this),
        items = $('img', self),
        btn_left = $('.btnLeft', self),
        btn_right = $('.btnRight', self),
        btn_bottom = $('.btnBottom', self),
        effect = $('.carrouselEffect', self),
        content = items.wrapAll('<div />').parent().addClass('carrouselImg').width(50000);
		
        play();

        items.each(function() {
			
            var img = $(this),
            round = $('<span />');

            btn_bottom.append(round);
			

            round.click(function() {

                var index = round.index();
                btn_left.show();
                btn_right.show();
                round.parent().find('.current').removeClass('current');
                round.addClass('current');
                items.filter('.current').removeClass('current');
                items.eq(index).addClass('current');

                if (index <= 0) {
                    btn_left.hide();
                }

                if (index >= (items.length - 1)) {
                    btn_right.hide();
                }

                content.stop().animate({
                    marginLeft: -(self.width() * index)
                }, time);

            });

			img.height(self.height()).width(self.width());
			
			// $('.carrouselEffect').attr('title',toto);

        });

        effect.hover(function() {
            
			var toto = $('.current', content).attr('title');
			effect.attr('title',toto);

		});
        effect.click(function() {
            
			// document.location.href = $('.current', content).attr('title');
			document.location.href = $('.current', content).data('lien');
			
		});

        btn_left.click(function() {

			var round_current = $('span.current', btn_bottom),
            round_prev = round_current.prev();

            if (round_prev.length != 0) {

                round_prev.trigger('click');

            } else {

                $('span:last', btn_bottom).trigger('click');

            }

		});

        btn_right.click(function() {

			var round_current = $('span.current', btn_bottom),
            round_next = round_current.next();

            if (round_next.length != 0) {

                round_next.trigger('click');

            } else {

                $('span:first', btn_bottom).trigger('click');

            }

		});

        $('span:first', btn_bottom).trigger('click').css('margin-left', Math.floor(self.width() - (items.length * 20)) / 2);

        function play() {

            setTimeout(function() {

                btn_right.trigger('click');

                play();

            }, duree);

        }

    });
	

};

/* menu de la home*/
$(function() { 

	// $('nav li div').css('display','none');
	// $('nav li').mouseover(function(){
		// this.find('div').css('display','block');
	// });
	loadsearch();
	$('nav li div').hide();

	$('nav li').hover(function() {
		// $('div.image_home').hide();
		$(this).find('div').show();
		
		if ($(this).hasClass('ras')){ 
			$('section.chapeau').css('visibility','hidden');
			$('#menu_home').css('background-position','0 0');
		} 
		else if ($(this).hasClass('acc')){
			$('section.chapeau').css('visibility','hidden');
			$('#menu_home').css('background-position','0 -140px');
		}
		else if ($(this).hasClass('end')){
			$('section.chapeau').css('visibility','hidden');
			$('#menu_home').css('background-position','0 -280px');
		 } 
		 else if ($(this).hasClass('dec')){
		 	$('section.chapeau').css('visibility','hidden');
			$('#menu_home').css('background-position','0 -420px');
		} 
	}, function() {
		$(this).find('div').hide();
		$('section.chapeau').css('visibility','visible');
		$('#menu_home').css('background-position','0 140px');
	
	})
	
});

$(function() { 
	//Appel le plugin
	$('.carrouselCfi').carrouselCfi(1000,7000);
});

