function BC_addScriptTag(id, url, callback) {
	var scriptTag = document.createElement("script");
	var noCacheIE = '&noCacheIE=' + (new Date()).getTime();
   
    // Add script object attributes
    scriptTag.setAttribute("type", "text/javascript");
    scriptTag.setAttribute("charset", "utf-8");
    scriptTag.setAttribute("src", url + "&callback=" + callback + noCacheIE);
    scriptTag.setAttribute("id", id);
	
	var head = document.getElementsByTagName("head").item(0);
	head.appendChild(scriptTag);	
}

function BC_getPlaysTotal(video_id) {
	BC_addScriptTag("playsTotal", "http://api.brightcove.com/services/library?command=find_video_by_id&video_id=" + video_id + "&video_fields=id,playsTotal&token=O2gGtQO8ouCkwCD8-Fu5xYBOqLKp0s3LoT1D8jV1swetzMPWavh5xg..", "BC_response");
}

function BC_addCommas(nStr){
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function BC_response(jsonData) {
    try {
        if (jsonData != null){
            var views = jQuery('#video-' + jsonData.id).text().split(":");
            var play_total = BC_addCommas(parseInt(views[1].replace(/,/g, "")) + jsonData.playsTotal);
            jQuery('#video-' + jsonData.id).text(views[0] + ": " + play_total);
            jQuery('#video-' + jsonData.id).css("display", "block");
        }
    } catch (e){ }
}

BC_getPlaysTotal(video_id);
