/*!
 * This file is a part of Mibew Messenger.
 *
 * Copyright 2005-2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
!function(e){e.Objects.Models.Controls={},e.Objects.Models.Status={};var s=[],t=e.Application,o=t.module("Chat",{startWithParent:!1});o.addInitializer(function(o){var a=e.Objects,n=e.Objects.Models,l=e.Objects.Models.Controls,r=e.Objects.Models.Status;o.page&&n.page.set(o.page),n.thread=new e.Models.Thread(o.thread),n.user=new e.Models.ChatUser(o.user);var d=new e.Layouts.Chat;a.chatLayout=d,t.mainRegion.show(d);var i=new e.Collections.Controls;n.user.get("isAgent")||(l.userName=new e.Models.UserNameControl({weight:220}),i.add(l.userName),l.sendMail=new e.Models.SendMailControl({weight:200,link:o.links.mail,windowParams:o.windowsParams.mail}),i.add(l.sendMail)),n.user.get("isAgent")&&(l.redirect=new e.Models.RedirectControl({weight:200,link:o.links.redirect}),i.add(l.redirect),l.history=new e.Models.HistoryControl({weight:180,link:o.links.history,windowParams:o.windowsParams.history}),i.add(l.history)),l.sound=new e.Models.SoundControl({weight:160}),i.add(l.sound),l.refresh=new e.Models.RefreshControl({weight:140}),i.add(l.refresh),o.links.ssl&&(l.secureMode=new e.Models.SecureModeControl({weight:120,link:o.links.ssl}),i.add(l.secureMode)),l.close=new e.Models.CloseControl({weight:100}),i.add(l.close),a.Collections.controls=i,d.controlsRegion.show(new e.Views.ControlsCollection({collection:i})),r.message=new e.Models.StatusMessage({hideTimeout:5e3}),r.typing=new e.Models.StatusTyping({hideTimeout:5e3}),a.Collections.status=new e.Collections.Status([r.message,r.typing]),d.statusRegion.show(new e.Views.StatusCollection({collection:a.Collections.status})),n.user.get("isAgent")||(n.avatar=new e.Models.Avatar({imageLink:o.avatar||!1}),d.avatarRegion.show(new e.Views.Avatar({model:n.avatar}))),a.Collections.messages=new e.Collections.Messages,n.messageForm=new e.Models.MessageForm(o.messageForm),d.messageFormRegion.show(new e.Views.MessageForm({model:n.messageForm})),d.messagesRegion.show(new e.Views.MessagesCollection({collection:a.Collections.messages})),n.soundManager=new e.Models.ChatSoundManager,!n.user.get("isAgent")&&o.links.chat&&window.parent&&window.parent.postMessage&&window.parent!==window&&window.parent.postMessage("mibew-chat-started:"+window.name+":"+o.links.chat,"*"),s.push(a.server.callFunctionsPeriodically(function(){var s=e.Objects.Models.thread,t=e.Objects.Models.user;return[{"function":"update",arguments:{"return":{threadState:"threadState",threadAgentId:"threadAgentId",typing:"typing",canPost:"canPost"},references:{},threadId:s.get("id"),token:s.get("token"),lastId:s.get("lastId"),typed:t.get("typing"),user:!t.get("isAgent")}}]},function(s){return s.errorCode?void e.Objects.Models.Status.message.setMessage(s.errorMessage||"refresh failed"):(s.typing&&e.Objects.Models.Status.typing.show(),e.Objects.Models.user.set({canPost:s.canPost||!1}),void e.Objects.Models.thread.set({agentId:s.threadAgentId,state:s.threadState}))}))}),o.on("start",function(){e.Objects.server.restartUpdater()}),o.addFinalizer(function(){e.Objects.chatLayout.destroy();for(var t=0;t<s.length;t++)e.Objects.server.stopCallFunctionsPeriodically(s[t]);"undefined"!=typeof e.Objects.Models.avatar&&e.Objects.Models.avatar.finalize(),e.Objects.Collections.messages.finalize(),delete e.Objects.chatLayout,delete e.Objects.Models.thread,delete e.Objects.Models.user,delete e.Objects.Models.page,delete e.Objects.Models.avatar,delete e.Objects.Models.messageForm,delete e.Objects.Models.Controls,delete e.Objects.Models.Status,delete e.Objects.Collections.messages,delete e.Objects.Collections.controls,delete e.Objects.Collections.status})}(Mibew);