﻿/* =====================================================================================
CÓDIGO RESPONSÁVEL PELO EFEITO DESLIZANTE DE ELEMENTOS HTML (Gaia.UI.Effects.Slider)
PROGRAMADOR: RANDOLF LOUREIRO COSTA BRITO
DATA: 20/03/2009
ÚLTIMA REVISÃO: 24/03/2009 [RANDOLF LOUREIRO COSTA BRITO]
======================================================================================== */

/* 
Functions:
- __Count()  = Conta quantos elementos existem para serem movimentados.
- __Effect() = Função que realiza o efeito slide no elemento. Ex:(elemento sendo uma <div>).
-------------------------------------------------------------
- Next()     = Função que movimenta o elemento para direita.
- Previous() = Função que movimenta o elemento para esquerda.
OBS: Ambas possuem o parâmetro "stDelay", 0 para quando o usuário clicar no botão e interrompero delay e 1 para quando carregar a página ativar o delay. 
-------------------------------------------------------------
- Delay()    = Função que realiza em um tempo X o efeito slide.
- Init()     = Função que inicializa o efeito slide com o delay.
*/

var slider_stop = "";
var slider_continue = "";

function Slider(id) {
    this.id = id;
    this.__container = document.getElementById(id + "_container"); //Guarda o id do container que realizará o efeito.
    this.__previous = document.getElementById(id + "_previous"); //Guarda o id do botão anterior.
    this.__pause = document.getElementById(id + "_pause"); //Guarda o id do botão pause.
    this.__next = document.getElementById(id + "_next"); //Guarda o id do botão próximo.
    this.__containerLength = 0; //Guarda o total de sub elementos dentro do container principal.
    this.__activeElement = 0; //Conta cada elemento para saber se esta no início ou no fim dos mesmos.
    this.__activeSense = 0; //Variável que auxilía quando o efeito slide deve ir para direita ou esquerda. 0 = next, 1 = previous.
    this.__intervalEffect = null; //Guarda o intervalo para o efeito do elemento.
    this.__intervalDelay = null; //Guarda o intervalo para o efeito ser realizado pela função Delay.
    this.DelayTime = 0; //Define o tempo em que a função Delay funcionará, valor da variável é definida no geral.xml na tabela uc_Licitação.

    this.__Count = function() {
        var totalElements = 0;

        for (i = 0; i < this.__container.childNodes.length; i++) {
            if ((this.__container.childNodes.item(i).nodeName == "DIV") || (this.__container.childNodes.item(i).nodeName == "LI"))
                totalElements++;
        }

        return totalElements;
    }

    /* Popula a variável containerLengh com a quantidade de elementos existes para o efeito slide. Ex: 3 div´s para movimentar. */
    this.__containerLength = this.__Count();

    this.__Effect = function(sense) {
        if (sense == 1) {
            /* Se for o último movimento do slide a direita, entrará nesta condição. */
            if (((parseInt(this.__container.style.left) - 10) < (-1 * (this.__activeElement + 1) * (parseInt(this.__container.style.width) / this.__containerLength)))
             ||
             (((parseInt(this.__container.style.left) - 10) < (-1 * (this.__activeElement) * (parseInt(this.__container.style.width) / this.__containerLength))) && this.__activeElement == this.__containerLength - 1)) {

                if ((((parseInt(this.__container.style.left) - 10) < (-1 * (this.__activeElement) * (parseInt(this.__container.style.width) / this.__containerLength))) && this.__activeElement == this.__containerLength - 1)) {
                    this.__container.style.left = (-1 * (this.__activeElement) * (parseInt(this.__container.style.width) / this.__containerLength)) + "px";
                    clearInterval(this.__intervalEffect);
                }
                else {
                    this.__container.style.left = (-1 * (this.__activeElement + 1) * (parseInt(this.__container.style.width) / this.__containerLength)) + "px";
                    clearInterval(this.__intervalEffect);
                    this.__activeElement++;
                }

                /* Habilita os botões quando finaliza o efeito slide. */
                if (this.__next.disabled == true) {
                    this.__next.disabled = false;
                    this.__pause.disabled = false;
                    this.__previous.disabled = false;
                    this.__next.className = "bt_proxima";
                    this.__previous.className = "bt_anterior";
                }

                /* Se for o último elemento a direita desabilitará o botão próximo. */
                if (this.__activeElement == (this.__containerLength - 1)) {
                    this.__next.className = "bt_proxima_inativo";
                    this.__next.disabled = true;
                    this.__activeSense = 1;
                }
            }
            else/* Se for o ínicio do efeito slide entrará aqui. */
            {
                if (parseInt(this.__container.style.left) > (-1 * (this.__activeElement + 1) * (parseInt(this.__container.style.width) / this.__containerLength))) {

                    if ((parseInt(this.__container.style.left) - 10) >= (-1 * (parseInt(this.__container.style.width) / this.__containerLength) * (this.__containerLength - 1)))
                        this.__container.style.left = (parseInt(this.__container.style.left) - 10) + "px";

                    /* Desabilita os botões enquanto ocorre o efeito slide. */
                    this.__next.disabled = true;
                    this.__pause.disabled = true;
                    this.__previous.disabled = true;
                }
                else {
                    clearInterval(this.__intervalEffect);
                    this.__activeElement++;
                }
            }
        }
        else {
            /* Se for o último movimento do slide a esquerda, entrará nesta condição. */
            if ((parseInt(this.__container.style.left) + 10) > (-1 * (this.__activeElement - 1) * (parseInt(this.__container.style.width) / this.__containerLength))
            ||
            (((parseInt(this.__container.style.left) + 10) > (-1 * (this.__activeElement) * (parseInt(this.__container.style.width) / this.__containerLength))) && this.__activeElement == 0)) {

                if (((parseInt(this.__container.style.left) + 10) > (-1 * (this.__activeElement) * (parseInt(this.__container.style.width) / this.__containerLength))) && this.__activeElement == 0) {
                    this.__container.style.left = (-1 * (this.__activeElement) * (parseInt(this.__container.style.width) / this.__containerLength)) + "px";
                    clearInterval(this.__intervalEffect);
                }
                else {
                    this.__container.style.left = (-1 * (this.__activeElement - 1) * (parseInt(this.__container.style.width) / this.__containerLength)) + "px";
                    clearInterval(this.__intervalEffect);
                    this.__activeElement--;
                }

                /* Habilita os botões quando finaliza o efeito slide. */
                if (this.__next.disabled == true) {
                    this.__next.disabled = false;
                    this.__pause.disabled = false;
                    this.__previous.disabled = false;
                    this.__next.className = "bt_proxima";
                    this.__previous.className = "bt_anterior";
                }

                /* Se for o primeiro elemento a esquerda desabilitará o botão anterior. */
                if (this.__activeElement == 0) {
                    this.__previous.className = "bt_anterior_inativo";
                    this.__previous.disabled = true;
                    this.__activeSense = 0;
                }
            }
            else/* Se for o ínicio do efeito slide entrará aqui. */
            {
                if (parseInt(this.__container.style.left) < (-1 * (this.__activeElement - 1) * (parseInt(this.__container.style.width) / this.__containerLength))) {

                    if ((parseInt(this.__container.style.left) + 10) <= 0)
                        this.__container.style.left = (parseInt(this.__container.style.left) + 10) + "px";

                    /* Desabilita os botões enquanto ocorre o efeito slide. */
                    this.__next.disabled = true;
                    this.__pause.disabled = true;
                    this.__previous.disabled = true;
                }
                else {
                    clearInterval(this.__intervalEffect);
                    this.__activeElement--;
                }
            }
        }
    }

    this.Next = function(stDelay) {
        var obj = this;
        if (stDelay == 0) {
            clearInterval(this.__intervalDelay);
            this.__pause.className = "bt_play";
            this.__pause.value = ">";
            this.__pause.title = "Play";
        }

        obj.__intervalEffect = setInterval(id + "_slider.__Effect(1)", 20);
    }

    this.Previous = function(stDelay) {
        var obj = this;
        if (stDelay == 0) {
            clearInterval(this.__intervalDelay);
            this.__pause.className = "bt_play";
            this.__pause.value = ">";
            this.__pause.title = "Play";
        }

        obj.__intervalEffect = setInterval(id + "_slider.__Effect(2)", 20);
    }

    this.Delay = function() {
        if (this.__activeSense == 0) {
            if (this.__activeElement < this.__containerLength - 1)
                eval(id + "_slider.Next('1')");
        }
        else {
            if (this.__activeElement > 0)
                eval(id + "_slider.Previous('1')");
        }
    }

    this.Init = function() {
        var obj = this;

        /*Tratar problema do efeito slide nos navegadores Chrome e Firefox, quando a aba estiver inativa: TT398 */
        if (!document.all) {
            if (slider_stop.indexOf(id + "_slider.__intervalDelay") == -1)
                slider_stop += " clearInterval(" + id + "_slider.__intervalDelay); ";

            window.onblur = (function(conteudo_stop) { return function() { eval(conteudo_stop); }; })(slider_stop);

            if (slider_continue.indexOf(id + "_slider.Init()") == -1)
                slider_continue += " if (" + id + "_slider.__intervalDelay != null && " + id + "_slider.__intervalEffect != null) " + id + "_slider.Init(); ";

            window.onfocus = (function(conteudo_continue) { return function() { eval(conteudo_continue); }; })(slider_continue);
        }

        /* Caso exista somente um elemento dentro do container, os botões anterior, próximo e pause não apareceram.  */
        if (this.__containerLength <= 1) {
            this.Pause();
            this.__previous.style.display = "none";
            this.__pause.style.display = "none";
            this.__next.style.display = "none";
        }
        else {
            if (this.DelayTime == 0)
                this.Pause();
            else
                obj.__intervalDelay = setInterval(id + "_slider.Delay()", 1000 * obj.DelayTime);

            if (this.__activeElement == 0)
                this.__previous.className = "bt_anterior_inativo";

            this.__previous.disabled = true;
        }
    }

    this.Pause = function() {
        if (this.__pause.className == "bt_pause") {
            this.__pause.className = "bt_play";
            this.__pause.value = ">";
            this.__pause.title = "Play";
            clearInterval(this.__intervalDelay);
        }
        else {
            this.__pause.className = "bt_pause";
            this.__pause.value = "II";
            this.__pause.title = "Pause";
            this.Init();

            if (this.__activeElement > 0)
                this.__previous.disabled = false;
        }

        if (this.DelayTime == 0)
            this.DelayTime = 3;
    }
}

