$(function () {


    /***** MENU LATERAL ******/


    $('.menuLateral').click(function (e) {
        e.stopPropagation();
        var href = $(this).children('div').find('a').attr("href");

        if (href) {
            window.location = href;
        }
    });
    $('.menuLateral').mouseenter(function (index) {
        $(this).removeClass("menuLateral_bg").addClass("menuLateral_bg_on");
        //$(this).children('div').find('a').css("color", "#cc0000");
    }).mouseleave(function () {
        $(this).removeClass("menuLateral_bg_on").addClass("menuLateral_bg");
        //$(this).children('div').find('a').css("color", "#2188aa");
    });



    /***** FIM MENU LATERAL ******/










    /***** CONTROLADOR NUMÉRICO ******/

    var nValores = $('#DestaqueFotos div').length;    

    if (nValores > 1) {
        $('#DestaqueFotos div').each(function (index) {
            var contador = $('#DestaqueFotos div').index(this) + 1;

            if ($('#DestaqueFotos div').index(this) == 0)
                $('<li class="activo">' + contador + '</li>').appendTo('#controlador');
            else {
                $('<li>' + contador + '</li>').appendTo('#controlador');
            }
        });
    }
    else {
        $('#setaAnterior img').css("display", "none");
        $('#setaSeguinte img').css("display", "none");
    }


    $('#controlador li').click(function (index) {

        var cell = $('div[class^="show"]')
        var indexActivo = cell.parent("div").children().index(cell);
        if (indexActivo != $('#controlador li').index(this)) {
            //alert($('#controlador li').index(this) + 1);
            window.clearInterval(intval);
            //var current = ($('#DestaqueFotos div.show') ? $('#DestaqueFotos div.show') : $('#DestaqueFotos div:first'));
            //if ( current.length == 0 ) current = $('#DestaqueFotos div:first');
            var current = $('#DestaqueFotos div.show');
            var next = $(nValores[$('#controlador li').index(this)]);
            efeitos(next, current);
        }
    });
    /***** FIM CONTROLADOR NUMÉRICO ******/


    /***** CONTROLADOR NUMÉRICO ******/
    $('#setaAnterior img').click(function () {
        rotate('anterior');
        //alert('kjl');
        window.clearInterval(intval);
        intval = window.setInterval('rotate("anterior")', 6000)
    });

    $('#setaSeguinte img').click(function () {
        rotate('seguinte');
        window.clearInterval(intval);
        intval = window.setInterval('rotate("seguinte")', 6000)

    });
    /***** CONTROLADOR SETAS ******/

    //Inicia o banner
    $('#DestaqueFotos').css('display', 'block');
    theRotator();



    var posicaoDireita = 383 - $('#controlador').width() - 4;
    $('#controladorGeral').css('left', posicaoDireita + 'px');
    $('#controladorGeral').css('visibility', 'visible');



});




function theRotator() {

    //Opacidade de todas as imagens a 0
    $('#DestaqueFotos div').css({ opacity: 0.0 });
    //Primeria imagem opaca
    $('#DestaqueFotos div:first').css({ opacity: 1.0 });

    // chama a funcao para correr o slideshow, 6000 = muda de imagem de 6 em 6 segundos
    var nValores = $('#DestaqueFotos div').length;
    if (nValores > 1) {
        intval = window.setInterval('rotate("seguinte")', 6000)
    }
}


function rotate(posicao) {

    if (posicao == 'seguinte') {
        //Primeira Imagem
        var current = ($('#DestaqueFotos div.show') ? $('#DestaqueFotos div.show') : $('#DestaqueFotos div:first'));
        if (current.length == 0) current = $('#DestaqueFotos div:first');
        //Próxima Imagem, quando chegar ao fim volta à primeira
        var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('#DestaqueFotos div:first') : current.next()) : $('#DestaqueFotos div:first'));
    }
    else if (posicao == 'anterior') {
        //Última Imagem
        var current = ($('#DestaqueFotos div.show') ? $('#DestaqueFotos div.show') : $('#DestaqueFotos div:last'));
        if (current.length == $('#DestaqueFotos div').next().length) current = $('#DestaqueFotos div:last'); /*esta first XXXX se houver stresss */
        //Imagem Anterior, quando chegar ao principio volta à última
        var next = ((current.prev().length) ? ((current.prev().hasClass('show')) ? $('#DestaqueFotos div:last') : current.prev()) : $('#DestaqueFotos div:last'));
    }

    efeitos(next, current);
};



function efeitos(next, current) {
    //Un-comment the 3 lines below to get the images in random order	
    //var sibs = current.siblings();
    //var rndNum = Math.floor(Math.random() * sibs.length );
    //var next = $( sibs[ rndNum ] );

    //Mostra a proxima imagem
    next.css({ opacity: 0.0 })
	.addClass('show')
	.animate({ opacity: 1.0 }, 1000);

    //Esconde a actual
    current.animate({ opacity: 0.0 }, 1000)
	.removeClass('show');

    //Troca o estado do botao numérico
    var cell = $('div[class^="show"]')
    var indexActivo = cell.parent("div").children().index(cell);
    var arrayBotoes = $('#controlador li');
    $('#controlador li').removeClass('activo');
    $(arrayBotoes[indexActivo]).addClass('activo');
}


/**** PROGRAMAÇÃO AGENDA ****/
