// handlers and helper functions for recommend comment counts
//<![CDATA[
//get ID part
function getNumericID(storyID) {
    var str = storyID.toString();
    return str.substring(0, str.indexOf("."));
}

//get ID type part
function getTypeID(storyID) {
    var str = storyID.toString();
    return str.substring(str.indexOf("."));
}

//escape periods and other chars which cause jQuery to barf in IDs
function escapeSpecialCharacters(storyID) {
    return storyID.replace(/\./g, "\\.");
}

//on successfull recommend count retrieval
function handleRecommendCount(data, controlID, contentID) {
    
    var escID = escapeSpecialCharacters(controlID);
    var escCID = escapeSpecialCharacters(contentID);
    var control = jQuery(escID); //jQuery("#uslRecommendControl_" + controlID); //uslRecommendControl ID
    
    if (data.recommendCount) {
        var recCode = '<span id="recCount_' + contentID + '">' + data.recommendCount + '</span>';
        jQuery(escID).contents().find(".uslRecCount").html(recCode);
    } else {
        var requestBatch = new RequestBatch();
        requestBatch.AddToRequest(new UpdateArticleAction(new ArticleKey(contentID)));
        requestBatch.BeginRequest("http://sitelife.usatoday.com/ver1.0/Direct/Process", pluckCallback);
        if (window.console && window.console.log) {
            console.log("UpdateArticleAction - " + contentID);
        }
    }

    // add count, update style after getting the correct control
    jQuery("body").delegate(escID + ":not('.uslRecommendedCount')", "click", function () {
        //var cnt = parseInt(jQuery("#recCount_" + escID).html());
        //var cnt = parseInt(jQuery(escID).contents().find("#recCount_" + escCID).html());
        escCID = "#recCount_" + escCID;
        var cnt = parseInt(jQuery(escCID).html());
        
        if (isNaN(cnt)) {
            cnt = 0;
        }
        cnt++;

        jQuery(escID).removeClass("uslRecommendCount").addClass("uslRecommendedCount");
        jQuery(escCID).html(cnt);
        
        var requestBatch = new RequestBatch();
        requestBatch.AddToRequest(new RecommendAction(new ArticleKey(contentID)));
        requestBatch.BeginRequest("http://sitelife.usatoday.com/ver1.0/Direct/Process", pluckCallback);
        return false;
    });
}

function pluckCallback(responseBatch){
    // debug
}
//]]>
