Colloquio.Views.Comments=Class.create(Colloquio.Views.CoreView,{LIST_CLASS:'comments_list',CUTOFF_CLASS:'comments_cutoff',LENGTH_CLASS:'comments_length',NEW_COMMENT_LINK_CLASS:'new_comment_link',REPLY_LINK_CLASS:'reply_comment_link',ERROR_PANEL_ID_SUFFIX:'error_panel',SUBMIT_BUTTON_LABEL:'Post Comment',COMMENT_BODY_NAME:'comment[body]',ORDER_DESCENDING:'desc',ORDER_ASCENDING:'asc',STATUS_OK:'ok',STATUS_ERROR:'error',initialize:function($super,element,createUrl,options)
{$super(element);this.createUrl=createUrl;this._setOptions({enableCommentTypes:false,enableReplies:true,order:this.ORDER_DESCENDING},options);this.commentsList=this.element.selectFirstByClassName(this.LIST_CLASS);this.cutoffDisplay=this.element.selectFirstByClassName(this.CUTOFF_CLASS);this.lengthDisplay=this.element.selectFirstByClassName(this.LENGTH_CLASS);this._initializeReplyElements(this.element);this.newCommentLink=this.element.selectFirstByClassName(this.NEW_COMMENT_LINK_CLASS);this.newCommentLink.observe('click',this._newComment.bindAsEventListener(this));},_onNewCommentFormSubmit:function(event)
{new Ajax.Request(this.createUrl,{asynchronous:true,evalScripts:true,onSuccess:this._insertNewComment.bind(this),onFailure:this._onPostFailure.bind(this),parameters:Form.serialize(this.form)});return false;},_initializeReplyElements:function(element)
{var replyElements=element.selectByClassName(this.REPLY_LINK_CLASS);for(var i=0;i<replyElements.length;i++)
replyElements[i].observe('click',this._newReplyComment.bindAsEventListener(this));},_insertNewComment:function(request)
{var response=request.responseText.evalJSON(false);if(response.status==this.STATUS_ERROR)
{if(response.message)
this._postError('Your comment '+response.message);else
this._onPostFailure();return;}
var comment=response.comment;var updateElement=this._getUpdateElement(comment);var commentElement=Colloquio.Dom.newNode(Colloquio.Html.Tag.LIST_ITEM);commentElement.id=this._commentId(comment.id);commentElement.innerHTML=response.content;commentElement.style.display='none';if(this.options.order==this.ORDER_ASCENDING)
updateElement.appendChild(commentElement);else
updateElement.insert({top:commentElement});Effect.BlindUp(this.form.parentNode,{duration:1.0,afterFinish:function(){this.form.parentNode.remove();}.bind(this)});Effect.BlindDown(commentElement,{duration:1.0});this._initializeReplyElements(commentElement);if(comment.parent_id)
$(this._replyLinkId(comment.parent_id)).show();else
this.newCommentLink.show();if(this.cutoffDisplay)
{this.cutoffDisplay.update(this.cutoffDisplay.innerHTML.toInteger()+1);this.lengthDisplay.update(this.lengthDisplay.innerHTML.toInteger()+1);}},_getUpdateElement:function(comment)
{var updateElement=null;if(comment.parent_id)
{var updateElementId=this._commentChildrenId(comment.parent_id);var updateElement=$(updateElementId);if(!updateElement)
{updateElement=Colloquio.Dom.newNode(Colloquio.Html.Tag.UNORDERED_LIST);updateElement.id=updateElementId;$(this._commentId(comment.parent_id)).appendChild(updateElement);}}
else
{updateElement=this.commentsList;}
return updateElement;},_newComment:function(event)
{this._insertNewCommentForm(event.element());},_newReplyComment:function(event)
{var replyElement=Event.element(event);var parentId=replyElement.id.split('_')[1];this._insertNewCommentForm(replyElement,parentId);},_insertNewCommentForm:function(replyElement)
{var parentId='0';if(arguments.length>1)
parentId=arguments[1];replyElement.hide();var newCommentDiv=this._buildNewCommentForm();newCommentDiv.id=this._newCommentId(parentId);newCommentDiv.className='NewComment';var parentElement;var childrenElement;if(parentId=='0')
{parentElement=this.element;childrenElement=this.commentsList;}
else
{parentElement=$(this._commentId(parentId));childrenElement=$(this._commentChildrenId(parentId));}
if(!childrenElement)
parentElement.appendChild(newCommentDiv);else
parentElement.insertBefore(newCommentDiv,childrenElement);},_buildNewCommentForm:function()
{var submitButton=Colloquio.Dom.newNode(Colloquio.Html.Tag.INPUT,{type:Colloquio.Html.InputType.BUTTON,value:this.SUBMIT_BUTTON_LABEL});this.form=Colloquio.Dom.newNode(Colloquio.Html.Tag.FORM,[Colloquio.Dom.newAuthTokenField(),Colloquio.Dom.newNode(Colloquio.Html.Tag.PARAGRAPH,[Colloquio.Dom.newNode(Colloquio.Html.Tag.TEXTAREA,{name:this.COMMENT_BODY_NAME})]),submitButton],{method:'post'});submitButton.observe('click',this._onNewCommentFormSubmit.bindAsEventListener(this));this.errorPanel=Colloquio.Dom.newNode(Colloquio.Html.Tag.DIV,{id:this._errorPanelId()});return Colloquio.Dom.newNode(Colloquio.Html.Tag.DIV,[this.errorPanel,this.form]);},_onPostFailure:function()
{this._postError('There was a problem posting your comment.  Please try again later.');},_postError:function(message)
{this.errorPanel.update(message);},_commentChildrenId:function(commentId)
{return'comment_'+commentId.toString()+'_children';},_commentId:function(id)
{return'comment_'+id.toString();},_newCommentId:function(commentId)
{return'new_comment_'+commentId.toString();},_replyLinkId:function(commentId)
{return'reply_'+commentId.toString();},_errorPanelId:function()
{if(!this.errorPanelId)
this.errorPanelId=this.element.id+'_'+this.ERROR_PANEL_ID_SUFFIX;return this.errorPanelId;}});