var election = {

    isVoting: false, // switch to prevent requests when doublecklicking
    currentCandidateMID: null,
    voteValidationURL: '/Templates/JsonPages/Election/Vote.aspx?action=validate',
    voteConfirmationURL: '/Templates/JsonPages/Election/Vote.aspx?action=vote',
    validateVote: function (candidateId) {

        currentCandidateMID = candidateId;

        if (!isAutheticated) {
            election.openLoginPopup();
            return;
        }

        $.post(election.voteValidationURL, { cid: currentCandidateMID }, function (data) {

            if (!data.success)
                election.openErrorPopup(data.msg);
            else
                election.openConfirmationPopup(data.msg, data.status);

        }, "json");


    },

    registerVote: function (candidateId) {

        if (election.isVoting)
            return;

        election.isVoting = true;

        $.post(election.voteConfirmationURL, { cid: currentCandidateMID }, function (data) {

            if (!data.success)
                election.openErrorPopup(data.msg);
            else
                election.openFeedbackPopup(data.msg, data.status);

            election.isVoting = false;

        }, "json");

    },

    openLoginPopup: function () {
        $('#login-popup').show();
        $('#login-popup .ifirst').focus();
    },

    openErrorPopup: function (msg) {
        election.closeAllPopups();
        $('#voting-error h4').html(msg);
        $('#voting-error').show();
    },

    openConfirmationPopup: function (msg, statusMsg) {
        election.closeAllPopups();

        $('#voting-confirm .vote-msg').html(msg);
        $('#voting-confirm .votes-count').html(statusMsg);
        $('#voting-confirm').show();
    },

    openFeedbackPopup: function (msg, statusMsg) {
        election.closeAllPopups();

        $('#voting-feedback h4').html(msg);
        $('#voting-feedback .votes-count').html(statusMsg);
        $('#voting-feedback').show();
    },

    closeAllPopups: function () {
        $('.standard-popup').hide();
    }

}


function postToUrl(params) {

    var form = document.forms[0];

    if (params == "listSearchInput" && $("#listSearchInput").val() != "") {
        $("#area").val("0");
        $("#age").val("0");
        $("#interest").val("0");
    }

    if (params == "sortName") {

        $("#sortInterest").val("");
        $("#sortArea").val("");

        if ($("#sortName").val() == "") {
            $("#sortName").val("ascending");
        }

        else if ($("#sortName").val() == "ascending") {
            $("#sortName").val("descending");
        }
        else if ($("#sortName").val() == "descending") {
            $("#sortName").val("ascending");
        } 
    }

    if (params == "sortInterest") {

        $("#sortName").val("");
        $("#sortArea").val("");

        if ($("#sortInterest").val() == "") {
            $("#sortInterest").val("ascending");
        }
        else if ($("#sortInterest").val() == "ascending") {
            $("#sortInterest").val("descending");
        }
        else if ($("#sortInterest").val() == "descending") {
            $("#sortInterest").val("ascending");
        }
    }

    if (params == "sortArea") {

        $("#sortName").val("");
        $("#sortInterest").val("");
        
        if ($("#sortArea").val() == "") {
            $("#sortArea").val("ascending");
        }
        else if ($("#sortArea").val() == "ascending") {
            $("#sortArea").val("descending");
        }
        else if ($("#sortArea").val() == "descending") {
            $("#sortArea").val("ascending");
        }
    }

    form.submit();
}

$(document).ready(function () {

    if ($('#listSearchInput') != null) {

        $('#listSearchInput').keydown(function (event) {
            if (event.keyCode == '13') {
                postToUrl('listSearchInput');
                event.preventDefault();
            }
        });
    }

    $('#login-popup .top-area input').keypress(function (event) {
        if (event.keyCode == 13) {
            location.href = $('#login-popup .button-86')[0];
            event.preventDefault();
        }
    });


});

