
var comments={'reply_to_comment_id':{},'edit_comment_id':null,'active_message_id':null,'create':function(shared_name,destination,show,number,collaborated,need_to_show_share_folder)
{var item=Canvas.get().collections[number].find(shared_name.substr(2));var comments_block=$j('#comments_tpl').get(0).cloneNode(true);$j(destination).empty();comments.parse(comments_block,(shared_name+'_'+number));comments_block.id=comments_block.id.replace('tpl',shared_name+'_'+number);comments_block.style.display='block';var close_link=$j(comments_block).find('#cmtclose_tpl').get(0);if(close_link)
{close_link.id='cmtclose_'+shared_name+'_'+number;}
if(collaborated)
{$j(comments_block).find('#comments_bottom_text_'+shared_name+'_'+number).show().find('b').text(collaborated);}
if(need_to_show_share_folder&&!Canvas.get().user.is_subuser&&Canvas.get().current_folder)
{$j(comments_block).find('#comments_bottom_text_private_'+shared_name+'_'+number).show().find('a').click(function(){box_event.set_category('Files - Comments menu');Canvas.get().current_folder.collaborate();return false;});}
destination.appendChild(comments_block);$j(destination).find('#cmtlst_'+shared_name+'_'+number).get(0).style.display='none';var t=shared_name.substr(0,2);var textarea=$j(destination).find('#cmttxt_'+shared_name+'_'+number);var limit=150;if(textarea.length)
{new AutoresizeTextarea({scroll_control:{height_object:'#cmtlst_'+shared_name+'_'+number,scroll_object:'#comments_'+shared_name+'_'+number},destination:textarea.get(0),default_value:$t('Enter your comment here','js_comments_js_input_message')});}
if(t=='d_')
{textarea.keydown(function(e){if(-1!=$j.inArray(e.keyCode,[8,9,17,35,36,37,38,39,40,46]))return;if(this.value.length>limit)return false;return true;}).keyup(function(){if(this.value.length>limit)this.value=this.value.substr(0,limit);})}
if(show)item.load();},'get_parent_shared_name':function(shared_name,number)
{if(typeof parent_shared_name!='undefined')
{return parent_shared_name;}
var comments_list_id=shared_name+'_'+number;var comments_list_full_id='cmtlst_'+comments_list_id;var j_comments_list=$j('#'+comments_list_full_id);var j_update=j_comments_list.parents('.update');if(j_update.size())
{var update_id=j_update.slice(0,1).attr('id').substr(11);var update=(typeof(oInbox)!='undefined')?oInbox[update_id]:oUpdates[update_id];if(update.folder)
{return update.folder.shared_name;}}
return null;},'show':function(shared_name,number,messages,is_owner,new_comments,params)
{params=params||{};comments.edit_comment_id=null;messages=messages||{};var comments_list_id=shared_name+'_'+number;var comments_list_full_id='cmtlst_'+comments_list_id;var j_comments_list;if($j('#gallery_main').is(':visible')&&!params['from_submit'])
{j_comments_list=$j('#gallery_comments_block_temp').find('#'+comments_list_full_id);}
else
{j_comments_list=$j('#'+comments_list_full_id);}
if(!j_comments_list.size())
{comments_list_full_id='cmtlst_c'+number+'_'+comments_list_id;j_comments_list=$j('#'+comments_list_full_id);}
if(!j_comments_list.size())return;var j_new_comment=j_comments_list.find('div.comments_table');j_new_comment.appendTo(j_comments_list);j_comments_list.find('div.comment').remove();if(messages.toString())
{var i;var level=0;for(i in messages)
{var j_comment=comments.create_comment(shared_name,number,messages[i],is_owner,new_comments,level);j_comment.appendTo(j_comments_list);}}
j_comments_list.show();var j_comment_to_reply=null;if(comments.reply_to_comment_id[comments_list_full_id])
{j_comment_to_reply=$j('#'+comments.reply_to_comment_id[comments_list_full_id]);}
if(j_comment_to_reply&&j_comment_to_reply.size())
{comments.reply(j_comment_to_reply.find('.comment_reply_link').get(0));}
else
{comments.reply(j_comments_list.parent().find('.reply_at_the_end').get(0),true);}
var count=j_comments_list.find('.comment').size();var j_folder_talk_disabled=j_comments_list.siblings('div.folder_talk_disabled');if(j_folder_talk_disabled.size()==1)
{j_folder_talk_disabled.show();j_new_comment.hide();}
var j_last_comments_message=j_comments_list.siblings('div.last_comments_message');if(new_comments&&count<new_comments)
{j_last_comments_message.show();}
else
{j_last_comments_message.hide();}
var j_item=j_comments_list.parents('.item');j_item[(count>0)?'addClass':'removeClass']('has_comments');var comments_label=(count>0?count:'');var j_comments_menu=j_item.find('.comments_menu span');j_comments_menu.html(comments_label);if($j('#gallery_main').is(':visible'))
{j_comments_list.find('textarea').get(0).blur();$j('#gallery_comments_part').show();}},'create_comment':function(shared_name,number,comment_params,is_owner,new_comments,level)
{var j_comment=$j('#cmtmgs_tpl').clone();var id='m'+comment_params.message_id+'_'+number;j_comment.attr('id',id);var from_user;from_user=Canvas.get().users.find(comment_params.from_user_id);if((from_user)&&(from_user.id!=2)&&user_id!=2)
{j_comment.find('.author').html(from_user.display_name({profile_link:true}));}
else
{var name=comment_params.from_user;if(comment_params.from_user.length>20)
{name=get_short_string(comment_params.from_user,170,j_comment.find('.author'),1);}
j_comment.find('.author').text(name);}
var cmt_delete_id='cmtdel_'+shared_name+'_'+comment_params.message_id+'_'+number;j_comment.find('.date').text(comment_params.date+' at '+comment_params.time);if(new_comments&&comment_params['new'])
{j_comment.find('.date').append('<span class="date_new">'+$t('New','comments_js_new')+'</span>');}
var image='url('+images_url+'box_user_avatar_small.png)';if(from_user&&from_user.pic_urls)
{image='url('+from_user.pic_urls.small+')';}
j_comment.find('.avatar').css('background-image',image);j_comment.find('.comment_delete').attr('id',cmt_delete_id);if(Canvas.get().user.is_subuser)
{j_comment.find('#'+cmt_delete_id).hide();j_comment.find('.comment_edit').hide();}
var max_level_for_comment_reply=0;if(level>max_level_for_comment_reply)
{j_comment.find('.comment_reply_link').remove();}
comment_params.message=comment_params.message.replace(/\n/g,'<br />');j_comment.find('.message').html(comment_params.message);if((comment_params.from_user_id!=user_id||comment_params.from_user_id==2)&&!is_owner)
{j_comment.find('#'+cmt_delete_id).css('visibility','hidden');}
if(comment_params.from_user_id!=user_id||comment_params.from_user_id==2)
{j_comment.find('.comment_edit').css('visibility','hidden');}
var j_reply_comments=j_comment.find('.reply_comments');for(var i in comment_params.reply_comments)
{var j_reply_comment=comments.create_comment(shared_name,number,comment_params.reply_comments[i],is_owner,new_comments,level+1);j_reply_comment.appendTo(j_reply_comments);}
if(comment_params.reply_comments.length==0)
{j_reply_comments.css({'border':0});j_comment.children('.comment_reply_to_this_thread').remove();}
else
{j_comment.children('.comment_reply_to_this_message').remove();}
j_comment.css('display','block');return j_comment;},'reply':function(obj,reply_at_the_end)
{comments.edit_comment_id=null;var j_comment_reply=$j(obj);var j_comment;var j_reply_at_the_end;var j_comments_list;var j_reply_comments;var margin_left;var level;if(reply_at_the_end)
{j_reply_at_the_end=j_comment_reply;j_comments_list=j_reply_at_the_end.siblings('.comments');j_reply_comments=j_comments_list;margin_left=0;comments.reply_to_comment_id[j_comments_list.attr('id')]=null;level=0;}
else
{j_comment=$j(obj).parent('.comment');j_comments_list=j_comment.parents('.comments');j_reply_at_the_end=j_comments_list.next('.comment_reply');j_reply_comments=j_comment.children('div.reply_comments');margin_left=j_comment.css('margin-left');margin_left=isNaN(margin_left)?0:parseInt(margin_left.substr(0,margin_left.length-2));comments.reply_to_comment_id[j_comments_list.attr('id')]=j_comment.attr('id');level=j_comment.parents('.comment').size()+1;}
j_comments_list.find('.message').show();j_comments_list.find('.message_area').show();j_comments_list.parents('.item_expansion').css('height','');var j_new_comment=j_comments_list.find('div.comments_table');j_new_comment.removeClass('comments_table_no_reply');var j_new_comment_input=j_new_comment.find('textarea');j_new_comment.css('margin-left',margin_left+'px');j_new_comment.appendTo(j_reply_comments);var s=485;if($j('#gallery_main').is(':visible'))
{s=620;j_new_comment_input.css('margin-right',0).css('margin-bottom',10);}
j_new_comment_input.css('width',(s-level*55)+'px');j_new_comment_input.val('');j_new_comment_input.focus();j_comments_list.parent().find('.comment_reply').show();j_comment_reply.parent().children('.comment_reply').hide();j_comment_reply.parent().children('.comment_edit').show();},'edit':function(obj)
{var j_comment_area=$j(obj).parent().parent();var j_all_comments_area=j_comment_area.parents('.comments');var tmp=j_comment_area.attr('id');var tmp=tmp.split('_');comments.edit_comment_id=tmp[0].substr(1,tmp[0].length);var j_new_comment=j_comment_area.parents('.comments').find('div.comments_table');var j_new_comment_input=j_new_comment.find('textarea');var j_comment_current_message=j_comment_area.find('.message:eq(0)');var j_new_comment_submit_button_text=j_new_comment.find('.btn_action span span');var current_comment_message=j_comment_current_message.html().replace(/<br>/gi,"\n");j_all_comments_area.find('.message').show();j_all_comments_area.find('.message_area').show();j_comment_current_message.hide();j_comment_current_message.parent('.message_area').hide();var level=$j(obj).parent().parent('.comment').parents('.comment').size()+1;var s=485;if($j('#gallery_main').is(':visible'))
{s=620;j_new_comment_input.css('margin-right',0).css('margin-bottom',10);}
j_new_comment_input.css('width',(s-level*55)+'px');j_new_comment.addClass('comments_table_no_reply');var edit_comment_block=j_comment_area.find('.edit_comment:eq(0)');var margin_left=j_comment_area.css('margin-left');j_new_comment.css('margin-left',margin_left);edit_comment_block.append(j_new_comment);j_new_comment_input.focus();j_new_comment_input.val(current_comment_message);j_all_comments_area.parent().find('.comment_reply').show();j_comment_area.find('.comment_reply:eq(1)').hide();},'close':function(obj)
{var tmp=obj.id.split('_');var shared_name=tmp[1]+'_'+tmp[2];var number=tmp[3];$j('#cmt_'+shared_name+'_'+number).hide();},'send':function(obj)
{if($j(obj).hasClass('comment_submit_button_disabled'))return false;var tmp=obj.id.split('_');var textarea;var author_name;if(tmp[0]=='cmtsbm')
{textarea=$j('#'+obj.id.replace('cmtsbm','cmttxt'));author_name=$j('#'+obj.id.replace('cmtsbm','cmtname'));}
else
{textarea=$j('#'+obj.id.replace('cmtfrm','cmttxt'));author_name=$j('#'+obj.id.replace('cmtsbm','cmttxt'));}
if(tmp[1].substr(0,1)=='c')tmp=tmp.slice(1);var shared_name=tmp[1]+'_'+tmp[2];var number=tmp[3];var tvalue=$t('Enter your comment here','js_comments_js_input_message');$j('#last_comments_message_'+shared_name+'_'+number).css('display','');var message_error=($j.trim(textarea.val())==''||tvalue==$j.trim(textarea.val()));var avalue=$t('Enter your name here','js_comments_js_input_name_message');var author_error=(!user_login&&($j.trim(author_name.val())==''||avalue==$j.trim(author_name.val())));if(message_error||author_error)
{if(message_error&&author_error)
{alert($t('Please enter author name and comment.','comments_js_message_error_author_error'));return false;}
if(message_error)
{alert($t('Please enter comment.','comments_js_message_error'));}
if(author_error)
{alert($t('A name is required to make a comment.','comments_js_author_error'));}
return false;}
textarea.readOnly=true;var query={message:$j.trim(textarea.val()),get_messages:1};textarea.attr('disabled','disabled');switch(shared_name.substr(0,2))
{case'f_':var item=Item.find(shared_name.substr(2));query.file_id=shared_name.substr(2);if(item&&item.shared_name)query.shared_name=item.shared_name;break;case'd_':query.folder_id=shared_name.substr(2);break;case'l_':query.web_link_id=shared_name.substr(2);break;case't_':query.discussion_id=shared_name.substr(2);break;case's_':query.shared_name=shared_name.substr(2);break;}
var parent_shared_name=comments.get_parent_shared_name(shared_name,number);if(parent_shared_name)
{query.parent_shared_name=parent_shared_name;}
else if(typeof(collab_invite_token)!='undefined')
{query.collab_invite_token=collab_invite_token;}
if(!user_login)
{query.author=$j.trim(author_name.val());}
var j_comment=$j(obj).parents('.comment');if(j_comment.size())
{var parent_comment_id=j_comment.attr('id');parent_comment_id=parent_comment_id.substr(1,parent_comment_id.indexOf('_')-1);query.parent_comment_id=parent_comment_id;}
var url=site_root+'index.php?rm=box_comments_add';if(comments.edit_comment_id)
{query.edit_comment_id=comments.edit_comment_id;url=site_root+'index.php?rm=box_comments_edit';}
var q=$j.box.formatQuery(query);$j.post(url,q,function(json)
{if(json.result!=1)return;textarea.readOnly=false;textarea.blur();textarea.css('height',16);textarea.val(tvalue).removeAttr('disabled');if(!user_login&&avalue)
{author_name.val(avalue);}
textarea.blur();$j('#cmtsbm_'+shared_name+'_'+number).removeClass('comment_submit_button_disabled').removeAttr('disabled');comments.show(shared_name,number,json.messages,json.is_owner,json.show_new,{from_submit:true});},'json');$j('#cmtsbm_'+shared_name+'_'+number).addClass('comment_submit_button_disabled').attr('disabled','disabled');$j('#cmttxt_'+shared_name+'_'+number).css('height',36);},'delete_one':function(obj)
{comments.active_message_id=obj.id;var reply_comment_count=$j(obj).parent('.comment').find('.comment').size();popup=$j.popup.form($t('Delete comment','comments_popup_delete_title'),[['text',$t('Are you sure you want to delete this comment?','comments_popup_delete_text')],reply_comment_count==0?[]:['text',$t('The %1 replies to this comment will also be deleted.','comments_popup_delete_reply_comments_text',reply_comment_count)],{type:'buttons',items:{okay:[$t('Okay','js_popup_button_okay'),function(params)
{comments.message_delete();return false;},true],cancel:[$t('Cancel','js_popup_button_cancel'),function(params){$j.popup.close();}]}}]);},'message_delete':function()
{var tmp=comments.active_message_id.split('_');var shared_name=tmp[1]+'_'+tmp[2];var message_id=tmp[3];var number=tmp[4];var query={message_id:message_id,get_messages:1};switch(shared_name.substr(0,2))
{case'f_':query.file_id=shared_name.substr(2);var item=Item.find(shared_name.substr(2));if(item&&item.shared_name)
{query.shared_name=item.shared_name;}
break;case'd_':query.folder_id=shared_name.substr(2);break;case'l_':query.web_link_id=shared_name.substr(2);break;case't_':query.discussion_id=shared_name.substr(2);break;case's_':query.shared_name=shared_name.substr(2);break;}
var parent_shared_name=comments.get_parent_shared_name(shared_name,number);if(parent_shared_name)
{query.parent_shared_name=parent_shared_name;}
var q=$j.box.formatQuery(query);var url=site_root+'index.php?rm=box_comments_delete';$j.getJSON(url,q,function(json)
{if(json.result!=1)return;comments.show(shared_name,number,json.messages,json.is_owner,'',{from_submit:true});var comments_list_id=shared_name+'_'+number;$j.popup.close();});},'parse':function(obj,value)
{$j('*',obj).each(function()
{this.id=this.id.replace('tpl',value);});},'redirect':function(link)
{setCookie('box_redirect_url',document.location,null,'/',document.domain,0);document.location=link.href;}}