﻿Type.registerNamespace("MyVi.Voting");

MyVi.Voting = function (data) {
    for (val in data) {
        if (val.charAt(0) != "_" && !(data[val] instanceof Function))
            this[val] = data[val];
    }
};

MyVi.Voting.prototype =
{
    ID: undefined,
    VoteURL: undefined,
    VotedStat: undefined,
    Authorized: undefined,
    Container: undefined,
    VoteUpButtonText: '',
    ErrorMessage: '',

    //getVotedStat: function () {

    //this.VotedStat = eval('(' + this.VotedStatString + ')');
    //this.showVotingPanel();

    //        var target = this;

    //        $.ajax({
    //            url: this.GetVotedStatURL,
    //            data: "id=" + this.ID + "&r=" + Math.floor(Math.random() * 1000000),
    //            dataType: "json",
    //            success: function (data) {
    //                target.VotedStat = data.votedStat;
    //                target.showVotingPanel();
    //            }
    //        });
    //},

    showVotingPanel: function () {
        var target = this;
        container = this.Container;
        negativeVotes = $('<div></div>').addClass('nodisplay').attr('id', 'negative-votes-count')
            .text(this.VotedStat.NegativeVotesCount).appendTo($(container));
        voteDownButton = $('<a></a>').addClass('round-corner').attr('href', 'javascript:void(0)').appendTo($(container));
        $(voteDownButton).click(function () {
            target.vote(this, 0);
        });
        //        rate = $('<div></div>').addClass('rate').attr('id', 'current-rate')
        //            .text(this.VotedStat.Rate).appendTo($(container));
        voteUpButton = $('<a></a>').addClass('round-corner').attr('href', 'javascript:void(0)').text(target.VoteUpButtonText).appendTo($(container));
        $(voteUpButton).click(function () {
            target.vote(this, 10);
        });
        positiveVotes = $('<div></div>').addClass('nodisplay').attr('id', 'positive-votes-count')
            .text(this.VotedStat.PositiveVotesCount).appendTo($(container));
        if (this.VotedStat.IsUserVotedPositive == null) {
            $(voteUpButton).addClass('vote-up');
            $(voteDownButton).addClass('vote-down');
        }
        else {
            if (this.VotedStat.IsUserVotedPositive) {
                $(voteUpButton).addClass('vote-up-selected');
                $(voteDownButton).addClass('vote-down');
            }
            else {
                $(voteUpButton).addClass('vote-up');
                $(voteDownButton).addClass('vote-down-selected');
            }
            $('#negative-votes-count').show();
            $('#positive-votes-count').show();
        }
    },

    vote: function (sender, rate_val) {
        var target = this;
        if (sender.className.indexOf("vote-up-selected") == -1 & sender.className.indexOf("vote-down-selected") == -1) {
            $(sender).unbind("click");
            $.ajax({
                url: target.VoteURL,
                data: "id=" + this.ID + "&rate=" + rate_val,
                type: "POST",
                dataType: 'json',
                success: function (data) {
                    target.successVoting(data.data);
                },
                error: function (data) {
                    target.errorVoting();
                }
            });
        }
    },

    successVoting: function (data) {
        var target = this;
        if (data.IsUserVotedPositive == true) {
            this.VotedStat.PositiveVotesCount = this.VotedStat.PositiveVotesCount + 1;
            if ($('.vote-down-selected').length != 0)
                this.VotedStat.NegativeVotesCount = this.VotedStat.NegativeVotesCount - 1;
            $('.vote-up-selected').removeClass('vote-up-selected').addClass('vote-up');
            $('.vote-down-selected').removeClass('vote-down-selected').addClass('vote-down');
            $('.vote-up').removeClass('vote-up').addClass('vote-up-selected');
            $('.vote-up-selected').click(function () {
                target.vote(this, 10);
            });
        } else {
            this.VotedStat.NegativeVotesCount = this.VotedStat.NegativeVotesCount + 1;
            if ($('.vote-up-selected').length != 0)
                this.VotedStat.PositiveVotesCount = this.VotedStat.PositiveVotesCount - 1;
            $('.vote-up-selected').removeClass('vote-up-selected').addClass('vote-up');
            $('.vote-down-selected').removeClass('vote-down-selected').addClass('vote-down');
            $('.vote-down').removeClass('vote-down').addClass('vote-down-selected');
            $('.vote-down-selected').click(function () {
                target.vote(this, 0);
            });
        }
        //$('.rate').text(data.Rate);

        $('#negative-votes-count').text(this.VotedStat.NegativeVotesCount);
        $('#positive-votes-count').text(this.VotedStat.PositiveVotesCount);

        if (!($('#positive-votes-count').is(':visible'))) {
            var negativeVotesCountConteinerWidth = parseInt($('#negative-votes-count').width());
            var negativeVotesCountconteinerMargin = parseInt($('#negative-votes-count').css('margin-left').substring(0, 2));
            $('#negative-votes-count').css('margin-right', -(negativeVotesCountConteinerWidth + negativeVotesCountconteinerMargin));

            $('#negative-votes-count').show();
            $('#negative-votes-count').animate({ 'margin-right': '0px' }, 'slow');
            $('#positive-votes-count').show('slide', { direction: 'right' }, 'slow');
        }
    },

    errorVoting: function () {
        alert(this.ErrorMessage);
    }
};
