//Galeria de Imagem
var atuFoto = -1;
var lstIdtFoto = new Array();
var lstNomFoto = new Array();

//Noticia
var idxNoticia = -1;

//Banner Home
var atuBanner = -1;
var lstBanner = new Array();

$(document).ready(function () {
    if ($.isFunction(self.onLoadPag)) onLoadPag();

    $('img.idioma').click(function () {
        var loc = location.search.substring(1, location.search.length);
        var i = '';

        if (loc.length == 0){
            i += '?';
        } else {
            i += '&';
        }

        i += 'idioma=' + $(this).attr('id');
        self.location = self.location + i
    });

    $('#qm0 > a').each(function() {
        h = (40 - $(this).height()) / 2
        $(this).css('padding-top', h - (h % 2));
        $(this).css('padding-bottom', h + (h % 2));
    });

    mudaBanner();

    $('div.box').add_layer("url('imagens/titulo_fundo_c.jpg') repeat-x top");
    $('div.box').add_layer("url('imagens/titulo_fundo_l.jpg') no-repeat top left");
    $('div.box').add_layer("url('imagens/titulo_fundo_r.jpg') no-repeat top right");

    if (prefixo == 'cadastro') {
        $tabs = $("div#tabHtml > ul").tabs();
        $('input:visible:not(:radio), select:visible, textarea:visible').focus(function(){
            $(this).addClass('campo_focus');
        });
     
        $('input:visible:not(:radio), select:visible, textarea:visible').blur(function(){
            $(this).removeClass('campo_focus');
        });
    }

    $("a[rel^='prettyPhoto']").prettyPhoto();

    lstFoto(true, true);
    
    $("#ant").click(function() {
        lstFoto(false, false);
    });

    $("#pro").click(function() {
        lstFoto(true, false);
    });

    if (prefixo == 'inc' && menu == 'home') {

        $('#banner_noticia #destaque_botao div').click(function() {
            $.post("ajax.php?tipo=pega_destaque", {
                id: $(this).attr('id')
            }, function(str){
                str = eval(str);
                $('#destaque_tit').html(url_decode(str.nom_titulo));
                $('#destaque_imagem').html(url_decode(str.imagem));
            });

            $('#destaque_botao div').removeClass('ativo');
            $(this).addClass('ativo');
        });
        

        mudaNoticia();

        $('div.box_conteudo').each(function() {
            var altTotal = $(this).find('img:eq(0)').outerHeight();
            var atu = 0;
            atu += $(this).find('.box_tit').outerHeight();
            atu += $(this).find('img:eq(1)').outerHeight();
            atu += $(this).find('.box_texto').outerHeight();
            atu += $(this).find('.box_mais').outerHeight();

            var dif = altTotal - atu - 15;

            if (dif > 0) {
                $(this).find('.box_mais').css('margin-top', dif);
            }
        });
    
        setTimeout("atuNoticia();", 1000);

    } else if (prefixo == 'cadastro' && menu == 'curriculo') {
        $("#idt_municipio").cascade("#idt_estado", {
            ajax: {
                url: 'ajax.php?tipo=municipio_estado'
            }
        });

    } else if (prefixo == 'inc' && (menu == 'videoinst' || menu == 'videotec')) {
        $('div.video').transparentCorners();

        for (var x = 1; x <= 2; x++) {
            for (var lin = 1; lin <= ($('div.video').length / 3); lin++) {
                var obj = $('div.video[linha=' + lin + ']');
                var tam_max = 0;

                obj.each(function() {
                    if ($(this).outerHeight() > tam_max) {
                        tam_max = $(this).outerHeight();
                    }
                });

                obj.each(function() {
                    var top = $(this).find('a').css('margin-top');
                    top = parseInt(top.substr(0, top.length - 2));

                    if (isNaN(top)) {
                        top = 0;
                    }

                    $(this).find('a').css('margin-top', top + tam_max - $(this).outerHeight());
                });
            }
        }
    }

    if (queryString('abre') == 's') {
        var idt = queryString('id');
        if (idt == '') {
            $("a[rel^='prettyPhoto']:first").click();
        } else {
            var $o = $("a[rel^='prettyPhoto'][idt='" + idt + "']").click();
            if ($o.length == 0) {
                $("a[rel^='prettyPhoto']:first").click();
            }
        }
    }
});

function atuNoticia() {
    var not = $('#banner_noticia').css('margin-top');
    
    if (not != undefined) {
        not = parseInt(not.substr(0, not.length - 2));

        if (isNaN(not)) {
            not = 0;
        }

        var dif = $('#c2').outerHeight() - $('#noticia').outerHeight() + not;

        if (dif > 0) {
            $('#banner_noticia').css('margin-top', dif);
        }
    }
}
function aleatorio(inferior,superior){
    numPossibilidades = superior - inferior
    aleat = Math.random() * numPossibilidades
    aleat = Math.floor(aleat)
    return parseInt(inferior) + aleat
}

function lstFoto(next, random) {

    if (random){
        atuFoto = aleatorio(0,lstIdtFoto.length - 1);
    }else {
        if (next) {
            atuFoto++;
        } else {
            atuFoto--;
        }
    }
    
    if (atuFoto < 0) {
        atuFoto = lstIdtFoto.length - 1;
    } else if (atuFoto >= lstIdtFoto.length) {
        atuFoto = 0;
    }

    $.post("ajax.php?tipo=lstFoto", {
        id: lstIdtFoto[atuFoto],
        nome: lstNomFoto[atuFoto]
    }, function(str){
        $('#lstFoto').html(url_decode(str));
        atuNoticia();
    });
}

function mudaNoticia() {
    var tot = $('#banner_noticia #destaque_botao div').length;

    idxNoticia++;

    if (idxNoticia >= tot) {
        idxNoticia = 0;
    }

    $('#banner_noticia #destaque_botao div:eq(' + idxNoticia + ')').click();
    setTimeout("mudaNoticia();", 5000);
}

function mudaBanner() {
    atuBanner++;

    if (atuBanner >= lstBanner.length) {
        atuBanner = 0;
    }

    $('#banner').css('background-image', 'url(' + url_decode(lstBanner[atuBanner]) + ')');
    setTimeout("mudaBanner();", 5000);
}

