if(Element.Methods['removeChildNodes']==null){Element.addMethods({removeChildNodes:function(ctrl){ctrl=$(ctrl);while(ctrl.childNodes[0]){ctrl.removeChild(ctrl.childNodes[0]);}
return ctrl;}});}
function loadContent(file){var head=document.getElementsByTagName('head').item(0);var scriptTag=$('loadScript');if(scriptTag){head.removeChild(scriptTag);}
script=document.createElement('script');script.src=file+'?random='+(Math.random()*10);script.type='text/javascript';script.id='loadScript';head.appendChild(script);}
function emailInput(destination){if(!destination||destination==''){$('destinationEmail').style.display='block';$('destinationWriter').style.display='none';$('emailLookup').value='';$('emailButton').onclick=function(){emailCancel();$('emailButton').onclick=function(){emailInput();}}}else{$('destinationEmail').style.display='none';$('destinationWriter').style.display='block';$('emailTo').value='';$('emailLookup').value=destination;$('contactButton').onclick=function(){emailCancel();$('contactButton').onclick=function(){emailInput(destination);}}}
$('emailStory').style.display='block';}
function emailCancel(){$('emailStory').style.display='none';}
function emailStory(){new Ajax.Request('/story/email/',{method:'post',onSuccess:function(transport){var response=transport.responseXML;alert($('emailTo').value?'Story sent.':'Message sent.');emailCancel();},onFailure:function(transport){alert('failed');},parameters:{content:$('emailContent').value?$('emailContent').value:'',id:$('emailId').value?$('emailId').value:'',to:$('emailTo').value?$('emailTo').value:'',lookup:$('emailLookup').value?$('emailLookup').value:'',from:$('emailFrom').value?$('emailFrom').value:'',name:$('emailFromName').value?$('emailFromName').value:'',comment:$('emailComment').value?$('emailComment').value:''}});}
function removeFunkyChars(str){var searchArray=new Array('“','”','…');var replaceArray=new Array('"','"','...');for(var i=0;i<=searchArray.length;i++){str=str.replace(eval('/'+searchArray[i]+'/g'),replaceArray[i]);}
return str;}
function limitText(limitField,limitCount,limitNum){limitField=$(limitField);limitCount=$(limitCount);if(limitField.value.length>limitNum){limitField.value=limitField.value.substring(0,limitNum);}else{limitCount.update('You have '+(limitNum-limitField.value.length)+' characters left.');}}
function processComments(jsonObject){storyCommentObj.process(jsonObject);}
function StoryCommentObj(storyId,authValue){this.commentOrder='';this.storyId=storyId;this.authValue=authValue;this.perPage='';this.page=1;this.method='comments.get';this.filter='';this.urlBase='http://my.dailyherald.com/api/json/index_test.php';this.load=function(){var url=this.urlBase+'?method='+this.method+'&story='+this.storyId+'&auth='+this.authValue+(this.commentOrder?'&order='+this.commentOrder:'')+(this.page?'&page='+this.page:'')+(this.perPage !=''?'&perpage='+this.perPage:'');loadContent(url);}
this.parseComment=function(transport){var commentXML=transport.responseXML.documentElement.getElementsByTagName('comment')[0];;var comment={'id':commentXML.getAttribute('id'),'story':commentXML.getAttribute('story'),'status':commentXML.getAttribute('status'),'vote':commentXML.getAttribute('vote'),'score':commentXML.getAttribute('score'),'displayName':commentXML.getElementsByTagName('user')[0].firstChild?commentXML.getElementsByTagName('user')[0].firstChild.nodeValue:'','title':'','comment':commentXML.getElementsByTagName('body')[0].firstChild?commentXML.getElementsByTagName('body')[0].firstChild.nodeValue:'','created':commentXML.getAttribute('inserted')};this.replaceBlock($('commentblock_'+comment.id),this.buildCommentBlock(comment));}
this.displayLoginBox=function(commentId){this.loginBox=$('ctSignUp').remove();$('commentblock_'+commentId).insert({before:this.loginBox.show()});}
this.vote=function(commentId,vote){if(this.user&&this.user.activated){var voteVal='';if(vote=='up'){voteVal='up';}else if(vote=='down'){voteVal='down';}else{return false;}
var authValue=this.authValue;new Ajax.Request('/comment/vote/',{method:'post',onSuccess:this.parseComment.bind(this),onFailure:function(transport){alert('failed');},parameters:{method:'stories.comments.comment.vote',vote:voteVal,comment:commentId,auth:authValue}});}else if(this.user){$('storyCommentDisplayOptIn').scrollTo();}else{this.displayLoginBox(commentId);}}
this.process=function(jsonObject){this.user=jsonObject.request.user?jsonObject.request.user:false;var comments=jsonObject.request.comments;var commentsInfo=jsonObject.request.commentsInfo;this.commentsInfo=commentsInfo;this.initUser();if(comments.length>0){this.commentOrder=commentsInfo.order;var sortControls=$('ctSort');this.replaceBlock(sortControls,this.buildSortControls(commentsInfo));var commentBoxContainer=$('commentBoxContainer');commentBoxContainer.removeChildNodes();commentBoxContainer.insert(this.buildPagination(commentsInfo,'top'));var ctCommentBox=new Element('div',{'id':'ctCommentBox'});for(var i=0;i<comments.length;i++){ctCommentBox.insert(this.buildCommentBlock(comments[i]));}
commentBoxContainer.insert(ctCommentBox);commentBoxContainer.insert(this.buildPagination(commentsInfo,'bottom'));}
if($('storyCommentCountBuzz')){$('storyCommentCountBuzz').removeChildNodes();$('storyCommentCountBuzz').insert('(');$('storyCommentCountBuzz').insert(new Element('span',{'id':'storyCommentCountNumber'}).update(commentsInfo.total));$('storyCommentCountBuzz').insert(')');$('storyCommentCountBuzz').insert(new Element('span',{'style':'padding:0 0 0 5px;'}).insert('&nbsp;|&nbsp;').insert(new Element('a',{'href':$('storyCommentCountNumber2')||$('storyCommentCountNumber3')?'/story/comments/?id='+this.storyId:'#storycomments'}).update('read')).insert('&nbsp;|&nbsp;').insert(new Element('a',{'href':$('storyCommentCountNumber2')||$('storyCommentCountNumber3')?'/story/comments/?id='+this.storyId:'#storycomments'}).update('post')));}
if($('storyCommentCountNumber2')){$('storyCommentCountNumber2').update('Click here to read '+commentsInfo.total+' comments');}
if($('storyCommentCountNumber3')){$('storyCommentCountNumber3').update('Click here to read '+commentsInfo.total+' comments or post a comment');}}
this.explain=function(){$('ctVoteExplain').toggle();return false;}
this.disable=function(){if($('storyCommentCountNumber')){$('storyCommentCountBuzz').style.visibility='hidden';}}
this.changeOrder=function(order){this.page=1;this.commentOrder=order;this.load();}
this.changePage=function(pageNum){this.page=pageNum;this.load();}
this.buildSortControls=function(){var ctSort=new Element('div',{'class':'ctSort'});ctSort.insert(new Element('h1').update('Sort by'));if(this.commentOrder=='old'){ctSort.insert(new Element('span',{'class':'ctSortOld'}).update('Oldest'));}else{ctSort.insert(new Element('a',{'href':'#','class':'ctSortOld'}).update('Oldest').observe('click',this.changeOrder.bind(this,'old')).observe('click',function(e){e.stop();}));}
if(this.commentOrder=='new'){ctSort.insert(new Element('span',{'class':'ctSortNew'}).update('Newest'));}else{ctSort.insert(new Element('a',{'href':'#','class':'ctSortNew'}).update('Newest').observe('click',this.changeOrder.bind(this,'new')).observe('click',function(e){e.stop();}));}
if(this.commentOrder=='score'){ctSort.insert(new Element('span',{'class':'ctSortUp'}).update('Readers\' Favorites'));}else{ctSort.insert(new Element('a',{'href':'#','class':'ctSortUp'}).update('Readers\' Favorites').observe('click',this.changeOrder.bind(this,'score')).observe('click',function(e){e.stop();}));}
return ctSort;}
this.buildPagination=function(commentsInfo,placement){var pagination=new Element('div',{'class':'ctPagination'});var ctShowing=new Element('div',{'class':'ctShowing'});ctShowing.insert('Showing ');ctShowing.insert(new Element('strong').update(new String((commentsInfo.page-1)*commentsInfo.perpage+1)+'-'+new String((commentsInfo.total<(commentsInfo.page*commentsInfo.perpage)?commentsInfo.total:(commentsInfo.page*commentsInfo.perpage)))));ctShowing.insert('&nbsp;out of&nbsp;');ctShowing.insert(new Element('strong').update(commentsInfo.total));pagination.insert(ctShowing);var ctNumbers=new Element('div',{'class':'ctNumbers'});if(this.page>1){ctNumbers.insert(new Element('a',{'href':'#storycomments'}).observe('click',this.changePage.bind(this,1)).update('&lt;&lt; First'));}
if(this.page-1>0){ctNumbers.insert(new Element('a',{'href':'#storycomments'}).observe('click',this.changePage.bind(this,this.page-1)).update('&lt; Prev'));if(this.page-2>0){ctNumbers.insert(new Element('a',{'href':'#storycomments'}).observe('click',this.changePage.bind(this,this.page-2)).update(''+(this.page-2)));}
ctNumbers.insert(new Element('a',{'href':'#storycomments'}).observe('click',this.changePage.bind(this,this.page-1)).update(''+(this.page-1)));}
ctNumbers.insert(new Element('span',{}).update(''+(this.page)));if(this.page+1<=Math.ceil(commentsInfo.total/commentsInfo.perpage)){ctNumbers.insert(new Element('a',{'href':'#storycomments'}).observe('click',this.changePage.bind(this,this.page+1)).update(''+(this.page+1)));if(this.page+2<=Math.ceil(commentsInfo.total/commentsInfo.perpage)){ctNumbers.insert(new Element('a',{'href':'#storycomments'}).observe('click',this.changePage.bind(this,this.page+2)).update(''+(this.page+2)));}
ctNumbers.insert(new Element('a',{'href':'#storycomments'}).observe('click',this.changePage.bind(this,this.page+1)).update('Next &gt;'));ctNumbers.insert(new Element('a',{'href':'#storycomments'}).observe('click',this.changePage.bind(this,Math.ceil(commentsInfo.total/commentsInfo.perpage))).update('Last &gt;&gt;'));}
pagination.insert(ctNumbers);return pagination;}
this.initUser=function(){if(this.user){if(document.getElementById('storyCommentProfile')){$('storyCommentProfile').removeChildNodes();$('storyCommentProfile').insert(this.user.displayName+', your Display Threshold is ');$('storyCommentProfile').insert(new Element('span').update(this.commentsInfo.scoreThreshold));$('storyCommentProfile').insert(' :: ');$('storyCommentProfile').insert(new Element('a',{'href':'http://my.dailyherald.com/account/?tab=memberservices&redirect='+escape(window.location)}).update('Preferences'));}
if(this.user.activated&&this.user.commentPermission){if(this.commentsInfo.total>0&&$('submitStoryComment')&&this.method !='comments.getinfo'){$('submitStoryComment').style.display='block';}else if(this.commentsInfo.total==0&&$('submitStoryComment')){$('submitStoryComment').style.display='block';;$('ctHiddenRules').style.display='block';;$('ctHiddenConstructive').style.display='block';;}}else if(!this.user.activated){$('storyCommentDisplayOptIn').style.display='block';;}}else if($('storyCommentDisplaySignIn')){if($('storyCommentProfile')){$('storyCommentProfile').removeChildNodes();$('storyCommentProfile').insert(new Element('a',{'href':'#signin'}).update('Sign in | Sign up to comment'));}
if(this.commentsInfo.total>0&&$('submitStoryComment')&&this.method !='comments.getinfo'){$('storyCommentDisplaySignIn').style.display='block';}else if(this.commentsInfo.total==0&&$('submitStoryComment')){$('storyCommentDisplaySignIn').style.display='block';$('ctHiddenRules').style.display='block';$('ctHiddenConstructive').style.display='block';}else if(this.method !='comments.getinfo'){$('storyCommentDisplaySignIn').style.display='block';}}else{if(document.getElementById('storyCommentProfile')){$('storyCommentProfile').removeChildNodes();$('storyCommentProfile').insert(new Element('a',{'href':'http://my.dailyherald.com/account/?redirect='+escape(window.location)}).update('Sign in | Sign up to comment'));}}
if($('ctVoteExplainThreshold')){$('ctVoteExplainThreshold').removeChildNodes();$('ctVoteExplainThreshold').insert(this.commentsInfo.scoreThreshold>0?'+'+this.commentsInfo.scoreThreshold:this.commentsInfo.scoreThreshold);$('ctVoteExplainThreshold').insert(new Element('img',{'src':'http://www.dailyherald.com/images/comments/comments3_thumbs_vote_down.png','width':19,'height':24,'alt':'Thumbs Down'}));}}
this.replaceBlock=function(oldBlock,newBlock){oldBlock=$(oldBlock);newBlock=$(newBlock);oldBlock.removeChildNodes();oldBlock.classNames().each(function(cn){newBlock.addClassName(cn);});while(newBlock.childNodes[0]){oldBlock.appendChild(newBlock.childNodes[0]);}}
this.expandComment=function(currentComment,type){this.replaceBlock($('commentblock_'+currentComment.id),this.buildCommentBlock(currentComment,true));}
this.shrinkComment=function(currentComment,type){this.replaceBlock($('commentblock_'+currentComment.id),this.buildCommentBlock(currentComment));}
this.buildCommentBlock=function(currentComment,override){var ctComment=new Element('div',{'class':'ctComment','id':'commentblock_'+currentComment.id});var isDeleted=currentComment.status==6?true:false;var isBelowThreshold=(currentComment.status==1&&currentComment.score<this.commentsInfo.scoreThreshold)?true:false;var isVotedUp=currentComment.vote==1?true:false;var isVotedDown=currentComment.vote==-1?true:false;var isUser=currentComment.isUser?true:false;var isNegative=currentComment.score<0?true:false;var isPositive=currentComment.score>0?true:false;if(isVotedUp||isDeleted||isUser||(isBelowThreshold&&!override&&!isVotedDown)){ctComment.insert(new Element('span',{'class':'voteUp'}));}else{ctComment.insert(new Element('a',{'href':'#','class':'voteUp'}).observe('click',this.vote.bind(this,currentComment.id,'up')).observe('click',function(e){e.stop();}));}
if(isVotedDown||isDeleted||isUser||(isBelowThreshold&&!override&&!isVotedUp)){ctComment.insert(new Element('span',{'class':'voteDown'}));}else{ctComment.insert(new Element('a',{'href':'#','class':'voteDown'}).observe('click',this.vote.bind(this,currentComment.id,'down')).observe('click',function(e){e.stop();}));}
var ctCommentH1=new Element('h1');if(!isDeleted){ctCommentH1.insert((currentComment.displayName.length>30?currentComment.displayName.substring(0,30)+'...':currentComment.displayName)+' :: ');}
ctCommentH1.insert(new Element('span').update(currentComment.created));ctComment.insert(ctCommentH1);var ctCommentH2=new Element('h2');if(isDeleted){ctCommentH2.addClassName('ctDeleted');}else if(isNegative){ctCommentH2.insert(currentComment.score);ctCommentH2.insert(new Element('img',{'src':'http://www.dailyherald.com/images/comments/comments3_thumb_d.png','width':19,'height':24,'alt':'Thumbs Down'}));}else if(isPositive){ctCommentH2.insert('+'+currentComment.score);ctCommentH2.insert(new Element('img',{'src':'http://www.dailyherald.com/images/comments/comments3_thumb_u.png','width':19,'height':24,'alt':'Thumbs Up'}));}else{ctCommentH2.insert(new Element('img',{'src':'http://www.dailyherald.com/images/comments/comments3_thumb_d.png','width':19,'height':24,'alt':'Thumbs Down'}));ctCommentH2.insert(currentComment.score);ctCommentH2.insert(new Element('img',{'src':'http://www.dailyherald.com/images/comments/comments3_thumb_u.png','width':19,'height':24,'alt':'Thumbs Up'}));}
ctComment.insert(ctCommentH2);var ctCommentBody=new Element('div',{'class':'ctCommentBody'});if(!isDeleted&&!isVotedDown&&(!isBelowThreshold||isUser||isVotedUp)){var commentLines=new String(currentComment.comment).split('\n');for(var lineI=0;lineI<commentLines.length;lineI++){ctCommentBody.insert(new Element('p').update(commentLines[lineI]));}
ctComment.insert(ctCommentBody);var ctAbuseBar=new Element('div',{'class':'ctAbuseBar'});if(this.user){ctAbuseBar.insert(new Element('a',{'class':'ctReportAbuse','href':'http://my.dailyherald.com/comment/report_abuse/?story='+currentComment.story+'&comment='+currentComment.id}));}else{ctAbuseBar.insert(new Element('a',{'class':'ctReportAbuse','href':'#'}).observe('click',this.displayLoginBox.bind(this,currentComment.id)).observe('click',function(e){e.stop();}));}
if(this.user&&this.user.deletePermission){ctAbuseBar.insert(new Element('a',{'class':'ctDelete','href':'http://my.dailyherald.com/comment/delete/?story='+currentComment.story+'&comment='+currentComment.id}));}
ctComment.insert(ctAbuseBar);}else if(isDeleted){ctCommentBody.addClassName('ctCommentAbuse');var abuseText=new Element('p');abuseText.insert('This comment removed for violating the ');abuseText.insert(new Element('a',{'href':'http://my.dailyherald.com/nfo/tos/'}).update('Terms of Service'));ctCommentBody.insert(abuseText);ctComment.insert(ctCommentBody);}else if(isVotedDown){var scoreText=new Element('p',{'class':(override?'status':'')});scoreText.insert('Voted Down (&nbsp;');if(override){scoreText.insert(new Element('a',{'href':'#','class':'ctShowHide'}).update('hide').observe('click',this.shrinkComment.bind(this,currentComment)).observe('click',function(e){e.stop();}));}else{scoreText.insert(new Element('a',{'href':'#','class':'ctShowHide'}).update('show').observe('click',this.expandComment.bind(this,currentComment)).observe('click',function(e){e.stop();}));}
scoreText.insert('&nbsp;)');ctCommentBody.insert(scoreText);if(override){var commentLines=new String(currentComment.comment).split('\n');for(var lineI=0;lineI<commentLines.length;lineI++){ctCommentBody.insert(new Element('p').update(commentLines[lineI]));}}
ctComment.insert(ctCommentBody);if(override){var ctAbuseBar=new Element('div',{'class':'ctAbuseBar'});ctAbuseBar.insert(new Element('a',{'class':'ctReportAbuse','href':'http://my.dailyherald.com/comment/report_abuse/?story='+currentComment.story+'&comment='+currentComment.id}));if(this.user&&this.user.deletePermission){ctAbuseBar.insert(new Element('a',{'class':'ctDelete','href':'http://my.dailyherald.com/comment/delete/?story='+currentComment.story+'&comment='+currentComment.id}));}
ctComment.insert(ctAbuseBar);}}else if(isBelowThreshold){var scoreText=new Element('p',{'class':(override?'status':'')});scoreText.insert('Below '+(this.user?'your':'default')+' threshold [&nbsp;');scoreText.insert(new Element(this.user?'a':'span',this.user?{'href':'http://my.dailyherald.com/account/?tab=memberservices&redirect='+escape(window.location)}:{}).update(this.commentsInfo.scoreThreshold));scoreText.insert('&nbsp;] (&nbsp;');if(override){scoreText.insert(new Element('a',{'href':'#','class':'ctShowHide'}).update('hide').observe('click',this.shrinkComment.bind(this,currentComment)).observe('click',function(e){e.stop();}));}else{scoreText.insert(new Element('a',{'href':'#','class':'ctShowHide'}).update('show').observe('click',this.expandComment.bind(this,currentComment)).observe('click',function(e){e.stop();}));}
scoreText.insert('&nbsp;)');ctCommentBody.insert(scoreText);if(override){var commentLines=new String(currentComment.comment).split('\n');for(var lineI=0;lineI<commentLines.length;lineI++){ctCommentBody.insert(new Element('p').update(commentLines[lineI]));}}
ctComment.insert(ctCommentBody);if(override){var ctAbuseBar=new Element('div',{'class':'ctAbuseBar'});ctAbuseBar.insert(new Element('a',{'class':'ctReportAbuse','href':'http://my.dailyherald.com/comment/report_abuse/?story='+currentComment.story+'&comment='+currentComment.id}));if(this.user&&this.user.deletePermission){ctAbuseBar.insert(new Element('a',{'class':'ctDelete','href':'http://my.dailyherald.com/comment/delete/?story='+currentComment.story+'&comment='+currentComment.id}));}
ctComment.insert(ctAbuseBar);}}
return ctComment;}}
