var ReadTopic=Class.create({curPage:0,target:null,elementType:"div",action:"/readTopicJson.do",pagerNS:"readTopic",lastMsgId:0,pageCount:0,currTopicId:0,isAdmin:false,viewerUserId:0,ownerId:0,tLogClass:"opHighlight",es:{scrollTo:null,topicPager:null,topicPager2:null,replyTarget:null},ps:{topLinks:"hubForumTopLinks-",postLinks:"postOptions-",editLink:"editMsg-",message:"msg_",messageRow:"msgRow_",newMessage:"hubFMsg-",header:"header-",footer:"footer-",pmLink:"uo-PM-"},ids:{replyTarget:"replyThreadTarget",target:"threadTarget",pgr1:"rtPagerTarget",pgr2:"rtPagerTarget2",replyBtn:"buttonReply",messageText:"messageText",ts:{header:"tpcHeaderTemplate",message:"msgTemplate",footer:"tpcFooterTemplate",reply:"quickReplyTemplate",lockedReplyCount:"lockedReplyCountTemplate",lockedArticleDiscussion:"lockedArticleDiscussionTemplate",sendReply:"sendReplyTemplate",postLinks:"postLinksTemplate",topLinks:"topLinksTemplate",loading:"loadingTemplate",newPage:"newPgTemplate",workoutMsg:"workoutMsgTemplate"},tl:{moderated:"moderated",unmoderated:"unmoderated",locked:"locked",unlocked:"unlocked",sticky:"sticky",nonSticky:"nonSticky",bulletin:"bulletin",nonBulletin:"nonBulletin",bump:"bumpTpc",unbump:"unBumpTpc",vetLocked:"vetLocked",vetUnlocked:"vetUnlocked",addFav:"addFavTpc"}},initialize:function(a){if(ReadTopic.instance){throw"An instance of ReadTopic has already been created."
}ReadTopic.instance=this;this.viewerUserId=a.viewerUserId;this.ownerId=a.ownerId;if(a.isAdmin){this.isAdmin=a.isAdmin}this.target=$(this.ids.target);this.es.topicPager=new AjaxPager(this.ids.pgr1,{eventNS:this.pagerNS,currentPage:this.curPage,pageCount:this.pageCount});this.es.topicPager2=new AjaxPager(this.ids.pgr2,{eventNS:this.pagerNS,currentPage:this.curPage,pageCount:this.pageCount});this.es.replyTarget=$(this.ids.replyTarget);Event.observe(document,this.pagerNS+":pagerStateChanged",this.onPageChanged.bindAsEventListener(this))},onPageChanged:function(a){if(a.memo.owner!=this){this.getTopicPage(this.currTopicId,a.memo.currentPage)}},getLastPostTopicPage:function(b){this.target.innerHTML="";this.setPagersVisible(false);$Templates().process(this.ids.target,this.ids.ts.loading,"");this.es.replyTarget.innerHTML="";
var c=function(h){var e=JSON.parse(h.responseText);if(e.success){var f=e.pageNum;var g=e.msgId;this.renderTopicPage(e,f);if(g!=null&&g>0){var d=$(this.ps.message+g);this.sleepScroll(d)}this.setPagersVisible(true)}else{this.target.innerHTML="There was a problem retrieving the topic page"}}.bind(this);var a="action=getLastPostTopicPage&id="+b+"&uid="+this.ownerId;new Ajax.Request(this.action,{onComplete:c,postBody:a,method:"post"})},setPagersVisible:function(a){this.es.topicPager.setVisible(a);this.es.topicPager2.setVisible(a)},getTopicPage:function(c,d){this.target.innerHTML="";this.setPagersVisible(false);$Templates().process(this.ids.target,this.ids.ts.loading,"");this.es.replyTarget.innerHTML="";var b=function(f){var e={};if(f.responseText.isJSON()){e=JSON.parse(f.responseText)}if(e.success){this.renderTopicPage(e,d);
this.setPagersVisible(true)}else{this.target.innerHTML="There was a problem retrieving the topic page"}}.bind(this);var a="action=loadTopicPage&id="+c+"&pg="+d;new Ajax.Request(this.action,{onComplete:b,postBody:a,method:"post"})},renderTopicPage:function(a,d){var c=a.msgs;var b=a.topicHeader;if(a.lockedArticle!=null&&a.lockedArticle==true){this.renderTopic(c,b,a.lockedArticle)}else{this.renderTopic(c,b)}this.renderTopLinks(b);this.refreshPagers(d,a.msgCount);this.currTopicId=b.topicId},renderTopic:function(g,a,e){var j=new Element(this.elementType,{id:this.ps.header+a.topicId});this.target.innerHTML="";this.target.insert({bottom:j});$Templates().process(j.id,this.ids.ts.header,a);$Templates().process(this.ps.topLinks+a.topicId,this.ids.ts.topLinks,a);for(var f=0;f<g.length;f++){var d=g[f];var c=new Element(this.elementType,{id:this.ps.newMessage+d.id});
this.target.insert({bottom:c});if(d.workoutTag){d.pgNm=this.curPage;$Templates().process(c.id,this.ids.ts.workoutMsg,d)}else{$Templates().process(c.id,this.ids.ts.message,d)}this.renderPostLinks(d);if(a.isTrainingLog==true&&a.authorId==d.authorId){var b=$(this.ps.messageRow+d.id);if(b){b.addClassName(this.tLogClass)}}}if(g.length>0){this.lastMsgId=g[g.length-1].id}else{this.lastMsgId=0}var h=new Element(this.elementType,{id:this.ps.footer+a.topicId});this.target.insert({bottom:h});$Templates().process(h.id,this.ids.ts.footer,a);if(this.viewerUserId){if(!a.locked){$Templates().process(this.ids.replyTarget,this.ids.ts.reply,a)}else{if(e!=null&&e==true){$Templates().process(this.ids.replyTarget,this.ids.ts.lockedArticleDiscussion,a)}else{$Templates().process(this.ids.replyTarget,this.ids.ts.lockedReplyCount,a)
}}}},renderPostLinks:function(d){if(d.authorAllowsPMs){var c=$(this.ps.pmLink+d.id);if(c!=null){c.show()}}if(this.viewerUserId){var a=false;if(!d.workoutTag){$Templates().process(this.ps.postLinks+d.id,this.ids.ts.postLinks,d);if(!this.isAdmin&&this.viewerUserId==d.authorId){a=true}}else{if(this.viewerUserId==d.authorId){a=true}}if(a){var b=$(this.ps.editLink+d.id);if(b){b.show()}}}},renderTopLinks:function(a){if(this.viewerUserId){var b=this.ids.tl;if(this.isAdmin){if(a.forceModerated==true){$(b.moderated).show()}else{$(b.unmoderated).show()}if(a.locked==true){$(b.locked).show()}else{$(b.unlocked).show()}if(a.sticky==true){$(b.sticky).show()}else{$(b.nonSticky).show()}if(a.bulletin==true){$(b.bulletin).show()}else{$(b.nonBulletin).show()}$(b.bump).show();$(b.unbump).show()}if(a.fcva==true){if(a.ignoreAcl==true){$(b.vetUnlocked).show()
}else{$(b.vetLocked).show()}}}},postReply:function(f,d,b){var c=$F(this.ids.messageText);if(!c||c.strip().length==0){alert("Enter a message in the Reply Box.");return}this.es.replyTarget.innerHTML="";$Templates().process(this.ids.replyTarget,this.ids.ts.sendReply,{msgTxt:c});var e=function(h){window.scrollTo(0,0);this.getTopicPage(f,(this.pageCount-1));if(this.lastMsgId>0){var g=$(this.ps.message+this.lastMsgId);this.sleepScroll(g)}}.bind(this);var a="subject="+b+"&categoryId="+d+"&messageType=reply&topicId="+f+"&messageText="+c;new Ajax.Request("/postMessage.do",{onComplete:e,postBody:a,method:"post"})},showUsrInfo:function(c){var a=$("uo-"+c);a.makePositioned();a.style.position="absolute";a.toggle()},refreshPagers:function(d,c){this.curPage=d;this.pageCount=Math.ceil(c/25);var b=this.es.topicPager;
b.pageCount=this.pageCount;b.iCur=d;b.refreshPager();var a=this.es.topicPager2;a.pageCount=this.pageCount;a.iCur=d;a.refreshPager()},sleepScroll:function(b){var a=this;this.scrollElem=b;if(this.scrollElem){setTimeout(function(){a.scrollElem.scrollTo()},700)}}});var $ReadTopic=function(){return ReadTopic.instance};
