// D�clarations globales charg�s dans index.php
var rootUrl;
var imagesUrl;
var imagesThumbsUrl;
var imagesThumbsPrefix;
var imagesExt;
var mosaicSize;
var images;

// INITIALISATION GLOBALE - Se d�clenche � la fin du chargement de la page
window.addEvent('domready', function() {

    // Conteneur principal de la mosaique
    var mosaicContainer = $('mosaic');

    // Injection des cellules de la mosaique dans le conteneur
    for(i=0; i<Math.min(mosaicSize,images.length); i++){
        var cell = new Element('li', {'class':'mosaic_cell'});
            cell.inject(mosaicContainer);
    }

    // Association d'une classe de comportement � chacune des cellules pr�c�demment cr��es
    $$('.mosaic_cell').each(function(item){
        var projectContent = new ProjectContent({cell : item});
        var changeProject = function() {
            projectContent.closeProject();
            projectContent.openProject();
        }

        changeProject.periodical(Math.floor(Math.random()*30000)+10000);
    });

});

//***********************************************
//*              CLASSES                        *
//***********************************************
var ProjectContent = new Class({

    cell : null,
    projectIndex : null,
    projectButton : null,
    projectButtonFx : null,

    Implements: Options,
    options: {
        cell : null
    },

    /********************************************
        * CONSTRUCTOR
        */

    initialize : function(options) {
        // Initialisation des options
        this.setOptions(options);
        this.cell = this.options.cell;

        // Ouverture d'un projet
        this.openProject();
    },

    /********************************************
     	* METHODS
     	*/

    openProject : function() {
        // On choisi et on reserve un projet au hasard parmis ceux disponibles
        this.projectIndex = this.reserveProject();

        // On cr�� le bouton du projet dans la cellule
        this.createProjectButton();

        // On fait apparaitre le bouton du projet
        this.revealProjectButton();
    },

    reserveProject : function(){
        var imagesCount = images.length;
        var testedProjectIndex = 0;
        var projectIsAvailable = false;

        do {
            testedProjectIndex = Math.floor(Math.random()*imagesCount);
            if(images[testedProjectIndex].active == false) {
                projectIsAvailable = true;
            }
        } while(projectIsAvailable == false); 

        // L'image est maintenant choisie alors on la rend indisponible
        images[testedProjectIndex].active = true;

        // On retourne l'index du projet choisi
        return testedProjectIndex;
    },

    createProjectButton : function(){
        this.projectButton = new Element('a',{'href':'#'});
        this.projectButton.setStyle('background-image', 'url(' + rootUrl + imagesThumbsUrl + imagesThumbsPrefix + images[this.projectIndex].filename + imagesExt +')');
        this.projectButton.addEvent('click', this.onProjectButtonClick.bind(this));
        this.projectButton.inject(this.cell);
    },

    onProjectButtonClick : function(event){
        event.stop(); // Permet d'�viter de se retrouver avec le # au bout de l'url et de revenir en haut de la page
        Slimbox.open(rootUrl + imagesUrl + images[this.projectIndex].filename + imagesExt, images[this.projectIndex].title);
    },

    revealProjectButton : function(){
        // D�claration de l'effet et attachement au bouton
        this.projectButtonFx = new Fx.Reveal(this.projectButton);

        // R�glage de l'effet
        this.projectButton.set('reveal', {
            mode: 'both',
            duration: Math.floor(Math.random()*2000)+1000,
            transition: 'bounce:out'
        });

        // D�clenchement de l'effet
        this.projectButton.reveal();
    },

    closeProject : function(){

        images[this.projectIndex].active = false;

        // Appelle l'effet dissolve puis d�truit l'�l�ment du DOM
        this.projectButton.nix(true);
    }
});
