/***
 * FavTog functions
 * Requires: jquery
 * The below functions were branched with those in the score_base.js file, they should be abstracted, localized, and merged back into score_base.js.
 */

$(document).ready(function() {
    // Click Function
    $('.favtog').click(function() {
        // Get ID
        var id = $(this).attr('id').replace("favtog", "");
        // If element has class "scored" then we are unfavoriting, submit score of 0, otherwise submit score of 1
        if ($(this).hasClass('scored')) {
            favtogScore(id, 0);
        } else {
            favtogScore(id, 1);
        }
    });
    mtAttachEvent('load', mtUpdateScores);
    mtAttachEvent('usersignin', mtUpdateScores);
});

// Set scoring namespace
var namespace = 'favtog';
// Map local functions to generic functions
function favtogScore(obj_id, score) { mtScore(namespace, obj_id, score, 'scored', 'scored'); }
function favtogScoreResponse(obj_id, score, count) { mtScoreResponse(namespace, obj_id, score, count, 'scored', 'scored'); }


/***
 * mtScore - handles action of scoring
 */
function mtScore(ns, obj_id, score, pending, complete) {
    // Define object to score
    var el = $('#' + ns + obj_id);
    // Die if object doesn't exist
    if( !el ) return false;
    // update class
    if (score) {
        $(el).addClass(pending);
    } else {
        $(el).removeClass(pending);
    };
    // Send ajax request, and call success function
    $.ajax({
      type: "POST",
      url: mt.blog.comments.script + '?__mode=' + ns + '_score&static=1&entry_id=' + obj_id + '&score=' + score,
      success: function(count){
        favtogScoreResponse(obj_id,score,count);
      }
    });
}

/***
 * mtScoreResponse - handles response from ajax request after scoring action
 */
function mtScoreResponse(ns, obj_id, score, count, pending, complete) {
    // Define object to update
    var el = $('#' + ns + obj_id);
    // Die if object doesn't exist
    if (!el.size()) return false;
    // If scoring, add complete class and update text. Remove if setting score to zero.
    if (score) {
        $(el).removeClass(pending).addClass(complete).html(count);
    } else {
        $(el).removeClass(pending).removeClass(complete).html(count);
    };
}

/***
 * mtUpdateScores - on page load, updates scores based up the viewing user (or IP address)
 */
function mtUpdateScores() {
    // Get user
    var u = mtGetUser();
    // If Anonymous not allowed and no user, die.
    if (!mt.blog.community.ifAnonymousRecommendAllowed && !u) return false;
    // Create comma-separated string of Entry IDs. If non exist, die.
    var entry_ids = '';
    var scores = $('.favtog');
    if (scores.size()) {
        for (var i = 0; i < scores.length; i++) {
            var id = $(scores[i]).attr('id').replace("favtog", "");
            if (entry_ids != '') entry_ids += ",";
            entry_ids += id;
        }
    } else {
        return false;
    };

    // Submit entry_ids via Ajax
    $.ajax({
      type: "POST",
      url: mt.blog.community.script + '?__mode=score&namespace=favtog&blog_id=' + mt.blog.id + '&f=scored,count&jsonp=mtScore_cb&id=' + entry_ids,
      success: function(r){
          eval(r);
      }
    });
    return false;
}

/***
 * mtScore_cb - handles response from ajax request after update scores action
 */
function mtScore_cb(s_hash) {
    // for each id in the returned hash
    for (var id in s_hash) {
        // if object has been scored
        if ( s_hash[id].scored ) {
            // Get the object
            var el = $('#favtog' + id);
            // If object exists
            if ( el.size() ) {
                // add class scored and update content text
                $(el).addClass('scored').html(s_hash[id].scored);
            }
        }
    }
}
