var dTableCount={};var dTable={};var originalComment={};function focusOnThis(inst){tinyMCE.execCommand('mceFocus',false,inst.editorId);}
$(document).ready(function(){refreshId=startIntervalCheck();$('.addCommentArea').click(function(){$(this).html('');$(this).val('');var editPieces=$(this).attr('id').split("-");var discussionTableID=editPieces[1];var id='newComment-'+discussionTableID;$("#commentButtons-"+discussionTableID).fadeIn();tinyMCE.execCommand('mceAddControl',false,id);});$('.commentSubmit').click(function(){clearInterval(refreshId);var form=$(this).parents('form');var commentKey=$(form).find('[name=commentKey]').val();var commentTopicID=$(form).find('[name=commentTopicID]').val();var discussionTableID=$(form).find('[name=discussionTableID]').val();var commentField=tinyMCE.get('newComment-'+discussionTableID);var comment=commentField.getContent();if(comment==''){$("#commentError-"+discussionTableID).html('Please enter a comment.').fadeIn(500);return false;}
$.post('/ajax-widget.php',{'sidDir':sidDir,'widget':"discussion",'method':'addComment','comment':comment,'commentKey':commentKey,'commentCount':dTableCount[commentKey]},function(data){dTableCount[commentKey]=data.numComments;$("#discussionCount-"+discussionTableID).html(data.numComments);$.each(data.comments,function(index,comment){addDiscussionRow(commentKey,comment);});dTable[commentKey].fnPageChange('last');},"json");$("#commentError-"+discussionTableID).fadeOut(500);$("#commentButtons-"+discussionTableID).fadeOut();tinyMCE.activeEditor.setContent('Add comment...',{format:'raw'});tinyMCE.execCommand('mceRemoveControl',false,'newComment-'+discussionTableID);$("#newComment-"+discussionTableID).val('Add comment...');refreshId=startIntervalCheck();return false;});$('.cancelButton').click(function(){var pieces=$(this).attr('id').split("-");var discussionTableID=pieces[1];$("#commentError-"+discussionTableID).fadeOut(500);$("#commentButtons-"+discussionTableID).fadeOut();tinyMCE.activeEditor.setContent('Add comment...',{format:'raw'});tinyMCE.execCommand('mceRemoveControl',false,'newComment-'+discussionTableID);$("#newComment-"+discussionTableID).val('Add comment...');});$('.saveEditComment').live('click',function(){var editPieces=$(this).attr('id').split("-");var commentID=editPieces[1];var commentKey=editPieces[2];var commentField=tinyMCE.get('comment-'+commentID);var comment=commentField.getContent();$.post('/ajax-widget.php',{'sidDir':sidDir,'widget':"discussion",'method':'editComment','comment':comment,'commentKey':commentKey,'commentID':commentID},function(data){updateDiscussionRow(commentID,data.comment);},"json");var id='comment-'+commentID;$("#editComment-"+commentID+"-"+commentKey).html('Edit');$("#saveEditComment-"+commentID+"-"+commentKey).hide();tinyMCE.execCommand('mceRemoveControl',false,id);return false;});$('.editComment').live('click',function(){var editPieces=$(this).attr('id').split("-");var commentID=editPieces[1];var commentKey=editPieces[2];var id='comment-'+commentID;if(tinyMCE.getInstanceById(id)==null){$(this).html('Cancel Edit');$("#saveEditComment-"+commentID+"-"+commentKey).show();originalComment[id]=$("#"+id).html();tinyMCE.execCommand('mceAddControl',false,id);}else{$(this).html('Edit');$("#saveEditComment-"+commentID+"-"+commentKey).hide();tinyMCE.execCommand('mceRemoveControl',false,id);$("#"+id).html(originalComment[id]);}});tinyMCE.init({theme:"advanced",mode:"",plugins:"emotions",width:"100%",theme_advanced_buttons1:"bold,italic,underline,bullist,numlist,link,unlink,image,emotions,forecolor,removeformat,cleanup",theme_advanced_buttons2:"",theme_advanced_buttons3:"",theme_advanced_toolbar_location:"bottom",theme_advanced_toolbar_align:"center",content_css:"/clients/"+sidDir+"/stylesheet.php,/clients/"+sidDir+"/stylesheet_buttons.php,/clients/"+sidDir+"/css_custom.css",entity_encoding:"raw",add_unload_trigger:false,remove_linebreaks:false,inline_styles:false,convert_fonts_to_spans:true,init_instance_callback:"focusOnThis",editor_selector:""});setInterval(function(){updateAllTablePrettyDates();},5000);});function setupDiscussionTable(commentKey,discussionTableID,numComments,enableRating,sortOrder){dTableCount[commentKey]=parseInt(numComments);dTable[commentKey]=$('#'+discussionTableID).dataTable({"sDom":'lft<"#'+discussionTableID+'-addComment"><"dataTables_bottombar"ip>',"sPaginationType":"full_numbers","aaSorting":[[0,sortOrder]],"aoColumns":[{"bVisible":false},{"bSortable":false},{"bSearchable":enableRating,"bVisible":enableRating}]});$("#"+discussionTableID).css('width','100%');$("#"+discussionTableID+"-addComment").replaceWith($("#commentSubmission-"+discussionTableID));$("#commentSubmission-"+discussionTableID).show();updatePrettyDates(dTable[commentKey]);$("#viewTable-"+discussionTableID).click(function(){$("#discussion-"+discussionTableID).toggle("slow");$("#"+discussionTableID).css('width','100%');$(this).toggleClass("commentOff");$(".commentOn").text("Show Comments");$(".commentOff").text("Hide Comments");});$("#discussionDataTable-"+discussionTableID).show(2000);}
function addDiscussionRow(tableID,comment){dTable[tableID].fnAddData([comment.commentDate,comment.commentItem,comment.score]);}
function updateDiscussionRow(commentID,updatedComment){$("#comment-"+commentID).html(updatedComment);}
function startIntervalCheck(){var refreshId=setInterval(function(){var tableCountJSON=$.toJSON(dTableCount);$.post('/ajax-widget.php',{'sidDir':sidDir,'widget':"discussion",'method':"pollComments",'tableCountJSON':tableCountJSON},function(data){$.each(data,function(commentKey,commentData){dTableCount[commentKey]=commentData.numComments;$("#discussionCount-"+commentData.discussionTableID).html(commentData.numComments);$("#discussionCount-"+commentData.discussionTableID).effect('highlight');$("#discussionLastDate-"+commentData.discussionTableID).html(commentData.lastComment.commentDate);$("#discussionLastDate-"+commentData.discussionTableID).effect('highlight');$("#discussionLastScreenName-"+commentData.discussionTableID).html(commentData.lastComment.screenName);$("#discussionLastScreenName-"+commentData.discussionTableID).effect('highlight');$.each(commentData.comments,function(index,comment){addDiscussionRow(commentKey,comment);});});},"json");},500000);return refreshId;}
function updatePrettyDates(oTable){var nodes=oTable.fnGetNodes();$.each(nodes,function(nindex,node){aRowData=oTable.fnGetData(node);var newDate=prettyDate(aRowData[0]);if(newDate)
$(node).find('.commentPrettyDate').html(newDate);});}
function updateAllTablePrettyDates(){$.each(dTable,function(index,oTable){updatePrettyDates(oTable);});}
function prettyDate(time){var date=new Date((time||"").replace(/-/g,"/").replace(/[TZ]/g," ")),diff=(((new Date()).getTime()-date.getTime())/1000),day_diff=Math.floor(diff/86400);if(isNaN(day_diff)||day_diff<0||day_diff>=31)
return;return day_diff==0&&(diff<60&&"just now"||diff<120&&"1 minute ago"||diff<3600&&Math.floor(diff/60)+" minutes ago"||diff<7200&&"1 hour ago"||diff<86400&&Math.floor(diff/3600)+" hours ago")||day_diff==1&&"Yesterday"||day_diff<7&&day_diff+" days ago"||day_diff<31&&Math.ceil(day_diff/7)+" weeks ago";}
if(typeof jQuery!="undefined")
jQuery.fn.prettyDate=function(){return this.each(function(){var date=prettyDate(this.title);if(date)
jQuery(this).text(date);});};
