/*
 *  Diaporama 0.1 - jQuery plugin
 *  written by Didier Coiffier  
 *
 *  Copyright (c) 2011 Didier Coiffier (http://gweladur.fr)
 *
 */
 

(function($) {

    $.fn.diaporama = function(options){
      
        // default config
        var defaults = {
            showControls:   true,
            idPrev:         'previous',
            idNext:         'next',
            idAuto:         'autoButton',
            titlePrev:      'Précédent',
            titleNext:      'Suivant',
            speed:          800,
            auto:           true,
            pause:          2000,
            continuous:     true,
            diaporamaId:    'Diaporama',
            diapoDivClass:  '.Diapo',
            diapoWidth:     800,
            diapoHeight:    600,
            darkenColor:    'rgba(0,0,0,0.1)'
        }; 
        
        // update with selected options
        var options = $.extend(defaults, options);  

        // Wrap diapos with diaporama frame and diapo wrapper
        $(options.diapoDivClass).wrapAll("<div id=\""+options.diaporamaId+"\"><div id=\"diaWrapper\"> </div></div>");

        // get the number of diapo
        var numOfDia = $(options.diapoDivClass).length;
        // set the current diapo to the first
        var currentDia = 0;
        // get index of last diapo
        var lastDia = numOfDia-1;
        // controls are clickable
        var clickable = true;

        // update diapo list for continuous scrolling
        if(options.continuous)  {
            $(options.diapoDivClass+":first-child").before($(options.diapoDivClass).eq(lastDia).clone());
            $(options.diapoDivClass+":last-child").after($(options.diapoDivClass).eq(1).clone());
            $(options.diapoDivClass+":first-child").css("margin-left","-"+ options.diapoWidth +"px");
        }               

        $(options.diapoDivClass).css('float','left');
        $(options.diapoDivClass).css('height',options.diapoHeight+"px");
        $(options.diapoDivClass).css('width',options.diapoWidth+"px");
        $(options.diapoDivClass).css('background-color',options.darkenColor);
        
        $("#diaWrapper").css('width',(numOfDia+1)*options.diapoWidth);
        
        $("#"+options.diaporamaId).css("overflow","hidden");
        
        if(options.showControls){
            var controlHtml = '';               
            controlHtml += ' <span id="'+ options.idPrev +'"><a href=\"javascript:void(0);\">'+ options.titlePrev +'</a></span>';
            controlHtml += ' <span id="'+ options.idNext +'"><a href=\"javascript:void(0);\">'+ options.titleNext +'</a></span>';
            $("#"+options.diaporamaId).after(controlHtml);                                      
            $("a","#"+options.idNext).click(function(){     
                animate("next",true);
            });
            $("a","#"+options.idPrev).click(function(){     
                animate("prev",true);               
            });
        }

        function adjust(){
            if(currentDia>lastDia) {currentDia=0;}
            if(currentDia<0) {currentDia=lastDia;}
            $("#diaWrapper").css("margin-left",(currentDia*options.diapoWidth*-1));
            clickable = true;
        } // adjust
            
        function animate(dir,clicked){
            if (clickable){
                clickable = false;
                var oldDia = currentDia;                
                switch(dir){
                    case "next":
                        currentDia = (oldDia>=lastDia) ? (options.continuous ? currentDia+1 : lastDia) : currentDia+1;                      
                        break; 
                    case "prev":
                        currentDia = (currentDia<=0) ? (options.continuous ? currentDia-1 : 0) : currentDia-1;
                        break; 
                    default:
                        currentDia = dir;
                        break; 
                }   
                var delta = Math.abs(oldDia-currentDia);
                var speed = delta*options.speed;                        
                shift = (currentDia*options.diapoWidth*-1);
                $("#diaWrapper").animate(
                    { marginLeft: shift }, 
                    { queue:false, duration:speed, complete:adjust }
                );              
                    
                if(!options.continuous && options.showControls){                    
                    if(currentDia===0){
                        $("a","#"+options.idPrev).hide();
                    } else {
                        $("a","#"+options.idPrev).show();
                    }                   
                    if(currentDia===lastDia){
                        $("a","#"+options.idNext).hide();
                    } else {
                        $("a","#"+options.idNext).show();
                    }
                }               
                    
                if(clicked) {clearTimeout(timeout);}
                if(options.auto && dir==="next" && !clicked){
                    timeout = setTimeout(function(){
                        animate("next",false);
                    },delta*options.speed+options.pause);
                }
            
            }
                
        } // animate
        
        // init
        var timeout;
        if(options.auto){
            timeout = setTimeout(function(){
                    animate("next",false);
                },options.pause);
        }                                   
                            
        if(!options.continuous && options.showControls){                    
            if(currentDia===0){
                $("a","#"+options.idPrev).hide();
            } else {
                $("a","#"+options.idPrev).show();
            }                   
        }               
    };
 
})(jQuery);

