﻿var iIndex = 0;
var stInterval;
var sAtivo = true;

$(document).ready(function () {

    $('a[id*=lnk_]').each(function (intIdex) {
        $(this).bind("click", function () {
            slideSwitch(intIdex);
        });

    });

    $('#lkLeft').click(function () {
        slideSwitch('L');

    });

    $('#lkRight').click(function () {
        slideSwitch('R');

    });

    $('#lnkPlay').click(function () {
        $(this).css('background-color', "#92D4AE"); //#1EA058
        $('#lnkPause').css('background-color', "#1EA058");
        sAtivo = true;
        rstInterval("R");
    });

    $('#lnkPause').click(function () {
        sAtivo = false;
        clearInterval(stInterval);
        $(this).css('background-color', "#92D4AE");
        $('#lnkPlay').css('background-color', "#1EA058");
    });

    hoverButton(1);
    rstInterval("R");

});

function rstInterval(sGo) {    
    clearInterval(stInterval)
    stInterval = setInterval("slideSwitch('" + sGo + "')", 6000);
}


function slideSwitch(sVal) {
    if (sAtivo) rstInterval("R");
    var $active = $('#painelPlay img.active');
    var $next;
    $active.addClass('last-active');

    if (sVal == "R") {
        $next = $active.next().length ? $active.next() : $('#painelPlay img:eq(0)');
    } else if (sVal == "L") {
        $next = $active.prev().length ? $active.prev() : $('#painelPlay img:last');
    } else {
        $next = $('#painelPlay img:eq(' + sVal + ')');
    }

    trol($next, $active);
}


function trol(objOn, objOff) {

    hoverButton(objOn.attr('val'));

    objOn.css({ opacity: 0.0 })
                .addClass('active')
                .animate({ opacity: 1.0 }, 1000, function () {
                    objOff.removeClass('active last-active');
                });


            }

function CallOferta(iDestino, sParam) {
    if (iDestino == 1) window.location.href = 'Estoque.aspx?txtBusca=' + sParam;
    if (iDestino == 2) window.location.href = 'Acessorios.aspx?txtBuscaAcessorio=' + sParam;
    if (iDestino == 3) veiShow(sParam);

}

function hoverButton(sVal) {
    $('a[id*=lnk_]').each(function () {
        $(this).removeClass("btHovOn").addClass("btHovOff");
    });

    $('#lnk_' + sVal).removeClass("btHovOff").addClass("btHovOn");

    $(JsonOferta).each(function () {
        if (this.Cod == sVal) {
            $('img[id*=imgPlay]').attr('src', 'img_ofertas/' + this.foto);
            $('#painelMensagem #Titulo').html(this.titulo);
            $('#painelMensagem #SubTitulo').html(this.subTitulo);
        }
    });
}

