﻿_contentLikerView = new function () {
    var _this = this;

    this.contentID = function (value) {
        if (typeof value != "undefined") {
            $("#hdnContentID").val(value);
        }
        return $("#hdnContentID").val();
    }

    this.rate = function (value) {
        if (typeof value != "undefined") {
            var sValue;
            if (value > 0) {
                sValue = "+" + value.toString();
            }
            else {
                sValue = value.toString();
            }
            $("#vote-valueID").html(sValue);
        }
        return $("#vote-valueID").html();
    }

    this.disableLiker = function () {
        $("#pnlLiker").addClass("voting-disabled");
    }
}

function contentLike(isLike) {    
    $.ajax({
        type: 'POST',
        url: "/Content/Vote",
        data: { 'contentID': _contentLikerView.contentID(),
            'isLike': isLike
        },
        success: contentLikeSuccess,
        error: contentLikeError
    });
}

function contentLikeSuccess(newRate, status, request) {    
    _contentLikerView.rate(newRate);
    _contentLikerView.disableLiker();
}

function contentLikeError(error) {      
   alert("Unable to submit your vote. Probably you have already been voted");
}
$(function () {

    if($("#pnlLiker").hasClass("voting-disabled")) {
        $("#pnlLiker a").unbind("click")
    } else {
        $("#likeId").bind("click", function (e) {
            contentLike(true);
            $("#pnlLiker a").unbind("click");
        });
        $("#dislikeId").bind("click", function (e) {
            contentLike(false);
            $("#pnlLiker a").unbind("click");
        });
    }
});
