﻿Home = function () {
    this._objIAjax = null;
    this._modalDiv = null;
}

Home.Load = function () {
    var _dados = new Home();
    _dados.initialize();
    return _dados;
}

Home.prototype = {

    initialize: function () {
        this._objIAjax = new AjaxInterface();
        this._objModal = new ModalClass();
        $(document).keypress($.methodDelegate(this, this._handlerEscKeyPress));
        $('.sendMail').click($.methodDelegate(this, this._modalCadastro));
        $('#btnSDMail').click($.methodDelegate(this, this._enviaNoticia));
        $('#btnVoteSvy').click($.methodDelegate(this, this._mostrarSub));
    },

    _handlerEscKeyPress: function (e) {
        if (e.keyCode == 27) {
            this._objModal._fecharModal(this._modalDiv);
        }
    },

    _modalCadastro: function (e) {
        $('#noticia').val(e.currentTarget.id.replace('sd_', ''));
        this._objModal._abrirModal('modal');
        this._modalDiv = { data: 'modal' };
    },

    _mostrarSub: function () {
        var str = { id: $('#idEnquete').val(), opcao: $('input:radio[name=opts]:checked').val() };
        this._objIAjax.executeBind('Home/votoEnquete', str, 'POST', $.methodDelegate(this, this._mostrarSub_Success), null, $.methodDelegate(this, this._mostrarSub_Begin));
    },

    _mostrarSub_Begin: function () {
        this._objIAjax._carregando('ldnEnquete', 'btnVoteSvy');
    },

    _mostrarSub_Success: function (data, context, method) {
        this._objIAjax._carregando(null, 'ldnAction');
        var msg = '<div id="enqResOpt">Seu voto já foi computado. Obrigado!</div>';
        $('#enqInfo').hide();
        //if (!data.Flag) {
            $('#enqResult').empty().show().html(msg);
//        } else {
//            var opts = '';
//            $.each(data.Enquete.Resultado, function (index, value) {
//                opts += '<li>' + index + ' - ' + value + '</li>';
//            });
//            $('#enqResult').empty().show().html(msg + '<div id="enqPergunta">Total de votos: ' + data.Enquete.TotalVotos + '</div>\
//                                <div id="enqResOpt">\
//                                    <ul>' + opts + '</ul>\
//                                </div>');
//        }
    },

    _validateForm: function () {
        var mail = $('#tbxDesEmail').val();
        var rt = true;
        if ($('#tbxNome').val() == '') { $('#retNome').empty().html('*'); rt = false; }
        if ($('#tbxDesNome').val() == '') { $('#retNome').empty().html('*'); rt = false; }
        if (mail == '') { $('#retDesEmail').empty().html('*'); tr = false; }
        if (!this._validaEmail(mail)) { $('#retDesEmail').empty().html('* Email inválido'); tr = false; }
        if ($('#txaMensagem').val() == '') { $('#rettxaMensagem').empty().html('*'); tr = false; }
        return rt;
    },

    _validaEmail: function (email) {
        var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
        if (email.search(emailRegEx) != -1) {
            return true;
        } else {
            return false;
        }
    },

    _enviaNoticia: function () {
        if (this._validateForm()) {
            $('.retorno').hide();
            var str = { nome: $('#tbxNome').val(), desnome: $('#tbxDesNome').val(), mail: $('#tbxDesEmail').val(), msg: $('#txaMensagem').val(), not: $('#noticia').val() };
            this._objIAjax.executeBind("../Home/enviaNoticia", str, 'POST', $.methodDelegate(this, this._enviaNoticia_Success), null, $.methodDelegate(this, this._enviaNoticia_Begin));
        }
    },

    _enviaNoticia_Begin: function () {
        this._objIAjax._carregando('ldnSendMail', 'btnSDMail');
    },

    _enviaNoticia_Success: function (data, context, method) {
        this._objIAjax._carregando('btnSDMail', 'ldnSendMail');
        this._objModal._fecharModal(this._modalDiv);
        alert(data.Msg);
        //$('#sendMailRet').empty().show().html(data.Msg);
    }
}

$(document).ready(function () {
    Home.Load();
});

