﻿$(document).ready(function () {
    $('html body a[rel=popup]').click(function () {
        var ancora = $(this).attr('href');
        abrePopup(ancora);
        return false;
    });

    $('.popup a.fechar').click(function () {
        fechaPopup();
        return false;
    });

    // exibe e oculta os meses do arquivo
    $('div.coluna2 div.arquivo ul h5 a').click(function () {
        $('div.coluna2 div.arquivo ul li ul').hide();
        $(this).parent().next('ul').show();
        return false
    });

    // Load theme
    Galleria.loadTheme('/scripts/srcgaleria/themes/classic/galleria.classic.js');
});

//function inicializaPost() {
//    // Comportamentos
//    $('.post div.coluna1 div.post div.metas a.exibir_comentarios').addClass('ativo');

//    // exibe e oculta area de comentarios e form para comentar
//    $('div.coluna1 div.post div.metas a.exibir_comentarios, div.coluna1 div.post div.metas div.opcoes_compartilhamento a.comentarios').click(function () {
//        var comentarios = $('div.comentarios', $(this).parents('div.metas'));
//        if (comentarios.is(':hidden')) {
//            comentarios.show();
//            $('a.exibir_comentarios', $(this).parents('div.metas')).addClass('ativo');
//        } else {
//            comentarios.hide();
//            $('a.exibir_comentarios', $(this).parents('div.metas')).removeClass('ativo');
//        }
//        return false
//    });
//}

function inicializaPost(post) {
    // Comportamentos
    $('#' + post + ' a.exibir_comentarios').addClass('ativo');

    // exibe e oculta area de comentarios e form para comentar
    $('#' + post + ' a.exibir_comentarios, #' + post + ' div.metas div.opcoes_compartilhamento a.comentarios').click(function () {
        var comentarios = $('#' + post + ' div.metas div.comentarios');
        if (comentarios.is(':hidden')) {
            comentarios.show();
            $('a.exibir_comentarios', $(this).parents('div.metas')).addClass('ativo');
        } else {
            comentarios.hide();
            $('a.exibir_comentarios', $(this).parents('div.metas')).removeClass('ativo');
        }
        return false
    });
}

function carregaGaleriaEsquerda(codGaleria) {
    $('.galleria.' + codGaleria).galleria({
        image_crop: true, // crop all images to fit
        thumb_crop: true, // crop all thumbnails to fit
        transition: 'fade', // crossfade photos
        transition_speed: 700, // slow down the crossfade
        data_config: function (img) {
            // will extract and return image captions from the source:
            return {
                title: $(img).parent().next('strong').html(),
                description: $(img).parent().next('strong').next().html()
            };
        },
        extend: function () {
            this.bind(Galleria.IMAGE, function (e) {
                // bind a click event to the active image
                $(e.imageTarget).css('cursor', 'pointer').click(this.proxy(function () {
                    // open the image in a lightbox
                    this.openLightbox();
                }));
            });
        }
    });
}

function carregaGaleriaGrande(codGaleria) {
    $('.galleria_grande.' + codGaleria).galleria({
        image_crop: true, // crop all images to fit
        thumb_crop: true, // crop all thumbnails to fit
        transition: 'fade', // crossfade photos
        transition_speed: 700, // slow down the crossfade
        data_config: function (img) {
            // will extract and return image captions from the source:
            return {
                title: $(img).parent().next('strong').html(),
                description: $(img).parent().next('strong').next().html()
            };
        },
        extend: function () {
            this.bind(Galleria.IMAGE, function (e) {
                // bind a click event to the active image
                $(e.imageTarget).css('cursor', 'pointer').click(this.proxy(function () {
                    // open the image in a lightbox
                    this.openLightbox();
                }));
            });
        }
    });
}

// Popups
// links com rel="popup" executarao a funcao abaixo. a popup abrira dependendo do href do mesmo
function abrePopup(popup) {

    $('.pelicula').remove();
    $('.popup').hide();

    var alturaBody = $('body').outerHeight() + 100;
    $('body').append('<div class="pelicula"></div>');
    $('.pelicula').css({ 'height': alturaBody, 'background-color': '#000', 'opacity': '.5' })
    $('#' + popup + '.popup').show();
    $('#' + popup + '.popup').centerInClient();

    if ($.browser.msie && $.browser.version == 6) {
        $('#' + popup + '.popup').css('margin-top', '10px');
        //$('form select').hide();
    }

    return false;
}
function fechaPopup() {
    $('.pelicula').remove();
    $('.popup').hide();
}


// Enquete
// votar
function VotarEnquete() {
    // esconde opcoes de votacao
    $('div.coluna2 div.enquete div.formenquete div.votacao').hide();
    // esconde opcoes de votar
    $('div.coluna2 div.enquete div.formenquete ul.opcoes li.votar').hide();
    // mostra resultado de sucesso
    $('div.coluna2 div.enquete div.formenquete div.sucesso_votacao').show();
    // mostra opcao de voltar
    $('div.coluna2 div.enquete div.formenquete ul.opcoes li.voltar').show();
}
// voltar
function VoltarEnquete() {
    // esconde resultado e mensagem de sucesso
    $('div.coluna2 div.enquete div.formenquete div.sucesso_votacao, div.coluna2 div.enquete div.formenquete div.resultado_votacao').hide();
    // esconde opcoes de voltar
    $('div.coluna2 div.enquete div.formenquete ul.opcoes li.voltar').hide();
    // mostra opcoes de votacao
    $('div.coluna2 div.enquete div.formenquete div.votacao').show();
    // mostra opcoes de votar
    $('div.coluna2 div.enquete div.formenquete ul.opcoes li.votar').show();
    // mostra botao resultado
    $('div.coluna2 div.enquete div.formenquete ul.opcoes li.resultado').show();
}
// resultado votacao
function VerResultadoEnquete() {
    // esconde opcoes de votacao
    $('div.coluna2 div.enquete div.formenquete div.votacao, div.coluna2 div.enquete div.formenquete div.sucesso_votacao').hide();
    // esconde botao votar
    $('div.coluna2 div.enquete div.formenquete ul.opcoes li.votar').hide();
    // esconde botao resultado
    $('div.coluna2 div.enquete div.formenquete ul.opcoes li.resultado').hide();
    // mostra resultado de sucesso
    $('div.coluna2 div.enquete div.formenquete div.resultado_votacao').show();
    // mostra botao voltar
    $('div.coluna2 div.enquete div.formenquete ul.opcoes li.voltar').show();
}

//FIX

function SetUniqueRadioButton(nameregex, current) {
    re = new RegExp(nameregex);
    for (i = 0; i < document.forms[0].elements.length; i++) {
        elm = document.forms[0].elements[i]
        if (elm.type == 'radio') {
            if (re.test(elm.name)) {
                elm.checked = false;
            }
        }
    }
    current.checked = true;
}

function CarregarComentarioTextareaCount(id) {
    var options2 = {
        'maxCharacterSize': 500,
        'originalStyle': 'originalDisplayInfo',
        'warningStyle': 'warningDisplayInfo',
        'warningNumber': 40,
        'displayFormat': '#input Caracteres de #left'
    };
    $('.txtareaComentario' + id).textareaCount(options2);
}

function comentar(idPost) {
    nome = $("#txtNome_" + idPost);
    email = $("#txtEmail_" + idPost);
    website = $("#txtWebsite_" + idPost);
    comentario = $("#txtComentario_" + idPost);

    valida = true;
    $("#lblErroNome" + idPost).hide();
    $("#lblErroEmail" + idPost).hide();
    $("#lblErroEmailInvalido" + idPost).hide();
    $("#lblErroComentario" + idPost).hide();

    if (nome.val() == "") {
        valida = false;
        $("#lblErroNome" + idPost).show();
    }

    if (email.val() == "") {
        valida = false;
        $("#lblErroEmail" + idPost).show();
    } else {
        regex = new RegExp(/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/);
        if (!regex.test(email.val())) {
            valida = false;
            $("#lblErroEmailInvalido" + idPost).show();
        }
    }

    if (comentario.val() == "") {
        valida = false;
        $("#lblErroComentario" + idPost).show();
    }

    if (valida) {
        $.ajax({
            url: '/Comentario.aspx?i=' + idPost + '&n=' + nome.val() + '&e=' + email.val() + '&w=' + website.val() + '&c=' + comentario.val(),
            success: function (data) {
                if ($("#olComentario_" + idPost + " li:first").html() == null)
                    $("#olComentario_" + idPost).html(data);
                else
                    $("#olComentario_" + idPost + " li:first").before(data);
                nome.val("");
                email.val("");
                website.val("");
                comentario.val("");

                var numComentarios = $("#olComentario_" + idPost + " li").length;
                $(".post_" + idPost + " .comentarios").html(numComentarios + " comentários");
                $("#olComentario_" + idPost).prev("h5").html("Comentários (" + numComentarios + "):");
            }
        });
    }
}

function loadOrkut(destino, titulo, summary, destination) {
    //                                        google.setOnLoadCallback(function () {
    new google.orkut.share.Button({
        lang: 'en',
        style: google.orkut.share.Button.STYLE_MINI,
        title: titulo,
        summary: (summary),
        destination: destination
    }).draw(destino);
    //                                        });
}
