﻿
Type.registerNamespace("PlainJoeStudios.Web.Controls");PlainJoeStudios.Web.Controls.Comments=function(args){this.Ctor(args);};PlainJoeStudios.Web.Controls.Comments.prototype={_jqDivRoot:null,_jqCommentsHolder:null,_tComments:null,_paginationId:null,_PostPaneSetName:null,_jqTxtCommentMessage:null,_jqBtnCommentPost:null,_forObjectId:null,_forObjectType:null,_pageSize:null,Ctor:function(args)
{var This=this;this._paginationId=args.PaginationId;this._forObjectId=args.ForObjectId;this._forObjectType=args.ForObjectType;this._pageSize=args.PageSize;this._jqDivRoot=$("#"+args.RootId);this._jqCommentsHolder=this._jqDivRoot.find(".jqCommentsHolder");this._jqDelete=this._jqDivRoot.find(".jqDelete")
this._jqTxtCommentMessage=this._jqDivRoot.find(".jqTxtCommentMessage");this._jqBtnCommentPost=this._jqDivRoot.find(".jqBtnCommentPost");this._tComments=$tau("vx","CommentsSearchResult");this._PostPaneSetName=args.RootId+"#post";g_PlainJoePage.RegisterPane(this._PostPaneSetName,this._jqDivRoot.find(".jqPostComment_LogInFirst").eq(0),0);g_PlainJoePage.RegisterPane(this._PostPaneSetName,this._jqDivRoot.find(".jqPostComment").eq(0),1);g_PlainJoePage.RegisterPane(this._PostPaneSetName,this._jqDivRoot.find(".jqPostComment_PostedOk").eq(0),2);g_PlainJoePage.ShowPane(this._PostPaneSetName,0);g_PlainJoePage.GetControl(this._paginationId).RegisterPageChangeListener(function(oldPage,newPage){This.PageChange(oldPage,newPage,This._forObjectType,This._forObjectId);});this._jqDelete.click(function(){This.ButtonDelete_Click($(this).attr("vx:commentId"));});this._jqBtnCommentPost.click(function(){This.ButtenCommentPost_Click(This._forObjectType,This._forObjectId);});g_PlainJoePage.AttachLoggedInListener(function(user){This.UserLoggedIn(user)});},SetNewObject:function(objecType,objectId)
{var This=this;This._forObjectId=objectId;This._forObjectType=objecType;This.PageChange("1","1",objecType,objectId);},PageChange:function(oldPage,newPage,objectType,objectId)
{var This=this;This.Search(newPage,objectType,objectId);},ButtonDelete_Click:function(commentId)
{var This=this;This.DeleteComment(commentId);},UserLoggedIn:function(user)
{g_PlainJoePage.ShowPane(this._PostPaneSetName,1);},ButtenCommentPost_Click:function(objectType,objectId)
{var This=this;PlainJoeStudios.Veritas.WebServer.Services.Comments.PostComment(objectType,objectId,this._jqTxtCommentMessage.val(),function(r){This.PostComment_Success(objectType,objectId);},function(r){This.PostComment_Failure(r);});},PostComment_Success:function(objectType,objectId)
{var This=this;This.PageChange(1,1,objectType,objectId);$.scrollTo(This._jqCommentsHolder,800,{offset:{top:-100},easing:"swing"});this._jqTxtCommentMessage.val("");},PostComment_Failure:function(result)
{g_PlainJoePage.ShowError("Could not post comment. Please try again.\r\n\r\n"+result.get_message());},Search:function(newPage,objType,objId)
{var This=this;PlainJoeStudios.Veritas.WebServer.Services.Comments.CommentsSearch(objId,objType,This._pageSize,newPage,function(result)
{This.SearchLoadUI(result);},function(result)
{g_PlainJoePage.ShowError("Could not load comments. Please try again.\r\n"+result.get_message());});},SearchLoadUI:function(result)
{var This=this;This._jqCommentsHolder.empty();g_PlainJoePage.GetControl(This._paginationId).SetState(result.PageNumberRequested,result.TotalPagesAvailable);for(var i=0;i<result.Comments.length;++i)
{(function(i)
{var textEncoded=result.Comments[i].Text;textEncoded=textEncoded.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/</g,"&lt;").replace(/>/g,"&gt;");try
{textEncoded=textEncoded.replace(/\n/g,"<br />");}
catch(e){}
var tiResult=This._tComments.Generate(null,false,{"Text":textEncoded,"UserName":result.Comments[i].UserPostedBy,"DateCreated":result.Comments[i].DateCreated,"UserImage":result.Comments[i].UserImage});if(result.Comments[i].CanEdit)
{tiResult.namedElements.btnDelete.show();}
tiResult.namedElements.btnDelete.click(function()
{This.DeleteComment(result.Comments[i].Id,tiResult);});This._jqCommentsHolder.append(tiResult.root);})(i);}},DeleteComment:function(commentId,tiComment)
{var This=this;if(confirm("Are you sure you want to delete this comment?"))
{tiComment.root.slideUp();PlainJoeStudios.Veritas.WebServer.Services.Comments.DeleteComment(parseInt(commentId),parseInt(This._pageSize),function(result)
{g_PlainJoePage.GetControl(This._paginationId).SetState(1,result);This.PageChange(1,1,This._forObjectType,This._forObjectId);},function()
{g_PlainJoePage.ShowError("Could not delete comment. Please try again.");tiComment.root.slideDown();});}}};