$(document).ready(function() {
    $('a.flagPost').click(function() {
        $(this).hide();

        var postID = $(this).attr("title");

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

        $(this).prev().html('Thank you.');

        return false;
    });

    $('a.unFlagPost').click(function() {
        $(this).hide();

        var postID = $(this).attr("title");

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

        $(this).prev().html('Thank you.');

        return false;
    });

    $('a.flagTopic').click(function() {
        $(this).hide();
        var tid = $(this).attr("title");

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

        $(this).prev().html('Thank you.');

        return false;
    });

    $('a.unFlagTopic').click(function() {
        $(this).hide();
        var tid = $(this).attr("title");

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

        $(this).prev().html('Thank you.');

        return false;
    });

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

        $(this).hide();
        var topicID = $('#topicID').val();
        var postID = $(this).attr("title");
        var userID = $(this).attr("rel");

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

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

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

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

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

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

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

        });

        $(this).parent("form").siblings("a.revokeForm").prev().html('Revoked.');
        return false;
    });

    /*Filters for group stream*/
    $('input#selectFilter1').click(function() {
        if (this.checked) {
            $('input.sFilter').each(function() {
                if (this.checked) {
                    this.checked = !this.checked;
                }
            });
        } else if ($('input.sFilter').is(':checked') == false) {
            $('input#selectFilter1').attr('checked', true);
        }
    });

    $('input.sFilter').click(function() {
        if ($(this).is(':checked') == true) {
            $('input#selectFilter1').attr('checked', false);
        } else if ($('input.sFilter').is(':checked') == false) {
            $('input#selectFilter1').attr('checked', true);
        }
    });

    /*More options for group stream*/
    $('a#mOptions').click(function() {
        $('div#exFilters').show();
        $('span#smOptions').hide();
        return false;
    });

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

});

function setTopicRating(score, element) {
    var score = score;
    var topicID = $(element).attr('id').substring(6);
    $.get('/api/groups/1.0/rateTopic?score=' + score + '&topicID=' + topicID, function(response) {
        if ($('error', response).size() == 1) {
            //alert($('message', response).text());
            return false;
        }
    });
    return false;
}
