var XWiki=(function(b){var a=b.viewers=b.viewers||{};
a.Comments=Class.create({xcommentSelector:".xwikicomment",initialize:function(){if($("commentscontent")){this.startup()
}if($("Commentstab")){this.container=$("Commentspane");
this.generatorTemplate="commentsinline.vm"
}else{if($$(".main.layoutsubsection").size()>0&&$$(".main.layoutsubsection").first().down("#commentscontent")){this.container=$$(".main.layoutsubsection").first();
this.generatorTemplate="comments.vm"
}}this.addTabLoadListener()
},startup:function(){if($("commentform")){this.form=$("commentform").up("form")
}else{this.form=undefined
}this.loadIDs();
this.addDeleteListener();
this.addReplyListener();
this.addSubmitListener(this.form);
this.addCancelListener();
this.addEditListener();
this.addPreview(this.form)
},loadIDs:function(){$$(this.xcommentSelector).each(function(d){var c=d.id;
d._x_number=c.substring(c.lastIndexOf("_")+1)-0
})
},addDeleteListener:function(){$$(this.xcommentSelector).each(function(c){c=c.down("a.delete");
if(!c){return
}c.observe("click",function(d){c.blur();
d.stop();
if(c.disabled){return
}else{new b.widgets.ConfirmedAjaxRequest(c.readAttribute("href")+(Prototype.Browser.Opera?"":"&ajax=1"),{onCreate:function(){c.disabled=true
},onSuccess:function(){var e=c.up(this.xcommentSelector);
if(this.form&&this.form.descendantOf(e.next(".commentthread"))){this.resetForm()
}e.replace(this.createNotification("$msg.get('core.viewers.comments.commentDeleted')"));
this.updateCount()
}.bind(this),onComplete:function(){c.disabled=false
}},{confirmationText:"$msg.get('core.viewers.comments.delete.confirm')",progressMessageText:"$msg.get('core.viewers.comments.delete.inProgress')",successMessageText:"$msg.get('core.viewers.comments.delete.done')",failureMessageText:"$msg.get('core.viewers.comments.delete.failed')"})
}}.bindAsEventListener(this))
}.bind(this))
},addEditListener:function(){$$(this.xcommentSelector).each(function(c){c=c.down("a.edit");
if(!c){return
}c.observe("click",function(d){c.blur();
d.stop();
if(c.disabled){return
}else{if(c._x_editForm){var e=c.up(this.xcommentSelector);
e.hide();
c._x_editForm.show()
}else{new Ajax.Request(c.readAttribute("href").replace("viewer=comments","xpage=xpart&vm=commentsinline.vm"),{onCreate:function(){c.disabled=true;
c._x_notification=new b.widgets.Notification("$msg.get('core.viewers.comments.editForm.fetch.inProgress')","inprogress")
},onSuccess:function(f){if(this.editing){this.cancelEdit(false,this.editing)
}var g=c.up(this.xcommentSelector);
g.insert({before:f.responseText});
c._x_editForm=g.previous();
this.addSubmitListener(c._x_editForm);
this.addPreview(c._x_editForm);
c._x_editForm.down("a.cancel").observe("click",this.cancelEdit.bindAsEventListener(this,c));
g.hide();
c._x_notification.hide();
this.editing=c
}.bind(this),onFailure:function(f){var g=f.statusText;
if(f.statusText==""||f.status==12031){g="Server not responding"
}c._x_notification.replace(new b.widgets.Notification("$msg.get('core.viewers.comments.editForm.fetch.failed')"+g,"error"))
}.bind(this),on0:function(f){f.request.options.onFailure(f)
},onComplete:function(){c.disabled=false
}})
}}}.bindAsEventListener(this))
}.bind(this))
},cancelEdit:function(d,c){if(d){d.stop()
}var e=c.up(this.xcommentSelector);
c._x_editForm.hide();
e.show();
this.cancelPreview(c._x_editForm);
this.editing=false
},addReplyListener:function(){if(this.form){$$(this.xcommentSelector).each(function(c){c=c.down("a.commentreply");
if(!c){return
}c.observe("click",function(d){c.blur();
d.stop();
if(this.form.up(".commentthread")){this.form.up(".commentthread").previous(this.xcommentSelector).down("a.commentreply").show()
}c.up(this.xcommentSelector).next(".commentthread").insert({top:this.form});
this.form["XWiki.XWikiComments_replyto"].value=c.up(this.xcommentSelector)._x_number;
this.form["XWiki.XWikiComments_comment"].value="";
this.form["XWiki.XWikiComments_comment"].focus();
c.hide()
}.bindAsEventListener(this))
}.bind(this))
}else{$$(this.xcommentSelector+" a.commentreply").each(function(c){c.hide()
})
}},addSubmitListener:function(c){if(c){c.down("input[type='submit']").observe("click",function(e){e.stop();
if(c.down("textarea").value!=""){var f=new Hash(c.serialize(true));
f.set("xredirect",window.docgeturl+"?xpage=xpart&vm="+this.generatorTemplate);
f.set("xpage","xpart");
f.set("vm",this.generatorTemplate);
var d=c.action.replace(/\?.*/,"");
f.unset("action_cancel");
c._x_notification=new b.widgets.Notification("$msg.get('core.viewers.comments.add.inProgress')","inprogress");
c.disable();
this.restartNeeded=false;
new Ajax.Request(d,{method:"post",parameters:f,onSuccess:function(){this.restartNeeded=true;
this.editing=false;
c._x_notification.replace(new b.widgets.Notification("$msg.get('core.viewers.comments.add.done')","done"))
}.bind(this),onFailure:function(g){var h=g.statusText;
if(g.statusText==""||g.status==12031){h="Server not responding"
}c._x_notification.replace(new b.widgets.Notification("$msg.get('core.viewers.comments.add.failed')"+h,"error"))
}.bind(this),on0:function(g){g.request.options.onFailure(g)
},onComplete:function(g){if(this.restartNeeded){this.container.update(g.responseText);
document.fire("xwiki:docextra:loaded",{id:"Comments",element:this.container});
this.updateCount()
}else{c.enable()
}}.bind(this)})
}}.bindAsEventListener(this))
}},addCancelListener:function(){if(this.form){this.initialLocation=new Element("span",{className:"hidden"});
$("_comments").insert(this.initialLocation);
this.form.down("a.cancel").observe("click",this.resetForm.bindAsEventListener(this))
}},addPreview:function(e){if(!e||!b.hasEdit){return
}var c="$xwiki.getURL('__space__.__page__', 'preview')".replace("__space__",encodeURIComponent($$("meta[name=space]")[0].content)).replace("__page__",encodeURIComponent($$("meta[name=page]")[0].content));
e.commentElt=e.down("textarea");
var d=e.down("input[type=submit]").up("div");
e.previewButton=new Element("span",{"class":"buttonwrapper"}).update(new Element("input",{type:"button","class":"button",value:"$msg.get('core.viewers.comments.preview.button.preview')"}));
e.previewButton._x_modePreview=false;
e.previewContent=new Element("div",{"class":"commentcontent commentPreview"});
e.commentElt.insert({before:e.previewContent});
e.previewContent.hide();
d.insert({top:e.previewButton});
e.previewButton.observe("click",function(){if(!e.previewButton._x_modePreview&&!e.previewButton.disabled){e.previewButton.disabled=true;
var f=new b.widgets.Notification("$msg.get('core.viewers.comments.preview.inProgress')","inprogress");
new Ajax.Request(c,{method:"post",parameters:{xpage:"plain",content:e.commentElt.value},onSuccess:function(g){this.doPreview(g.responseText,e);
f.hide()
}.bind(this),on400:function(g){this.doPreview("&nbsp;",e);
f.hide()
}.bind(this),onFailure:function(g){var h=g.statusText;
if(g.statusText==""||g.status==12031){h="Server not responding"
}f.replace(new b.widgets.Notification("$msg.get('core.viewers.comments.preview.failed')"+h,"error"))
},on0:function(g){g.request.options.onFailure(g)
},onComplete:function(g){e.previewButton.disabled=false
}.bind(this)})
}else{this.cancelPreview(e)
}}.bindAsEventListener(this))
},doPreview:function(d,c){c.previewButton._x_modePreview=true;
c.previewContent.update(d);
c.previewContent.show();
c.commentElt.hide();
c.previewButton.down("input").value="$msg.get('core.viewers.comments.preview.button.back')"
},cancelPreview:function(c){c.previewButton._x_modePreview=false;
c.previewContent.hide();
c.previewContent.update("");
c.commentElt.show();
c.previewButton.down("input").value="$msg.get('core.viewers.comments.preview.button.preview')"
},resetForm:function(c){if(c){c.stop()
}if(this.form.up(".commentthread")){this.form.up(".commentthread").previous(this.xcommentSelector).down("a.commentreply").show();
this.initialLocation.insert({after:this.form})
}this.form["XWiki.XWikiComments_replyto"].value="";
this.form["XWiki.XWikiComments_comment"].value="";
this.cancelPreview(this.form)
},updateCount:function(){if($("Commentstab")&&$("Commentstab").down(".itemCount")){$("Commentstab").down(".itemCount").update("$msg.get('docextra.extranb', ['__number__'])".replace("__number__",$$(this.xcommentSelector).size()))
}if($("commentsshortcut")&&$("commentsshortcut").down(".itemCount")){$("commentsshortcut").down(".itemCount").update("$msg.get('docextra.extranb', ['__number__'])".replace("__number__",$$(this.xcommentSelector).size()))
}},addTabLoadListener:function(c){var d=function(e){if(e.memo.id=="Comments"){this.startup()
}}.bindAsEventListener(this);
document.observe("xwiki:docextra:loaded",d)
},createNotification:function(c){var d=new Element("div",{"class":"notification"});
d.update(c);
return d
}});
return b
}(XWiki||{}));
document.observe("xwiki:dom:loaded",function(){new XWiki.viewers.Comments()
});