﻿//imagem anterior
function previous_image() {
    var pg_size = parseInt(fotosGaleriaInfo.pg_size);
    var pg_atual = parseInt(fotosGaleriaInfo.pg_atual);
    var pg_total = parseInt(fotosGaleria.length / pg_size) + ((fotosGaleria.length % pg_size > 0) ? 1 : 0);

    var reload = false;

    if (pg_atual > 1) {
        fotosGaleriaInfo.pg_atual = pg_atual - 1;
        pg_atual--;
        
        var temp = document.getElementById("items").innerHTML;
        document.getElementById("items").innerHTML = "";

        $(".scrollable-principal .items").fadeTo('fast', 0.8);
        $(".scrollable-principal .items").html("");
        
        for (i = 6; i > 0; i--) {
            var indice = (pg_atual * pg_size) - i;

            if (fotosGaleria.length - 1 >= (indice) && (indice) >= 0) {
                var img_ = fotosGaleriaInfo.url_thumb + fotosGaleria[indice].img;
                var alt_ = fotosGaleria[indice].img;
                if (fotosGaleria[indice].leg.length > 0)
                    alt_ = fotosGaleria[indice].leg;

                __addImagem(img_, alt_, indice);
            }
        }

        $(".scrollable-principal .items").fadeTo('fast', 1);
    }
}

//proxima imagem
function next_image() {    
    var pg_size = parseInt(fotosGaleriaInfo.pg_size);
    var pg_atual = parseInt(fotosGaleriaInfo.pg_atual);
    var pg_total = parseInt(fotosGaleria.length / pg_size) + ((fotosGaleria.length % pg_size > 0) ? 1 : 0);
    
    if (pg_atual < pg_total) {
        fotosGaleriaInfo.pg_atual = pg_atual + 1;

        $(".scrollable-principal .items").fadeTo('fast', 0.8);
        $(".scrollable-principal .items").html("");
        
        for (i = 0; i <= 5; i++) {
            var indice = (pg_atual * pg_size) + i;

            if (fotosGaleria.length - 1 >= indice) {
                var img_ = fotosGaleriaInfo.url_thumb + fotosGaleria[indice].img;
                var alt_ = fotosGaleria[indice].img;
                if (fotosGaleria[indice].leg.length > 0)
                    alt_ = fotosGaleria[indice].leg;

                __addImagem(img_, alt_, indice);
            }
        }

        $(".scrollable-principal .items").fadeTo('fast', 1);
    }
}
function __addImagem(img_, alt_, indice_) {
    var div = document.createElement('div');

    var link = document.createElement('a');
    link.href = "javascript:loadFoto('" + indice_ + "');";

    var img = new Image();
    img.alt = alt_;
    img.src = img_;
    img.width = 97;
    img.height = 73;

    jQuery(div).append(link);
    jQuery(link).append(img);

    $(".scrollable-principal .items").append(div);
}
function previous_image_outros(class_) {
    $("div.scrollable-" + class_).scrollable().prevPage();
}
function next_image_outros(class_) {
    $("div.scrollable-" + class_).scrollable().nextPage();
}

//carrega a imagem original
function loadFoto(ind) {
    $('#foto').addClass('loading');
    
    var creditos = "";
    if (fotosGaleria[ind].cred.length > 0) creditos = "<div class='creditos'>" + fotosGaleriaInfo.cred + ": " + fotosGaleria[ind].cred + "</div>";
    $("#legenda").html(fotosGaleria[ind].leg);

    $(function() {
        var img = new Image();

        var alt_ = fotosGaleria[ind].img;
        if (fotosGaleria[ind].leg.length > 0)
            alt_ = fotosGaleria[ind].leg;

        img.alt = alt_;

        $(img)
        .load(function() {
            $(this).hide();

            $('#foto').removeClass('loading');
            $('#foto').html("")
            $('#foto').append(creditos);
            $('#foto').append(this);

            $(this).fadeIn();
            $(this).attr('style', '');

            //equalcolumns
            ddequalcolumns_reset();
        })

        .attr('src', fotosGaleriaInfo.url + fotosGaleria[ind].img);
    });
}

function stars(n) {
    starsReset();
    for (i=1; i<=n; i++)
        document.getElementById("star" + i).className="selected";
}
function starsReset() {
    var stars = $(".stars div");
    for (i=0; i<stars.length; i++) 
        stars[i].className = "star";
}
function starsSelect(s) {
    starsReset();
    for (i=1; i<=document.getElementById("galeria_fotos_stars_sel").value; i++)
        document.getElementById("star" + i).className = (s == 's') ? "save" : "selected";
           
}
function saveAvaliacao(a) {
    var op = "operacao=saveAvaliacao&avaliacao=" + a + "&galeria_fotos=" + document.getElementById("galeria_fotos").value;
    fnAjax("post", "stars", appUrl_ + "galeria-fotos/ajax.ashx", op);
}
function avaliarComentario(i, c, a) {
    var op = "operacao=saveAvaliacaoComentario&avaliacao=" + a + "&comentario=" + c;
    fnAjax("post", "avaliacao_comentario" + i, appUrl_ + "galeria-fotos/ajax.ashx", op);
}
function denunciarComentario(i, c) {
    var op = "operacao=denunciarComentario&comentario=" + c;
    fnAjax("post", "acoes" + i, appUrl_ + "galeria-fotos/ajax.ashx", op);
}
function getComentarios(pag,all) {
    var op = "operacao=getComentarios&galeria=" + document.getElementById("galeria_fotos").value + "&pagina=" + pag + "&all=" + all;
    fnAjax("post", "comentarios_container", appUrl_ + "galeria-fotos/ajax.ashx", op, function() {tb_init('a.thickbox'); $(document).ready(function() {ddequalcolumns_reset()});});
}
