jQuery.fn.follow_question = function(content_id, is_saved, follow_text, unfollow_text) {
    var container = jQuery(this);
    
        
        var defaults = {
            unsaved_text : 'FOLLOW',
            saved_text : 'UNFOLLOW'
        }
        var options = {
            unsaved_text : follow_text,
            saved_text : unfollow_text
        }
        var options = $.extend({}, defaults, options);
    
        jQuery.extend(container, {
            content_id: content_id,
            is_saved: is_saved,
            saved_event_name: 'content_saved_'+content_id,
            unsaved_event_name: 'content_unsaved_'+content_id
        },options);
        var h = (container.is_saved) ? container.saved_text : container.unsaved_text;
        container.html(h);
        $(document).bind(container.saved_event_name, function(e) {
            container.is_saved = true;
            container.html(container.saved_text);
            container.attr('title', 'Click if you do not want to be notified of new activity with this question.');
        });
        $(document).bind(container.unsaved_event_name, function(e) {
            container.is_saved = false;
            container.html(container.unsaved_text);
            container.attr("title", "Be notified via email when there's new activity.");
        });
        $(container).unbind('click');
        container.click(function(event) {
            $.each($(container),function(index, value){
                event.preventDefault();
                // Force banner tool to make request to display recently saved/unsaved content
                $('#banner-tools-toggle').addClass("emptyElement");
                follower(container, content_id);
                return false
            });
        });
        if (is_saved){
            $(container).attr('title', 'Click if you do not want to be notified of new activity with this question.');
        }else{
            $(container).attr("title", "Be notified via email when there's new activity.");
        }
        return this;
    
}
function saver(container, content_id){
    var url = (container.is_saved) ? '/saved_content/unsave/'+container.content_id+'/' : '/saved_content/save/'+container.content_id+'/';
    var en = (container.is_saved) ? container.unsaved_event_name : container.saved_event_name;
    $.ajax({
        type: "GET",
        url: url,
        success: function(responseText){$(document).trigger(en);},
        error: function(request){if(request.status && request.status > 0) {alert( "Sorry, we're having some difficulties right now, please try back later");}}
    });
}
function follower(container, content_id){
    $.ajax({
        type: "GET",
        url: "/questions/"+content_id+'/'+((container.is_saved) ? 'unfollow' : 'follow'),
        success: function(msg){
            var data = JSON.parse(msg);
            saver(container, content_id);
        },
        error: function(request) {
            if(request.status && request.status > 0) {
                if (request.responseText === 'auth_required') {
                    $(document).bind('authentication_complete', function() {
                        $(document).unbind('authentication_complete');
                        ajax_auth_successful();
                        follower(container, content_id);
                        return false;
                    });
                    ajax_register('','','/accounts/choose-auth/');
                } else if (request.responseText === 'cannot_vote') {
                    alert('You cannot rate your own contribution')
                } else {
                    alert('having some technical difficulties, try back in a bit.');
                }
            }
        }
    });
}
