var MiniChat={icons:$H(),pe:$H(),init:function(){document.observe('dom:loaded',function(e){this.initDeleteForm();this.initBlock();if($('minichat-form-add')){$('minichat-form-add').observe('submit',this.submitAddForm.bind(this));}
if($('minichat-form-refresh')){$('minichat-form-refresh').observe('submit',this.submitRefreshForm.bind(this));}}.bind(this));},initBlock:function(){if($('minichat-block')){toggleLink=$('minichat-block').select('.maintitle .toggle');if(toggleLink.size()){if(ipb.Cookie.get('MiniChatHide')=='1'){$('minichat-block').down('h2').addClassName('collapsed');$('minichat-block-wrapper').hide();}
toggleLink.first().observe('click',function(e){Event.stop(e);title=Event.element(e).up('h2');Effect.toggle($('minichat-block-wrapper'),'blind',{duration:0.4});if(title.hasClassName('collapsed')){title.removeClassName('collapsed');ipb.Cookie.set('MiniChatHide','0',1);}
else{new Effect.Morph(title,{style:'collapsed',duration:0.4});ipb.Cookie.set('MiniChatHide','1',1);}});}}},initDeleteForm:function(){if(MiniChat.icons['delete_msg_on']&&MiniChat.icons['delete_msg_off']){$$('.minichat-delete-button').each(function(element){element.observe('mouseover',function(event){element.src=MiniChat.icons['delete_msg_on'];});element.observe('mouseout',function(event){element.src=MiniChat.icons['delete_msg_off'];});});}},submitAddForm:function(e){Event.stop(e);this.showMessage($('minichat-form-add'),'minichat-message-add','info',ipb.lang['minichat_sending_message']);$('minichat-form-add').request({evalJSON:'force',parameters:{module:'ajax',section:'messages',message:$F('minichat-field-message').encodeParam()},onSuccess:function(response){if(response.responseJSON['result']){$('minichat-field-message').clear();this.submitRefreshForm(null,false);this.showMessage($('minichat-form-add'),'minichat-message-add','success',ipb.lang['minichat_message_added']);}else{this.showMessage($('minichat-form-add'),'minichat-message-add','error',(response.responseJSON['error']?response.responseJSON['error']:ipb.lang['minichat_error_unknown']));}}.bind(this)});},submitRefreshForm:function(e,showMessage){showMessage=showMessage==false?false:true;if(e){Event.stop(e);}
if(showMessage){this.showMessage($('minichat-form-refresh'),'minichat-message-refresh','info',ipb.lang['minichat_refreshing'],'top',0);}
new Ajax.Request(ipb.vars['base_url'],{parameters:{app:'minichat',module:'ajax',section:'messages',action:'get-messages',md5check:ipb.vars['secure_hash'],url:$('minichat-form-refresh').action},onSuccess:function(response){if(response.responseJSON&&response.responseJSON['error']){if(showMessage){this.showMessage($('minichat-form-refresh'),'minichat-message-refresh','error',(response.responseJSON['error']?response.responseJSON['error']:ipb.lang['minichat_error_unknown']),'top',15);}}else{$('minichat-messages').update(response.responseText);if(showMessage){this.showMessage($('minichat-form-refresh'),'minichat-message-refresh','success',ipb.lang['minichat_list_refreshed'],'top');}
this.initDeleteForm();}}.bind(this)});},hideMessage:function(e){new Effect.Fade(this,{duration:0.4,afterFinish:function(){this.remove();}.bind(this)});e.stop();},showMessage:function(element,id,type,message,position,autohide){if(element&&id){autohide=autohide?autohide:5;html=ipb.templates['minichat_message'].evaluate({id:id,type:type,message:message});if($(id)){$(id).remove();if(this.pe.get(id)){this.pe.get(id).stop();this.pe.unset(id);}}
switch(position){case'before':element.insert({before:html});break;case'after':element.insert({before:html});break;case'top':element.insert({top:html});break;default:element.insert({bottom:html});}
if($(id)&&autohide>0){this.pe.set(id,new PeriodicalExecuter(this.hideMessage.bind($(id)),autohide));}}else{alert(message);}}};