﻿$(function () {

    //str = "http://" + document.location.hostname + "/";
    str = 'http://www.netimoveis.com/';

    if ($.cookie('votou') != null) {
        exibe_resultado();
    }
    else {

        //CARREGAR A ENQUETE ATIVA
        var pergunta = "";
        var id_pergunta = "";
        var total_real = "";
        var respostas = "";
        var count = 0;

        //$("#enquete_loader").show();

        $.post(
            str + "xml/enquete.aspx",
            function (data) {

                $(data).find('REGISTRO').each(function () {

                    count++;

                    id_pergunta = $(this).find('ID_PERGUNTA').text();
                    pergunta = $(this).find('PERGUNTA').text();
                    total_real = $(this).find('TOTAL_REAL').text();

                    respostas = respostas + '<p class="respostas" id="' + $(this).find('ID_RESPOSTA').text() + '"><b>' + count + '.</b> ' + $(this).find('RESPOSTA').text() + '</p><br />';

                });

                $('#pergunta').html('<span id=' + id_pergunta + '>' + pergunta + '</span>');
                $('#respostas').html(respostas);

                $("#dados_enquete").show();
                $("#enquete_loader").hide();
            },
            "xml"
        );
    }


    $("#aba_enquete").click(function () {
        if ($(this).attr('class') == "fechar") {
            $("#div_enquete").animate({ left: -311 }, 400);
            $("#aba_enquete img").attr('src', 'images/geral/aba_enquete_fechada.png');
            $("#div_enquete").css('z-index', '1');
            $(this).attr('class', 'abrir');
            $("#div_block_all").hide();
        }
        else if ($(this).attr('class') == "abrir") {
            $("#div_enquete").animate({ left: 0 }, 400);
            $("#aba_enquete img").attr('src', 'images/geral/aba_enquete_aberta.png');
            $("#div_enquete").css('z-index', '1000000');
            $(this).attr('class', 'fechar');
            $("#div_block_all").show();
        }
    });

    //EFEITO QUANDO CLICA EM UMA OPÇÃO
    id_resp_escolhida = "";

    $('.respostas').live('click', function () {
        $('.respostas').css('background-color', 'transparent');
        $(this).css('background-color', '#f1f1f1');

        id_resp_escolhida = $(this).attr('id');

    });

    $("#bt_votar").click(function () {

        var comentario = $("#textarea_coment").text();

        if (id_resp_escolhida == "") {
            alert('Por favor, escolha uma das respostas.')
        }
        else {

            $("#dados_enquete").hide();
            //$("#enquete_loader").show();

            if (comentario == $("#textarea_coment").attr("defaultValue")) {
                comentario = "Sem comentário";
            }

            $.post(
                str + 'interativa/grava_enquete.aspx',
                {
                    resposta: id_resp_escolhida,
                    comentario: comentario,
                    total_real: total_real
                },
                function (data) {
                    $.cookie('votou', '1');
                    exibe_resultado();
                },
                'html'
            )
        }
    });


    // ABA FAVORITOS
    $("#aba_favoritos").click(function () {
        if ($(this).attr('class') == "fechar_fav") {

            $("#div_favoritos").animate({ left: -311 }, 400);
            $("#aba_favoritos img").attr('src', 'images/geral/aba_fav_fechada.png');
            $("#div_favoritos").css('z-index', '1');
            $(this).attr('class', 'abrir_fav');
            $("#div_block_all").hide();

        }
        else if ($(this).attr('class') == "abrir_fav") {

            $("#div_favoritos").animate({ left: 0 }, 400);
            $("#aba_favoritos img").attr('src', 'images/geral/aba_fav_aberta.png');
            $("#div_favoritos").css('z-index', '1000000');
            $(this).attr('class', 'fechar_fav');
            $("#div_block_all").show();

            // CARREGA FAVORITOS
            rand = Math.random(2);
            $('#lista_fav').load(str + 'CarregaFavoritos.aspx?a=' + rand);

        }
    });

    // ABA HISTORICO
    $("#aba_historico").click(function () {
        if ($(this).attr('class') == "fechar_historico") {

            $("#div_historico").animate({ left: -311 }, 400);
            $("#aba_historico img").attr('src', 'images/geral/aba_hist_fechada.png');
            $("#div_historico").css('z-index', '1');
            $(this).attr('class', 'abrir_historico');
            $("#div_block_all").hide();

        }
        else if ($(this).attr('class') == "abrir_historico") {

            $("#div_historico").animate({ left: 0 }, 400);
            $("#aba_historico img").attr('src', 'images/geral/aba_hist_aberta.png');
            $("#div_historico").css('z-index', '1000000');
            $(this).attr('class', 'fechar_historico');
            $("#div_block_all").show();

        }
    });

    $('#bt_fechar_enq').click(function () {
        $("#div_enquete").animate({ left: -311 }, 400);
        $("#aba_enquete img").attr('src', 'images/geral/aba_enquete_fechada.png');
        $("#div_enquete").css('z-index', '1');
        $('#aba_enquete').attr('class', 'abrir');
        $("#div_block_all").hide();
    });

    $('#bt_fechar_his').click(function () {
        $("#div_historico").animate({ left: -311 }, 400);
        $("#aba_historico img").attr('src', 'images/geral/aba_hist_fechada.png');
        $("#div_historico").css('z-index', '1');
        $('#aba_historico').attr('class', 'abrir_historico');
        $("#div_block_all").hide();
    });

    $('#bt_fechar_fav').click(function () {
        $("#div_favoritos").animate({ left: -311 }, 400);
        $("#aba_favoritos img").attr('src', 'images/geral/aba_fav_fechada.png');
        $("#div_favoritos").css('z-index', '1');
        $('#aba_favoritos').attr('class', 'abrir_fav');
        $("#div_block_all").hide();
    });


    if ($.cookie("NetTextoBusca") != null) {

        $("#desc_hist").html($.cookie("NetTextoBusca"));
        $('#icones_hist').show();
    }



});



function exibe_resultado() {
    $("#dados_enquete").hide();
    $("#resultado").show()

    var respostas = "";
    var pergunta = "";
    var total_real = 0;
    var total_virtual = 0;
    var count = 0;
    var total = 0;
    var percentual = 0;

    //CALCULA O TOTAL DE VOTOS
    $.post(
        str + "xml/enquete.aspx",
        function (data) {

            $(data).find('REGISTRO').each(function () {

                total_real = $(this).find('TOTAL_REAL').text();
                total_virtual = $(this).find('TOTAL_VIRTUAL').text();

                total = total + parseInt(total_virtual) + parseInt(total_real);
            });


            //EXIBE O RESULTADO
            $.post(
                str + "xml/enquete.aspx",
                function (data) {

                    $(data).find('REGISTRO').each(function () {

                        count++;

                        pergunta = $(this).find('PERGUNTA').text();
                        total_real = $(this).find('TOTAL_REAL').text();
                        total_virtual = $(this).find('TOTAL_VIRTUAL').text();

                        percentual = ((parseInt(total_virtual) + parseInt(total_real)) * 100) / total;
                        percentual = percentual.toPrecision(3);

                        respostas = respostas + '<p><b>' + count + '.</b> ' + $(this).find('RESPOSTA').text() + '</p><b class="qtd_votos">Votos:</b> ' + percentual + '%';

                    });

                    $('#res_pergunta').html('<span>OBRIGADO PELO VOTO<br /><br />' + pergunta + '</span>');
                    $('#res_respostas').html(respostas);

                    $("#enquete_loader").hide();

                },
                "xml"
            );

        },
        "xml"
    );

}
