/**
* @file ring.js
* @author Thierry MALO
* @date March 17, 2008
* @last-modification March 17, 2008
* @licence http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt CeCILL v 2.0
*/

/**
Class: Ring
    Montre des applications autour d'un anneau. L'anneau tourne dans le sens des aiguilles d'une montre ou dans le sens inverse.

Note:
    L'Anneau peut exiger une doctype XHTML.

Arguments:
    objs - collection d'objet pour faire l'anneau.
    traj - liste de position (top, left), size (width, height) and z-index.
            Devrait être produit par mvalues.py
    options - un Objet, voir les options ci-dessous.

Options:
    moveDelai - Delai entre le mouvement en millisecondes. Par défaut 50.
    wFactor - Facteur de multiplication pour la largeur.
    hFactor - Facteur de multiplication pour la longueur.

Evénements:
    onMove - Fonction exécutée avant le déplacement des applications.
    onMoveEnd - Fonction exécutée après le déplacement des applications.
    ItemMoved - Fonction exécutée après chaque déplacement d'une application. Par défaut elle change la taille des images d'en dessous
 
Propriétés:
    moving  - 1 si l'anneau tourne actuellement à gauche (dans le sens des aiguilles d'une montre)
            -1 si l'anneau tourne actuellement à droite (en sens inverse des aiguilles d'une montre) 
            -0 autrement
*/

var Ring = new Class ({

    options:{
        moveDelai:50,
        wFactor:1.0,
        hFactor:1.0,
        onMove: Class.empty,
        onMoveEnd: Class.empty,
        onItemMoved: function(el,position){
            var img= el.getElements('img');
            
            var w=el.getStyle('width');
            var h=el.getStyle('height');

            img.each(function(obj){
                obj.setProperty('width',w);
                obj.setProperty('height',h);
            });
        
        }
    },
    
    initialize: function(objs,traj,options) {
        this.setOptions(options);
        this.datas=traj;
        this.objToMove = objs;
        this.idx =0;
        this.step = 0;
        this.delai = 
        this.step = Math.round((this.datas.length / this.objToMove.length) );
    },
    
    setPosition: function(i){
        var idx = i % this.datas.length;

        this.idx=idx;	
        var me=this;
        
        var wFactor = me.options.wFactor;
        var hFactor = me.options.hFactor;
        
        this.fireEvent('onMove', this.idx);
        
        this.objToMove.each(function(el){
            var donnee = me.datas[idx];

            el.setStyles(donnee);
            
            if (wFactor != 1.0) {
                el.setStyle('width',Math.round(donnee.width*wFactor));
            };
            if (hFactor != 1.0) {
                el.setStyle('height',Math.round(donnee.height*hFactor));
            };
            
            me.fireEvent('onItemMoved',[el,idx]);
            
            idx=(idx + me.step) % me.datas.length;

        });
        this.fireEvent('onMoveEnd');
    },
                
    _moveLeft:function(){
        var i = (this.datas.length +this.idx - 1) % this.datas.length;
        this.setPosition(i);
    },

    moveLeft:function(){
        if (this.moving==-1) return;

        this.moving=-1;
        var me=this;
        this.ltimer=me._moveLeft.periodical(me.options.moveDelai,me);
    },
    
    _moveRight: function(){
        var i = (this.idx + 1) %this.datas.length;
        this.setPosition(i);
    },
        
    moveRight:function(){
        if (this.moving==1) return;
        
        this.moving=1;
        var me=this;
        this.ltimer=me._moveRight.periodical(me.options.moveDelai,me);
    },
    
    stopMove: function(){
        $clear(this.ltimer);
        $clear(this.ftimer);
        this.moving=0;
    }

});

Ring.implement(new Events,new Options);

