/**
 * Classe permettant la mise en place d'une galerie d'images
 * @author Erase
 * @version 1.0.0
 * @commentaire Base pas dégueu mais patchs ajoutés à revoir
 * afin d'améliorer leur intégration dans le système
 *
 */
TerraGalery = Class.create();
TerraGalery.prototype = {
    initialize: function(){
        var defaults = {
            wait: 2500,
            idContentGd: 'slide'
        };
        var options = Object.extend(defaults, arguments[0] || {});
        if (options.wait && Object.isNumber(options.wait)) 
            this.wait = options.wait;
        if (options.idContentGd && Object.isString(options.idContentGd)) 
            this.idContentGd = options.idContentGd;
        
        this.icr = 0;
        this.NumberImages = 0;
        this.bol = true;
        this.aImgSlide = [];
        this.play;
        this.go = 'false';
        
        this.InitGalery();
    },
    
    InitGalery: function(){
        var elem = $(this.idContentGd);
        this.aImgSlide = elem.getElementsByTagName('img');
        this.NumberImages = this.aImgSlide.length;
        
        new Insertion.Bottom(elem, '<div id="ss_barre" style="background:none repeat scroll 0 0 rgba(0, 0, 0, 0.8);width:998px;height:55px;z-index:100;position:absolute;bottom:0px;"></div>');
        new Insertion.Bottom('ss_barre', '<div style="z-index:101" id="puces_' + this.idContentGd + '"></div>');
        new Insertion.Bottom('ss_barre', '<div style="width:950px;z-index:100;padding:20px 20px 20px 0;text-align:right;font-size:18px;font-weight:bold;color:#fff;" id="ss_title"></div>');
        for (var i = 0; i < this.NumberImages; i++) {
            this.aImgSlide[i].style.display = 'none';
            this.aImgSlide[i].style.zIndex = '50';
            this.InsertPuce(i, this.aImgSlide[i].getAttribute('src'));
        }
        this.aImgSlide[0].style.display = 'block';
        this.AffichePuce(0);
        if (this.aImgSlide[0].getAttribute('title')) {
            this.AfficheTitre(this.aImgSlide[0].getAttribute('title'));
        }
        else 
            if (this.aImgSlide[0].getAttribute('alt')) {
                this.AfficheTitre(this.aImgSlide[0].getAttribute('alt'));
            }
        var t = this;
        this.play = setInterval(function(){
            t.PlayGalery();
        }, this.wait);
    },
    
    EventMouseClick: function(e, elem){
        var elems = elem.id.split('_');
        this.AffichePuce(elems[1]);
        this.AfficheThis(elems[1]);
    },
    
    RazPuces: function(y){
        var elems = $$('img.puce_this');
        for (var i = 0, iM = elems.length; i < iM; i++) {
            elems[i].className = 'puce';
        }
    },
    
    AfficheTitre: function(titre){
        if ($('ss_title')) {
            $('ss_title').innerHTML = titre;
        }
    },
    
    AffichePuce: function(y){
        this.RazPuces();
        if ($('puce_' + y + '_' + this.idContentGd)) {
            $('puce_' + y + '_' + this.idContentGd).className = 'puce_this';
        }
    },
    
    InsertPuce: function(num, bck){
        var t = this;
        var m = parseInt(num * 65);
        new Insertion.Bottom("puces_" + this.idContentGd, "<img src='" + bck + "' class='puce' id='puce_" + num + "_" + this.idContentGd + "' width='60' height='45' style='position:absolute;margin-left:" + m + "px;cursor:pointer;' />")
        $('puce_' + num + '_' + this.idContentGd).observe('click', this.EventMouseClick.bindAsEventListener(this, $('puce_' + num + '_' + this.idContentGd)));
    },
    
    StopGalery: function(){
        if (this.play) {
            clearInterval(this.play);
        }
    },
    
    PlayGalery: function(){
        var imageShow, imageHide;
        if (this.go != 'false') {
            if (this.go - 1 < 0) {
                this.icr = this.NumberImages - 1;
            }
            else {
                this.icr = this.go - 1;
            }
            this.go = 'false';
        }
        imageShow = this.icr + 1;
        imageHide = this.icr;
        
        if (imageShow == this.NumberImages) {
            this.ChangePics(0, imageHide);
            this.icr = 0;
        }
        else {
            this.ChangePics(imageShow, imageHide);
            this.icr++;
        }
    },
    
    ChangePics: function(x, y){
        if (this.aImgSlide[x] && this.aImgSlide[y]) {
            this.aImgSlide[x].style.zIndex = '50';
            this.aImgSlide[x].style.display = 'block';
            if (this.aImgSlide[x].getAttribute('title')) {
                this.AfficheTitre(this.aImgSlide[x].getAttribute('title'));
            }
            else 
                if (this.aImgSlide[x].getAttribute('alt')) {
                    this.AfficheTitre(this.aImgSlide[x].getAttribute('alt'));
                }
            this.AffichePuce(x);
            this.SwitchEffect(y);
        }
    },
    
    AfficheThis: function(y){
        if (this.bol == true) {
            for (var i = 0; i < this.NumberImages; i++) {
                if (this.aImgSlide[i]) {
                    this.aImgSlide[i].style.display = 'none';
                    this.aImgSlide[i].style.zIndex = '50';
                }
            }
            this.StopGalery();
            if (this.aImgSlide[y]) {
                this.aImgSlide[y].style.display = 'block';
                this.aImgSlide[y].style.zIndex = '50';
                if (this.aImgSlide[y].getAttribute('title')) {
                    this.AfficheTitre(this.aImgSlide[y].getAttribute('title'));
                }
                else 
                    if (this.aImgSlide[y].getAttribute('alt')) {
                        this.AfficheTitre(this.aImgSlide[y].getAttribute('alt'));
                    }
            }
            
            this.go = y;
            var t = this;
            this.play = setInterval(function(){
                t.PlayGalery();
            }, this.wait);
            
        }
        else {
            this.AfficheThis(y);
        }
    },
    
    SwitchEffect: function(y){
        var elemPict = this.aImgSlide[y];
        new Effect.Fade(elemPict, {
            duration: 1,
            beforeStart: function(){
                this.bol = false;
                elemPict.style.zIndex = '99';
            },
            afterFinish: function(){
                this.bol = true
            }
        });
    }
}


