var tpc_c_debug=false;var tpc_c_increment_len=20;var tpc_c_insert_ad=4;var tpc_c_last_comment_idx=-1;var tpc_c_total_comments=-1;var tpc_c_json_url=null,tpc_c_rss_url=null;var tpc_c_descending=true;var tpc_c_comment_module_initialized=false;var tpc_c_df=new TPCDateFormat();function tpc_initCommentModule(data){if(data.result.length>0){$('#c-view').html($('<ul/>').attr('id','c-list'));if(!tpc_c_comment_module_initialized){tpc_c_total_comments=data.meta.totalComments;var _totalComments=$('<span/>').attr("class","c-live").html(tpc_c_total_comments+" comments");var _pendingComments=$('<span/>').attr("class","c-pending").html(data.meta.pendingComments+" pending");var _optionDescending=$('<option/>').attr('id','c-sort-desc').attr('selected','selected').attr('value','desc').html('Newest First');var _optionAscending=$('<option/>').attr('id','c-sort-asc').attr('value','asc').html('Oldest First');var _sorter=$('<select/>').attr('id','c-sort').append(_optionDescending).append(_optionAscending);_sorter.change(function(){tpc_c_descending=($(this).val()=='desc');tpc_resetCommentModule();tpc_loadArticleCommentModule();});var _rssContainer=$('<a/>').attr('id','c-rss').attr('href',tpc_c_rss_url).html('<img src="/universal/graphics/icons/feed-icon.gif" border="0" /> Follow comments with RSS');$('#c-info-panel').append(_rssContainer).append(_totalComments).append(_pendingComments).append(_sorter);}}else{$('#c-info-panel').append($('<div/>').attr('id','c-no-comments').html('There are no comments yet for this story'));$('#c-view').children().remove();}
tpc_c_comment_module_initialized=true;}
function tpc_createCommentHead(item,idx){var _userIdentStr='<span class="c-submitter-name">'+item.name+'</span>';if(item.location!='')_userIdentStr+='<span class="c-submitter-location"> from '+item.location+'</span>';tpc_c_df.d.setTime(item.createDate.time);var _commentDate=tpc_c_df.format('MMM dx, yyyy h:ii a');var _className="c-user-head"+((idx%2==0)?" c-even-head":" c-odd-head");var _domCHead=$('<div/>').attr('class',_className).append($('<div/>').attr('class','c-user').html(((tpc_c_debug)?'<strong>'+idx+'</strong> ':'')+_userIdentStr)).append($('<div/>').attr('class','c-date').html(_commentDate)).append($('<br/>').attr('clear','all'));return _domCHead;}
function tpc_createCommentBody(item,idx){var _className="c-user-body"+((idx%2==0)?" c-even-body":" c-odd-body");return $('<div/>').attr('class',_className).html(item.body);}
function tpc_createLoadMoreCommentsAnchor(){var _floor=tpc_c_last_comment_idx;var _ceiling=(tpc_c_last_comment_idx+tpc_c_increment_len);if(_ceiling>tpc_c_total_comments)
_ceiling=tpc_c_total_comments;var _loadMessage="Click here to load comments "+_floor+"-"+_ceiling+" of "+tpc_c_total_comments;return $('<a/>').attr('id','c-load-more-comments').attr('href','javascript:tpc_loadNextCommentSet()').attr('onclick','tpc_loadNextCommentSet()').html(_loadMessage);}
function tpc_resetCommentModule(){$('#c-view').children().remove();$('#c-view').html('<div align="center"><strong>Loading comments...</strong><br /><img src="/universal/images/bc-loading-video.gif" alt="Loading..." border="0" /></div>');}
function tpc_commentShowAd(){var _adTag=$('#jquery-comment-ad-hint').html();if(_adTag=='SHOW_AD'){var _ad=$('<div/>').attr('class','c-advertisement-container').html('ADVERTISEMENT<br /><img src="http://192.168.215.147/234X60AD.png" border="0" />');$('#c-list').append($('<li/>').attr('class','c-advertisement-item').append(_ad));}}
function tpc_loadNextCommentSet(){$('#c-load-more-comments').html('<img src="/universal/images/bc-loading-video.gif" alt="Loading..." border="0" />');$.getJSON(tpc_c_json_url,function(data){var _count=0;var _floor=tpc_c_last_comment_idx;var _ceiling=tpc_c_last_comment_idx+tpc_c_increment_len;if(_ceiling>tpc_c_total_comments)
_ceiling=tpc_c_total_comments;$.each(data.result,function(i,item){_count++;if(_count>_floor&&_count<=_ceiling){$('#c-list').append($('<li/>').attr('id','c-li-'+item.id).append(tpc_createCommentHead(item,_count)).append(tpc_createCommentBody(item,_count)));tpc_c_last_comment_idx=_count;}});$('#c-load-more-comments').remove();if(_ceiling<tpc_c_total_comments){$('#c-list').append(tpc_createLoadMoreCommentsAnchor());}});}
function tpc_loadArticleCommentModule(){$('#c-container').show();$.getJSON(tpc_c_json_url,function(data){tpc_initCommentModule(data);if(data.result.length>1){var _commentCollection=data.result;if(!tpc_c_descending){if(tpc_c_debug)alert("Reversing array.");_commentCollection.reverse();}
var _count=0;$.each(_commentCollection,function(i,item){_count++;if(i<tpc_c_increment_len){$('#c-list').append($('<li/>').attr('id','c-li-'+item.id).append(tpc_createCommentHead(item,_count)).append(tpc_createCommentBody(item,_count)));tpc_c_last_comment_idx=_count;}});if(tpc_c_total_comments>tpc_c_increment_len){$('#c-list').append(tpc_createLoadMoreCommentsAnchor());}}});}
