
var caroussel = {
    
    current: 0,
    timer: null,
    delay: 5000,
    images: Array(),
    
    load: function(p) {
        if(p.delay) this.delay = p.delay;
        if(p.images) this.images = p.images;
        caroussel.build();
    },
    
    build: function() {

        $(".caroussel .navigation").empty();
        
        if(this.images.length<=1) {
        	
        	var t = $(".caroussel .label .title").text();
        	var c = $(".caroussel .label .caption").text();
        	
	       	if($.trim(t)=='' && $.trim(c)=='') {
	        	$(".caroussel .label").hide();
	        }
        	return;
        }
        
        var i = 0;
        $(this.images).each(function(){
            $(".caroussel .navigation").append("<a href='#' onclick='return false;' class='caroussel_nav' linkto='"+i+"'>"+(i+1)+"</a>");
            i++;        
        });
        
        $(".caroussel .navigation a.caroussel_nav").click(function(){
            caroussel.show($(this).attr("linkto"));
        });
        
        $(".caroussel .navigation").mouseover(function(){
            caroussel.pause();
        });
        
        $(".caroussel .navigation").mouseout(function(){
            caroussel.resume();
        });
        
        this.start();
    },
    
    get: function(i) {
        return this.images[i];
    },
    
    show: function(i){
        var s = this.get(i).src;
        var d = (this.get(i).color != 'dark');
        var t = this.get(i).title;
        var c = this.get(i).caption;
        
        if($.trim(t)!='' || $.trim(c)!='') {
        	$(".caroussel .label span.title").text(t);
        	$(".caroussel .label span.caption").text(c);
        	$(".caroussel .label").show();
        } else {
        	$(".caroussel .label").hide();
        }
        
        if(this.current == i) {
            $(this).attr("src",s);
        } else {
            //$(".caroussel img.picture").fadeOut("100",function(){$(this).attr("src",e).fadeIn("100")});
            
            $("<img class='newpicture' />").appendTo($(".caroussel"));
            
            var o = $(".caroussel img.picture");
            var n = $(".caroussel img.newpicture");
            
            n.hide()
            .attr("src",s)
            .fadeIn(1000, function(){
            	o.remove();
            	n.addClass("picture").removeClass("newpicture");
            });
        }
        
        if(d) {
        	$('.caroussel .navigation').addClass('fonces');
        } else {
        	$('.caroussel .navigation').removeClass('fonces');
        }
        
        $('.caroussel [linkto!="'+i+'"]').removeClass('actif');
        $('.caroussel [linkto="'+i+'"]').addClass('actif');
        
        this.current = i;
    },
    
    next: function(){
        var i = eval(this.current) + 1;
        if(i>=this.nb()) i = 0;
        this.show(i);
    },
    
    start: function(){
        this.show(0);
        this.resume();
    },
    
    pause: function(){
        clearTimeout(this.timer);
    },
    
    resume: function(){
        this.timer = setInterval("caroussel.next()",this.delay);
    },
    
    nb: function(){
        return this.images.length;
    }
    
}


$(document).ready(function() {
	
	var images = new Array();
	
	$('.caroussel img').each(function(){
		images.push({ src: $(this).attr('src'),  color: $(this).attr('color'), title: $(this).attr('title'), caption: $(this).attr('caption'), });
		if($(this).hasClass('hidden')) $(this).remove();
	});
	
    caroussel.load({ images:images, delay: 5000 });
    
});
