﻿var voting = new function () {
    bDocVoted = false;
    eDocVoted = false;
    this.voteemotion = function () {
        if (eDocVoted) {
            alert('Вы уже ставили свою оценку.');
            return;
        }
        var form = jQuery('#frm_emotions');
        if (form && form.length == 1) {
            form = form.get(0);
            var r = jQuery("a.emo1", form);
            var docId = jQuery('#eDocsID', form);
            if (docId && docId.length > 0) {
                docId = docId.get(0).value;

                var emotions = "";
                r.each(function () {
                    if (emotions != "") {
                        emotions = emotions + ",";
                    }
                    emotions = emotions + jQuery(this).attr("emotion");
                });
                jQuery.post("/DocEmotion", { DocsID: docId, emotions: emotions },
                    function (data) {
                        if (eDocVoted) return;
                        if (data.success) {
                            eDocVoted = true;
                            jQuery("#frm_emotions").hide();
                            jQuery("#DocEmotionVoteRes").show();
                        } else if (data.error == 1) {
                            eDocVoted = true;
                            jQuery("#frm_emotions").hide();
                            jQuery("#DocEmotionVoteRes").show();
                            jQuery("#DocEmotionVoteRes").html('Вы уже ставили свою оценку.');
                        } else {
                            alert('Проголосовать не удалось.');
                        }
                    });
            }
        }
    };



    this.vote = function () {
        if (bDocVoted) {
            alert('Вы уже ставили свою оценку.');
            return;
        }
        var form = jQuery('#voting');
        if (form && form.length == 1) {
            form = form.get(0);
            var r = jQuery("input.spec_checkbox", form);
            var docId = jQuery('#DocsID', form);
            if (docId && docId.length > 0) {
                docId = docId.get(0).value;
                r.each(function () {
                    if (this.checked) {
                        var mark = this.value;
                        jQuery.getJSON("/DocVote/" + docId + "/" + mark,
			                function (data) {
			                    if (bDocVoted) return;
			                    if (data.success) {
			                        bDocVoted = true;
			                        jQuery("#DocVoteTbl1", form).hide();
			                        jQuery("#DocVoteTbl2", form).show();
			                    } else if (data.error == 1) {
			                        bDocVoted = true;
			                        jQuery("#DocVoteTbl1", form).hide();
			                        jQuery("#DocVoteTbl2", form).show();
			                        jQuery("#DocVoteRes", form).html('Вы уже ставили свою оценку.');
			                    } else {
			                        alert('Проголосовать не удалось.');
			                    }
			                }
			            );
                    }
                });
            }
        }
    }
};

$(function () { $('#vote_button').click(voting.vote); });
$(function () { $('#emo_button').click(voting.voteemotion); });
