$(document).ready(function() {

    $('a.flagQuestion').click(function() {
        $(this).hide();
        var questionID = $('#qID').val();
        var reason = $(this).attr("title");
        $.get('/api/qa/1.0/flagQuestion?questionID=' + questionID + '&reason=' + reason, function(response) {
            if ($('error', response).size() == 1) {
                alert($('message', response).text());
                return false;
            }

        });
        $(this).prev().html('Thank You');
        return false;
    });

    $('a.unFlagQuestion').click(function() {
        $(this).hide();
        var questionID = $('#qID').val();
        $.get('/api/qa/1.0/unFlagQuestion?questionID=' + questionID, function(response) {
            if ($('error', response).size() == 1) {
                alert($('message', response).text());
                return false;
            }

        });
        $(this).prev().html('Thank You');
        return false;
    });

    $('a.flagAnswer').click(function() {
        $(this).hide();
        var answerID = $(this).attr("rel");
        var reason = $(this).attr("title");

        $.get('/api/qa/1.0/flagAnswer?answerID=' + answerID + '&reason=' + reason, function(response) {
            if ($('error', response).size() == 1) {
                alert($('message', response).text());
                return false;
            }

        });
        $(this).prev().html('Thank You');
        return false;
    });

    $('a.unFlagAnswer').click(function() {
        $(this).hide();
        var answerID = $(this).attr("rel");

        $.get('/api/qa/1.0/unFlagAnswer?answerID=' + answerID, function(response) {
            if ($('error', response).size() == 1) {
                alert($('message', response).text());
                return false;
            }

        });
        $(this).prev().html('Thank You');
        return false;
    });

    $('a.updateStatus').click(function() {

        var questionID = $('#qID').val();
        var action = $(this).attr("title");

        if (action == 'closed') {
            if (!confirm("Are you sure you want to close this question to further editor answers?")) {
                return false;
            }
        } else {
            if (!confirm("Are you sure you want to reopen this question to further editor answers?")) {
                return false;
            }
        }

        $(this).hide();

        $.get('/api/qa/1.0/updateStatus?questionID=' + questionID + '&action=' + action, function(response) {
            if ($('error', response).size() == 1) {
                alert($('message', response).text());
                return false;
            }

        });
        $(this).prev().html('Thank You.');
        return false;
    });


    $('a.markBest').click(function() {
        $(this).hide();
        var answerID = $(this).attr('rel');
        var questionID = $('#qID').val();
        var doAction = $(this).attr('title');

        $.get('/api/qa/1.0/markBest?answerID=' + answerID + '&questionID=' + questionID + '&doAction=' + doAction, function(response) {
            if ($('error', response).size() == 1) {
                alert($('message', response).text());
                return false;
            }

        });
        $(this).prev().html('Thank You');
        return false;
    });


    $('a.answerBonus').click(function() {
        if (!confirm("Are you sure you want to award this user a bonus?")) {
            return false;
        }

        $(this).hide();
        var questionID = $('#qID').val();
        var answerID = $(this).attr("title");
        var userID = $(this).attr("rel");

        $.get('/api/qa/1.0/awardBonus?answerID=' + answerID + '&questionID=' + questionID + '&userID=' + userID, function(response) {
            if ($('error', response).size() == 1) {
                alert($('message', response).text());
                return false;
            }

        });
        $(this).prev().html('Thank You.');
        $('a.markAnswer').hide();
        $('a.markAnswer').prev().html('Reviewed');
        return false;
    });

    $('a.markAnswer').click(function() {
        if (!confirm("Are you sure? Bonus will no longer be possible for this answer.")) {
            return false;
        }

        $(this).hide();
        var answerID = $(this).attr("title");

        $.get('/api/qa/1.0/markAnswer?answerID=' + answerID, function(response) {
            if ($('error', response).size() == 1) {
                alert($('message', response).text());
                return false;
            }

        });
        $(this).prev().html('Gracias.');
        return false;
    });

    $('a.revokeFormQa').click(function() {
        $(this).siblings("form").toggle();
        return false;
    });

    $('input.doRevokeQa').click(function() {
        if (!confirm("Are you sure you want to revoke earnings for this answer?")) {
            return false;
        }

        $(this).parent("form").siblings("a.revokeFormQa").hide();
        $(this).parent("form").hide();

        var comment = $(this).siblings("input.revokeReason").val();
        var answerID = $(this).siblings("input.answerID").val();

        $.get('/api/qa/1.0/revokeEarnings?answerID=' + answerID + '&comment=' + comment, function(response) {
            if ($('error', response).size() == 1) {
                alert($('error', response).text());
                return false;
            }

        });

        $(this).parent("form").siblings("a.revokeFormQa").prev().html('Revoked.');
        $(this).parents("ul.qOptions").find("a.markAnswer").hide();
        $(this).parents("ul.qOptions").find("a.markAnswer").prev().html('Reviewed');
        $(this).parents("ul.qOptions").find("a.answerBonus").hide();
        return false;
    });

    $("#category").change(function () {
        if ($(this).children("option:selected").text() == 'Literature') {
            $("#getTitle").show();
            $("#getTitleLabel").show();
        } else {
            $("#getTitle").hide();
            $("#getTitleLabel").hide();
        }
    });    
});

function setQuestionRating(score) {
    var score = score;
    var questionID = $('#qID').val();
    $.get('/api/qa/1.0/rateQuestion?score=' + score + '&questionID=' + questionID, function(response) {
        if ($('error', response).size() == 1) {
            /*alert($('message', response).text());*/
        }
    });
    return false;
}

function setAnswerRating(score, element) {
    var score = score;
    var answerID = $(element).attr('id').substring(7);

    $.get('/api/qa/1.0/rateAnswer?score=' + score + '&answerID=' + answerID, function(response) {
        if ($('error', response).size() == 1) {
            /*alert($('info', response).text());*/
        }
    });
    return false;
}
