﻿// Global Variables
var overlayformId = '#overlayform';
var frmAddComment = '#frmAddComment';

// block when ajax activity starts
$().ajaxStart(function() {
    if ($VoteElement != null && $VoteElement != undefined)
        $VoteElement.parent().parent().parent().block({ message: BlockMessage });
});

// unblock when ajax activity stops
$().ajaxStop(function() {
    if ($VoteElement != null && $VoteElement != undefined)
        $VoteElement.parent().parent().parent().unblock();
});

$(document).ready(function() {
    // Bind Votes
    EnableVotingImages();
});


// Submit Vote
function SubmitVote(VoteElement) {
    $VoteElement = $(VoteElement);
    $VoteElement.parent().parent().parent().block({ message: BlockMessage });
    var id = $VoteElement.attr("Id");
    var StoryId = id.substring(id.indexOf("-") + 1);
    var value = $VoteElement.attr("rel");

    if (value > 0) { // Vote was a yes vote
        $VoteElement.attr('src', VoteYesImageUrl);
    } else {
        $VoteElement.attr('src', VoteNoImageUrl);
    }
    $VoteElement.addClass("Disabled");

    DisableVotingImages();

    // Send Ajax request
    $.post(SubmitVoteUrl, { StoryId: StoryId, value: value }, VoteCallback, "json");

}

function VoteCallback(data) {
    if (data.ActionStatus == 'Success') {
        ShowMessage(data.Message);
        var NewTotal = data.VoteMessage;
        var TotalElement;
        if (data.Value > 0) {
            TotalElement = "#PositiveVoteMessage-" + data.StoryId;
        } else {
            TotalElement = "#NegativeVoteMessage-" + data.StoryId;
        }
        $(TotalElement).html(NewTotal);
    }
    else {
        ShowMessage(data.Message);
    }
    EnableVotingImages();
}

function EnableVotingImages() {
    $("img.SubmitVote:not('img.Disabled')").click(function() { SubmitVote(this); return false; });
}

function DisableVotingImages() {
    $("img.SubmitVote").unbind("click");
}