/* anImage : animateur d'images, version 1.0
* (c) 2007 Damien Ravé (Le Caphar)
* anImage peut être librement copié ou modifié
* http://www.lepotlatch.org
*/

// Classe animage
var animage = function(elementId,images,delai,actif) {
    this.elementId = elementId;
    this.indexActuel = 0;
    this.images = images;
    this.delai = delai;

    // Précharge les images
    this.prechargement = function() {
        image = new Array();
        for (i= 0; i <= this.images.length -1 ; i++) {
            image[i] = new Image(100,25);
            image[i].src= this.images[i];
        }
    }

    // Configure l'image de base et l'événement onClick
    this.imageDepart = function() {
        var self = this;
        if (document.getElementById(this.elementId)) {
            document.getElementById(this.elementId).src = this.images[this.indexActuel];

            document.getElementById(this.elementId).onclick = function() {
                //animage.images[self.imgNo].lanceur();
                self.lanceur();
            }

        } else {
            setTimeout(function() {
                self.imageDepart()
            },50);
        }
    }
    // Changer l'image
    this.changeImage = function() {
        if (document.getElementById(this.elementId) && this.images.length - 1 > 0) {
            this.indexActuel = (this.indexActuel + 1) % (this.images.length);
            if (this.images[this.indexActuel] != undefined) {
                document.getElementById(this.elementId).src = this.images[this.indexActuel];
            }
        }
    }

    // Démarre ou arrête la boucle
    this.lanceur = function() {
        if (this.actif) {
            this.actif = 0;
            clearTimeout(this.timer);
        } else {
            this.actif = 1;
            var self = this;
            this.timer = setInterval(function() {
                self.changeImage()
                },this.delai);
        }
    }

    // Initialisation
    this.prechargement();
    this.imageDepart();
    if (actif === undefined || actif) {
        this.lanceur();
    }
}
