Ext.ns("SP.comment");Ext.ns("SP.comment.data");SP.comment.data.boxes=new Array;SP.comment.data.stores=new Array;SP.comment.pushBox=function(id){SP.comment.data.boxes.push(id)};
SP.comment.reply=function(associd,id){el=Ext.get("reply_"+id);if(el==null)return;link=Ext.get("replylink_"+id);if(link==null)return;link.fadeOut({duration:0.2});var clear=function(){replyForm.destroy();link.fadeIn()};var replyForm=new Ext.FormPanel({commentID:associd,postSuccess:function(){clear();SP.comment.data.stores[associd].reload()},id:"commentForm_"+id,labelWidth:60,frame:true,buttonAlign:"left",cls:"commentForm",border:false,width:600,defaultType:"textfield",items:[{fieldLabel:"Reply",itemCls:"replyComment",
name:"body",xtype:"textarea",width:500,height:160,grow:true,growMin:160,allowBlank:false,enableKeyEvents:true},{name:"parentID",xtype:"hidden"}],buttons:[{text:"Reply",width:"400",style:"margin-left: 60px",handler:SP.comment.postSaveHandler},{text:"Cancel",style:"margin-right: 60px",handler:function(){if(replyForm.getForm().findField("body").getValue()!="")Ext.Msg.confirm("Confirm Cancel","Do you really want to cancel this comment?",function(id){if(id=="yes")clear()});else clear()}}]});replyForm.getForm().findField("parentID").setValue(id);
replyForm.render(el)};SP.comment.postSaveHandler=function(b,e){if(!SP.isMember())SP.member.login({loginSuccess:function(){doSave(b.ownerCt.ownerCt)}});else SP.comment.doSave(b.ownerCt.ownerCt)};
SP.comment.doSave=function(fp){if(fp.getForm().isValid())fp.getForm().submit({url:SP.link.pURL("comment","action/commentActions?action=postSubmit&id="+fp.commentID),waitMsg:"Saving your comment...",failure:function(f,o){Ext.MessageBox.alert("Failure","Your comment failed to post, please alert an administrator or try again.")},success:function(f,o){if(o.result.success=="false")Ext.MessageBox.alert("Failure","Your comment failed to post, please alert an administrator or try again: "+o.result.message);
else fp.postSuccess()}});else Ext.MessageBox.alert("Form Problem","Please correct the fields highlighted in red before submitting your comment.")};
SP.comment.render=function(id){var commentFormHeight=230;var commentFormWidth=650;formFields=[{fieldLabel:"New Comment",itemCls:"newComment",name:"body",xtype:"textarea",height:100,allowBlank:false,enableKeyEvents:true}];var commentForm=new Ext.FormPanel({id:"commentForm_"+id,commentID:id,postSuccess:function(){store.reload();commentForm.getForm().reset()},labelAlign:"top",labelWidth:60,buttonAlign:"left",frame:false,bodyStyle:"padding:1px 1px 0",border:false,height:175,style:"margin-top: 10px; margin-bottom: 10px;",
defaults:{width:commentFormWidth},defaultType:"textfield",items:formFields,buttons:[{text:"Post",style:"margin-bottom:10px;",handler:SP.comment.postSaveHandler}]});if(!SP.isMember())commentForm.getForm().findField("body").on("keypress",function(field,newVal,oldVal){SP.member.login({loginSuccess:function(){}})});var store=new Ext.data.JsonStore({url:SP.link.pURL("comment","action/commentActions?action=loadComments&id="+id),root:"records",fields:["displayName","dateCreated","body",{name:"postTimestamp",
type:"date",dateFormat:"timestamp"},"ownerImage","contentID","replyLevel"]});SP.comment.data.stores[id]=store;store.on("load",function(store,records,options){p=Ext.getCmp("commentPanel");if(records.length==1)p.setTitle("1 Comment");else if(records.length>1)p.setTitle(records.length+" Comments")});store.load();var listView=new Ext.list.ListView({store:store,emptyText:"No comments",autoWidth:true,hideHeaders:true,flex:1,columns:[{header:"",tpl:new Ext.XTemplate('<table class="comment level{replyLevel}"><tr id="cHeader"><td valign="middle">{ownerImage} <span class="commentBody">By {displayName} on {dateCreated}</span></td></tr>'+
'<tr><td><div id="commentInfo">{body}</div>'+'<div id="commentLinks">'+'<tpl if="replyLevel == 0"><span id="reply_{contentID}"><a  id="replylink_{contentID}" href="#" onclick="SP.comment.reply(\''+id+"','{contentID}');return false;\">Reply &#187;</a><span></tpl>"+"</div></span></td></tr></table>"),dataIndex:""}]});var commentPanel=new Ext.Panel({title:"Comments",id:"commentPanel",collapsible:false,border:true,autoHeight:true,items:[listView]});commentPanel.render("comments_"+id);commentForm.render("comments_"+
id)};Ext.onReady(function(){for(var i=0;i<SP.comment.data.boxes.length;i++)SP.comment.render(SP.comment.data.boxes[i])});

