var CommentManager=Class.create({initialize:function(asset_id){this.asset_id=asset_id;this.treeze();$('comments').observe('click',this.click.bindAsEventListener(this));if((this.form=document.forms.comment_form)){this.default_in_reply=this.form.in_reply.value;this.form.reset_in_reply.observe('click',this.reset_in_reply.bindAsEventListener(this));}
$('asset_rate').observe('click',this.asset_rate_click.bindAsEventListener(this));this.rate_bar($('asset_rate').down('.current_rate'));},asset_rate_click:function(e){var el=e.element();if((el.hasClassName('rate_plus')||el.hasClassName('rate_minus'))){Utils.fetch('/rate_asset/'+this.asset_id,this.asset_rated.bind(this),new Hash({'rate':el.hasClassName('rate_plus')?1:-1}));}},click:function(e){var el=e.element();var comment_el;if((el.hasClassName('rate_up')||el.hasClassName('rate_down'))&&(comment_el=el.up('div.asset_comment'))){Utils.fetch('/rate_comment/'+this.asset_id,this.comment_rated.bind(this),new Hash({'comment':Utils.extract_number(comment_el.id),'rate':el.hasClassName('rate_up')?1:-1}));}else if((comment_el=el.up('div.asset_comment'))&&this.form){this.form.in_reply.value=comment_el.down('span.comment_user').firstChild.nodeValue+' @ '+comment_el.down('span.datetime').firstChild.nodeValue;this.form.in_reply_id.value=Utils.extract_number(comment_el.id);$(this.form.reset_in_reply).show();}},reset_in_reply:function(e){this.form.in_reply_id.value="";this.form.in_reply.value=this.default_in_reply;$(this.form.reset_in_reply).hide();},treeze:function(){$('comments').childElements().each(function(el){var rate=el.down('div.rate_it');rate.down('.rate_up').title=I18N.comment_rate_up;rate.down('.rate_down').title=I18N.comment_rate_down;this.rate_bar(rate.down('.current_rate'));var parent_comment=el.getAttribute('parent:id');if(parent_comment&&(parent_comment=$('comment_'+parent_comment))){parent_comment.appendChild(el.addClassName('child'));}}.bind(this));},rate_bar:function(el){var rate=Number(el.firstChild.nodeValue);el.update((rate>0?'+':'')+rate);if(rate>0){el.addClassName('current_rate_positive');}else if(rate<0){el.addClassName('current_rate_negative');}},comment_rated:function(response){if(!('error' in response)){var el;if((el=$('comment_'+response.id))){this.rate_bar(el.down('.current_rate').writeAttribute('class','current_rate').update(response.rate));}}else{alert(I18N.error+': '+response.error);}},asset_rated:function(response){if(!('error' in response)){var el;if((el=$('asset_rate'))){this.rate_bar(el.down('.current_rate').writeAttribute('class','current_rate').update(response.rate));}}else{alert(I18N.error+': '+response.error);}},});