/*  Galeria de fotos slider com jQuery
    BUDI INTERNET
    http://internetbudi.com.br
    
    versao 1.2
    
    todo:
    http://www.impressivewebs.com/javascript-content-switcher-without-javascript/
    */
/*
-> GALERIA */
var BudiGaleria = {
    /*OK*/
    init: function () { //faz start da galeria
        if (this.images) {
            if (!this.timeSlider)
                this.setTime(5000, 700);
            
            //inicia as imagens
            this.initImages();
            //inicia as descriptions
            if (this.description)
                this.initDescriptions();
            //inicia navigation
            if (this.navigation)
                this.initNavigation();
            
            //mostra primeira imagem
            fncSliderInit = function() {
                //seleciona a inicial
                BudiGaleria.imageSelect(0, this.timeFade*0.7);
                //inicia slider
                $.doTimeout('galeriaSlider', BudiGaleria.timeSlider, BudiGaleria.navNextSlide); //todo: fazer para o timer uma metodo q acione o botao de navigation. como q se acionando no momento da mudança de timer.
            };
            if (this.imagesObj[0].complete) fncSliderInit();
            else $(this.imagesObj[0]).load(fncSliderInit);
        }
    },
    /*OK*/
    initImages: function() {
        this.imagesObj = $('img', this.imagesSelector);
        for (i=0; i < this.imagesObj.length; i++) {
            this.imagesObj[i].tabIndex = i;
            $(this.imagesObj[i]).hide();
        }
    },
    /*todo*/
    initDescriptions: function() {
        this.descriptionsObj = $('div', this.descriptionSelector);
        
        for (i=0; i < this.imagesObj.length; i++) {
            if (!descriptions[i])
                descriptions = $(this.descriptionSelector).append('<div class="desc-item"></div>').children('div');
        }
    },
    /*OK - falta paging*/
    initNavigation: function() {
        this.navigationObj = $(this.navigationSelector);
        var imageNumber, navIcon;
        
        switch (this.navigationType) {
        case 'simple':
            this.navigationPrevButton = $('img.prev-button', this.navigationSelector);
            this.navigationNextButton = $('img.next-button', this.navigationSelector);
            
            this.navigationPrevButton.click(this.navSimpleClick);
            this.navigationNextButton.click(this.navSimpleClick);
            break;
        case 'list':
            for (i=0; i < this.imagesObj.length; i++) {
                imageNumber = i+1;
                navIcon = $('<span>'+imageNumber+'</span>');
                navIcon.click(this.navClick);
                navIcon.hover(function(){this.navHover(this)}, function(){this.navHover(this)});
                
                this.navigationObj.append(navIcon);
            }
            break;
        }
    },
    /*todo*/
    setDescription: function(selector) { //configura descrições das fotos
        this.description = true;
        this.descriptionSelector = selector;
    },
    /*OK*/
    setNavigation: function(type, selector) { //configura navigation
        this.navigation = true;
        this.navigationSelector = selector;
        this.navigationType = type;
        
        /* types: simple, list, paging  */
    },
    /*OK*/
    setImages: function(selector) { //configura imagens
        this.images = true;
        this.imagesSelector = selector;
    },
    /*OK*/
    setTime: function(timeSlider, timeFade) { //configura tempos. recebe segundos e converte para milisegundos
        this.timeSlider = timeSlider*1000;
        this.timeFade = timeFade*1000;
    },
    /*OK +/-*/
    imageSelect: function(index, timeFade) {
        if (!timeFade)
            timeFade = this.timeFade;
        
        var selectedImage = $.grep(this.imagesObj, function(ft){
                                                        return ($(ft).css('display') != 'none');
                                                    });
        if (this.descriptions)
            var selectedDescription = $.grep(this.descriptionsObj, function(ds){
                                                                        return ($(ds).css('display') != 'none');
                                                                   });
        
        $(selectedImage).fadeOut(timeFade); //sai anteriores
        $(selectedImage).removeClass('selected');
        $(this.imagesObj[index]).fadeIn(timeFade); //entra seleção
        $(this.imagesObj[index]).addClass('selected');
        
        if (this.descriptions) {
            $(selectedDescription).fadeOut(timeFade);
            $(this.descriptionsObj[index]).fadeIn(timeFade);
        }
        
        if (this.navigation) {
            switch (this.navigationType) {
            case 'simple':
                //ativa botao de nav
                break;
            case 'list':
                var navIcons = $('span', this.navigationSelector);
                navIcons.delay( timeFade/2 ).removeClass(); //limpas class de navs
                $(navIcons[index]).delay( timeFade/2 ).removeClass().addClass('selected'); //adiciona class na selected
                
                break;
            }
        }
    },
    navListClick: function(evt) {
        $.doTimeout('galeriaSlider');
        if (! $(evt.currentTarget).hasClass('selected')) { //não selected atualmente
            BudiGaleria.imageSelect(evt.currentTarget.tabIndex, this.timeFade*0.5);
        }
    },
    navListHover: function(ico) {
        if (! $(ico).hasClass('selected'))
            $(ico).toggleClass('hover');
    },
    /*OK*/
    navSimpleClick: function(evt) {
        $.doTimeout('galeriaSlider');
        var button = $(evt.currentTarget);
        
        if ( button.hasClass('prev-button') ) //ANTERIOR
            BudiGaleria.navPrevSlide();
        else if ( button.hasClass('next-button') ) //PROXIMA
            BudiGaleria.navNextSlide();
    },
    /*OK*/
    navNextSlide: function() {
        //proxima imagem
        //todo: usar selector :eq para eliminar tabIndex
        var nxtImg = $('img.selected + img', BudiGaleria.imagesSelector);
        if (nxtImg.length == 0)
            nxtImg = $(BudiGaleria.imagesSelector).children().first();
        
        BudiGaleria.imageSelect(nxtImg[0].tabIndex);
        return true; //return para continuar o timer em caso de timeSlider
    },
    /*ok*/
    navPrevSlide: function() {
        //imagem anterior
        var prvImg = $('img.selected', BudiGaleria.imagesSelector).prev();
        if (prvImg.length == 0)
            prvImg = $(BudiGaleria.imagesSelector).children().last();
        
        BudiGaleria.imageSelect(prvImg[0].tabIndex);
        return true;
    }
}
