//// AndaluciaGallery

var AndaluciaGallery = new Class({
    initialize: function(basecontent){
        this.imageGallery = null;
        this.imageController = null;
        this.imagesLogo = null;
        this.imagesCurrent = 0;

        this.videoGallery = null;
        this.videoController = null;
        this.videosLogo = null;
        this.videosCurrent = 0;

        this.audioGallery = null;
        this.audioController = null;
        this.audiosLogo = null;
        this.audiosCurrent = 0;

        this.Gallery = basecontent;

        this.currentGallery = 'imagesGallery';

        this.effectsOptions = { 
                duration: 300,
                transition: Fx.Transitions.Sine.easeInOut
        }

        this.videoGallery = this.Gallery.getElement('.screenShotVideos');
        this.imageGallery = this.Gallery.getElement('.screenShotImgs');
        this.audioGallery = this.Gallery.getElement('.screenShotAudios');
        if (this.videoGallery)
            this.Gallery.getElement('.toVideos').addEvent('click',this.changeToVideosGallery.bindWithEvent(this));
        if (this.audioGallery)
            this.Gallery.getElement('.toAudios').addEvent('click',this.changeToAudiosGallery.bindWithEvent(this));
        if (this.imageGallery)
            this.Gallery.getElement('.toImages').addEvent('click',this.changeToImagesGallery.bindWithEvent(this));
        if(this.imageGallery){
            this.imageController = this.Gallery.getElement('.imageController');
            this.loadImagesActions();
        }
        if(this.videoGallery){
            this.videoController = this.Gallery.getElement('.videoController');
            this.loadVideosActions();
        }
        if(this.audioGallery){
            this.audioController = this.Gallery.getElement('.audioController');
            this.loadAudiosActions();
        }
    },

    changeA2B: function(elementIn, elementOut){
            var efectoout = new Fx.Styles(elementOut, this.effectsOptions);
            var efectoin = new Fx.Styles(elementIn, this.effectsOptions);
            
            efectoout.start({
                            'opacity': [1,0]
            }).chain(function(){
                elementOut.setStyle('display','none');
 
                elementIn.setStyle('opacity',0);
                elementIn.setStyle('display','block');

                efectoin.start({
                    'opacity': [0,1]
                });
            });
    },

    fadeTo: function(gallery, controller){
        var galleryout;
        var controllerout;
        if(this.currentGallery == 'videosGallery') { galleryout=this.videoGallery; controllerout=this.videoController; }
        if(this.currentGallery == 'audiosGallery') { galleryout=this.audioGallery; controllerout=this.audioController; }
        if(this.currentGallery == 'imagesGallery') { galleryout=this.imageGallery; controllerout=this.imageController; }
        this.changeA2B(gallery, galleryout);
        this.changeA2B(controller, controllerout);
    },

    changeToAudiosGallery: function(){
        if(this.currentGallery != 'audiosGallery'){
            this.fadeTo(this.audioGallery, this.audioController);
            this.Gallery.getElement('.activeButton').removeClass('activeButton');
            this.Gallery.getElement('.toAudios').addClass('activeButton');
            this.currentGallery = 'audiosGallery';
        }
    },

    changeToVideosGallery: function(){
        if(this.currentGallery != 'videosGallery'){
            this.fadeTo(this.videoGallery, this.videoController);
            this.Gallery.getElement('.activeButton').removeClass('activeButton');
            this.Gallery.getElement('.toVideos').addClass('activeButton');
            this.currentGallery = 'videosGallery';
        }
    },

    changeToImagesGallery: function(){
        if(this.currentGallery != 'imagesGallery'){
            this.fadeTo(this.imageGallery, this.imageController);
            this.Gallery.getElement('.activeButton').removeClass('activeButton');
            this.Gallery.getElement('.toImages').addClass('activeButton');
            this.currentGallery = 'imagesGallery';
        }
    },


   nextImage: function(gallery, current){
        items = gallery.getElements('a');
        if (current < items.length-1){
            this.loadNewImage(gallery, items[current+1]);
            if (current < items.length-2){
                this.loadNewImage(gallery, items[current+2]);
            }
            this.changeA2B(items[current + 1], items[current]);
            current =  ++current;
        }
        return current;
    },


    loadNewImage: function(gallery, item){
        image = item.getElement('img');
        if (!image.getAttribute('src')){
            image_src = item.getAttribute('thumb');
            image.setAttribute('src', image_src);
        }
    },

    previousImage: function(gallery, current){
        items = gallery.getElements('a');
        if (current > 0){
            this.changeA2B(items[current - 1], items[current]); 
            current = --current;
        }
        return current
    },

    loadImagesActions: function(){
        this.imageController.getElement('.nextImg').addEvent('click',function(){
                            this.imagesCurrent = this.nextImage(this.imageGallery, this.imagesCurrent);
                            this.imageController.getElement('.imgNow').innerHTML = this.imagesCurrent + 1 ;
                        }.bindWithEvent(this));
        this.imageController.getElement('.prevImg').addEvent('click',function(){
                            this.imagesCurrent = this.previousImage(this.imageGallery, this.imagesCurrent);
                            this.imageController.getElement('.imgNow').innerHTML = this.imagesCurrent + 1 ;
                        }.bindWithEvent(this));
    },

    loadVideosActions: function(){
        this.videoController.getElement('.nextImg').addEvent('click',function(){
                            this.videosCurrent = this.nextImage(this.videoGallery, this.videosCurrent);
                            this.videoController.getElement('.imgNow').innerHTML = this.videosCurrent + 1 ;
                        }.bindWithEvent(this));
        this.videoController.getElement('.prevImg').addEvent('click',function(){
                            this.videosCurrent = this.previousImage(this.videoGallery, this.videosCurrent);
                            this.videoController.getElement('.imgNow').innerHTML = this.videosCurrent + 1 ;
                        }.bindWithEvent(this));
    },

    loadAudiosActions: function(){
        this.audioController.getElement('.nextImg').addEvent('click',function(){
                            this.audiosCurrent = this.nextImage(this.audioGallery, this.audiosCurrent);
                            this.audioController.getElement('.imgNow').innerHTML = this.audiosCurrent + 1 ;
                        }.bindWithEvent(this));
        this.audioController.getElement('.prevImg').addEvent('click',function(){
                            this.audiosCurrent = this.previousImage(this.audioGallery, this.audiosCurrent);
                            this.audioController.getElement('.imgNow').innerHTML = this.audiosCurrent + 1 ;
                        }.bindWithEvent(this));
    }


});
/* registerPloneFunction(loadGallery); */

