var LeoNet = window.LeoNet || {};
LeoNet.destaquesRot = {
    painel: null,
    itens: null,
    botoes: null,
    botoesTxts: {
        next: "proximo",
        prev: "anterior",
        stop: "parar",
        play: "executar"
    },
    botoesRefClickLEO: "",
    ativado: 0,
    anterior: 0,
    proximo: 0,
    navMSIE: !!navigator.userAgent.match(/MSIE/g),
    timeOut: null,
    timer: 10,
    animando: false,
    ft: document.createElement("div"),
    showft: false,
    contaTrocas: 0,
    init: function (a) {
        this.painel = document.getElementById(a.id);
        this.timer = a.timer || this.timer;
        this.botoesRefClickLEO = a.botoesRefClickLEO || this.botoesRefClickLEO;
        this.botoesTxts = a.botoesTxts || this.botoesTxts;
        if (typeof mostraft == "undefined") {
            mostraft = 0
        }
        this.showft = showft = (document.cookie.indexOf("destaquesRotNoft=1") == -1) ? true : false;
        this.itens = this.painel.getElementsByTagName("li");
        this.setNavegacao(this.ativado);
        this.aplicarSombras();
        if (this.itens.length == 1) {
            a.startOff = true
        } else {
            this.montarBotoes()
        }
        if (document.cookie.indexOf("destaquesRotStop=1") != -1) {
            a.startOff = true
        }
        if (!a.startOff) {
            this.stopPlay()
        } else {
            this.setBtStopPlay("play")
        }
    },
    events: {
        preventDefault: function (a) {
            if (a.preventDefault) {
                a.preventDefault()
            } else {
                a.returnValue = false
            }
        },
        add: function (c, b, a) {
            if (document.attachEvent) {
                c.attachEvent("on" + b, a)
            } else {
                if (document.addEventListener) {
                    c.addEventListener(b, a, true)
                }
            }
        }
    },
    stopPlay: function (a) {
        if ((!a && this.timeOut == null) || (a && this.timeOut != null)) {
            clearInterval(this.timeOut);
            this.timeOut = setInterval(function () {
                LeoNet.destaquesRot.navegar(1)
            },
            LeoNet.destaquesRot.timer * 1000);
            document.cookie = "destaquesRotStop=0; path=/";
            this.setBtStopPlay("stop")
        } else {
            document.cookie = "destaquesRotStop=1; path=/";
            clearInterval(this.timeOut);
            this.timeOut = null;
            this.setBtStopPlay("play")
        }
    },
    setBtStopPlay: function (a) {
        if (this.botoes != null) {
            var b = this.botoes.getElementsByTagName("a")[1];
            b.className = "bt" + a;
            b.innerHTML = this.botoesTxts[a]
        }
    },
    setNavegacao: function (a) {
        this.ativado = (parseInt(a) + (this.ativado || this.itens.length)) % this.itens.length;
        this.anterior = ((this.ativado || this.itens.length) - 1) % this.itens.length;
        this.proximo = ((this.ativado || this.itens.length) + 1) % this.itens.length
    },
    fadeIO: function (b, d, c) {
        if (d) {
            b.style.opacity = 0;
            b.style.filter = "Alpha(Opacity=0)"
        } else {
            b.style.opacity = 1;
            b.style.filter = "Alpha(Opacity=100)"
        }
        LeoNet.destaquesRot.animando = true;
        for (var a = 1; a <= 10; a++) {
            setTimeout(function (e) {
                return function () {
                    if (LeoNet.destaquesRot.navMSIE) {
                        b.style.filter = "Alpha(Opacity=" + (d ? 10 * e : 100 - 10 * e) + ")"
                    } else {
                        b.style.opacity = (d ? 0.1 * e : 1 - 0.1 * e)
                    }
                    if (e == 10) {
                        LeoNet.destaquesRot.animando = false;
                        if (typeof c == "function") {
                            c()
                        }
                    }
                }
            } (a), 75 * a)
        }
    },
    fecharft: function () {
        document.cookie = "destaquesRotNoft=1; path=/";
        this.showft = false;
        clearTimeout(LeoNet.destaquesRot.ftFadeOutTimer);
        this.ftFadeOut()
    },
    ftFadeOut: function () {
        this.fadeIO(this.ft, false, function () {
            LeoNet.destaquesRot.ft.style.display = "none";
            LeoNet.destaquesRot.botoes.style.display = "block"
        });
        this.fadeIO(this.botoes, true, null)
    },
    navegar: function (b) {
        var a = this.ativado;
        this.removerft = false;
        this.contaTrocas++;
        if (mostraft == 1 && this.showft && this.contaTrocas > 0 && this.contaTrocas % this.itens.length == 0) {
            this.ft.style.display = "block";
            this.ft.className = "ft";
            //this.ft.innerHTML = '<a href="' + mostraftLINK + '" target="_blank"><img src="' + mostraftIMG + '" border="0" /></a><div class="rodape"><a href="javascript:void(0)" onclick="LeoNet.destaquesRot.fecharft()"><img src="http://home.img.LEO.com.br/h3/ft-fechar.gif" border="0" /></div>';
            this.painel.appendChild(this.ft);
            this.fadeIO(this.ft, true, function () {
                LeoNet.destaquesRot.botoes.style.display = "none"
            });
            this.fadeIO(this.botoes, false, null);
            if (this.timeOut != null) {
                LeoNet.destaquesRot.ftFadeOutTimer = setTimeout(function () {
                    LeoNet.destaquesRot.ftFadeOut()
                },
                LeoNet.destaquesRot.timer * 1000)
            }
        } else {
            this.setNavegacao(b);
            this.itens[this.ativado].className = this.itens[this.ativado].className.replace(/desativado/g, "");
            this.itens[this.ativado].style.zIndex = 3;
            this.itens[a].style.zIndex = 4;
            this.itens[this.ativado].style.opacity = 1;
            this.itens[this.ativado].style.filter = "none";
            this.fadeIO(this.itens[a], false, function () {
                if (LeoNet.destaquesRot.navMSIE) {
                    LeoNet.destaquesRot.itens[a].style.filter = LeoNet.destaquesRot.itens[a].style.filter = "none"
                }
                LeoNet.destaquesRot.itens[a].className = "desativado";
                LeoNet.destaquesRot.itens[a].style.zIndex = 2
            });
            this.writeBotoesHTML();
            if (this.timeOut == null) {
                this.setBtStopPlay("play")
            } else {
                this.setBtStopPlay("stop")
            }
        }
    },
    montarBotoes: function () {
        this.botoes = document.createElement("div");
        this.botoes.className = "botoesNavegacao";
        this.writeBotoesHTML();
        this.painel.appendChild(this.botoes)
    },
    aplicarSombras: function () {
        var b = document.createElement("div");
        b.className = "sombra";
        for (var a = 0; a < this.itens.length; a++) {
            if (a > 0) {
                this.itens[a].className += " desativado"
            }
            this.itens[a].appendChild(b.cloneNode(true))
        }
    },
    writeBotoesHTML: function () {
        this.botoes.innerHTML = "<a rel='-1' href='" + this.itens[this.anterior].getElementsByTagName("a")[0].href.replace(/(^.*?click\.LEO.*?rf\=).*?(&u\=.*?)$/g, "$1" + this.botoesRefClickLEO + "$2") + "' class='anterior' name='" + this.botoesRefClickLEO + "'>" + this.botoesTxts.prev + "</a>      <a rel='0' href='" + this.itens[this.ativado].getElementsByTagName("a")[0].href.replace(/(^.*?click\.LEO.*?rf\=).*?(&u\=.*?)$/g, "$1" + this.botoesRefClickLEO + "$2") + "' class='' name='" + this.botoesRefClickLEO + "'></a>      <a rel='+1' href='" + this.itens[this.proximo].getElementsByTagName("a")[0].href.replace(/(^.*?click\.LEO.*?rf\=).*?(&u\=.*?)$/g, "$1" + this.botoesRefClickLEO + "$2") + "' class='proximo' name='" + this.botoesRefClickLEO + "'>" + this.botoesTxts.next + "</a>";
        var b = this.botoes.getElementsByTagName("a");
        for (var a = 0; a < b.length; a++) {
            this.events.add(b[a], "click", function (c) {
                LeoNet.destaquesRot.btNavegacaoClick(c)
            })
        }
    },
    btNavegacaoClick: function (a) {
        LeoNet.destaquesRot.events.preventDefault(a);
        if (a.srcElement && !a.target) {
            a.target = a.srcElement
        }
        if (a.target.rel == 0) {
            LeoNet.destaquesRot.stopPlay()
        } else {
            if (LeoNet.destaquesRot.animando == false) {
                setTimeout(function () {
                    LeoNet.destaquesRot.navegar(a.target.rel);
                    LeoNet.destaquesRot.stopPlay(true)
                },
                30)
            }
        }
    }
};

