Ext.ns("calislv.chat");var tx_calischat_labels={chatTitle:"Cāļčats",commonChatTitle:"Galvenais čats",friendsPanelTitle:"Draugi čatā",infoPanelFormat:"Cāļčats ({0}/{1})",inputEmptyText:'Uzraksti kaut ko un nospied "Enter"!',loginPrompt:"Jums jāielogojās sistēmā, ja vēlaties rakstīt čatā.",noFriends:'<div class="cchat-nousers">Neviena tava <a ext:qtip="Visi tavi draugi" href="{0}" target="_blank">drauga</a> pašlaik čatā nav.</div>',noUsers:'<div class="cchat-nousers">Pašlaik neviena lietotāja čatā nav.</div>',onlinePanelTitle:"Tagad čatā",qtipInfo:"Lietotāja informācija",qtipPrivate:"Privāta saruna",wait:"Lūdzu, uzgaidiet..."};calislv.chat.UsersPanel=Ext.extend(Ext.Panel,{autoWidth:true,barCountIndex:1,barCounts:[0,0],bodyBorder:false,emptyMessage:tx_calischat_labels.noUsers,filter:"all",layout:"fit",reloadTask:undefined,store:undefined,bodyStyle:"overflow-y: auto",title:tx_calischat_labels.onlinePanelTitle,createItems:function(){var a=new Ext.XTemplate('<table class="cchat-usertable" cellspacing="0"><tpl for="."><tr class="cchat-user"><td class="cchat-imagecol"><img alt="" height="{h}" ext:qtip="&lt;img alt=&quot;&quot; height=&quot;{fh}&quot; src=&quot;{if}&quot; width=&quot;{fw}&quot; /&gt;&lt;br /&gt;{un}" src="{is}" width="{w}" /></td><td class="cchat-namecol">{un}</td><td class="cchat-ctrlcol"><a class="cchat-priv" ext:qtip="'+tx_calischat_labels.qtipPrivate+'" href="javascript:void(0)" onclick="chatWindow.openPrivateChat({uid},\'{un}\',true)"<tpl if="!this.allowPrivateChat(uid)"> style="visibility:hidden"</tpl>>&nbsp;</a><a class="cchat-info" ext:qtip="'+tx_calischat_labels.qtipInfo+'" href="{ul}" target="_blank">&nbsp;</a></td></tr></tpl></table>',{allowPrivateChat:function(b){return(chatOptions.userId>0&&chatOptions.userId!=b)}});a.compile();this.items={xtype:"dataview",autoWidth:true,emptyText:this.emptyMessage,itemSelector:"tr.cchat-user",store:this.store,tpl:a}},createReloadTask:function(){this.reloadTask=new Ext.util.DelayedTask(this.refreshUsers,this)},createStore:function(){this.store=new Ext.data.JsonStore({autoLoad:false,baseParams:{action:"getusers",eID:"calis_chat",filter:this.filter,id:chatOptions.id,protocol:1},fields:["uid","un","h","is","if","fw","fh","ul","w"],root:"users",url:"/index.php"});this.store.addListener("load",function(b,a){this.barCounts[this.barCountIndex]=a.length;this.updateBarCounts();this.reloadTask.delay(45000+10000*Math.random())},this);this.store.addListener("loadexception",function(b,a){this.reloadTask.delay(45000+10000*Math.random())},this)},initComponent:function(){this.createReloadTask();this.createStore();this.createItems();calislv.chat.UsersPanel.superclass.initComponent.call(this)},refreshUsers:function(){this.store.reload()},updateBarCounts:function(){}});calislv.chat.FriendsPanel=Ext.extend(calislv.chat.UsersPanel,{barCountIndex:0,emptyMessage:String.format(tx_calischat_labels.noFriends,chatOptions.friendsURL),filter:"friends",title:tx_calischat_labels.friendsPanelTitle});calislv.chat.LinesPanel=Ext.extend(Ext.Panel,{autoScroll:true,bodyBorder:false,closable:false,form:undefined,id:Ext.id(this.body,"chat-lines-"),isActive:false,isLoading:true,layout:"border",linesPanel:undefined,loadTask:undefined,recordConstructor:undefined,store:undefined,updateDelays:{activated:15000,deactivated:45000},userId:0,createInputForm:function(){var a=this;this.form=new Ext.form.FormPanel({baseParams:{action:"post",eID:"calis_chat",id:chatOptions.id,userId:this.userId},bodyBorder:false,height:32,items:[{xtype:"textfield",emptyText:tx_calischat_labels.inputEmptyText,enableKeyEvents:true,hideLabel:true,id:this.id+"-text",listeners:{specialkey:function(c,b){if(b.keyCode==b.ENTER){a.submitForm.call(a)}}},maxLength:256,name:"lineraw",style:"display: block; margin: 5px auto;width: 90%"},{xtype:"hidden",id:this.id+"-line",name:"line",value:""}],region:"south",url:"/index.php"});return this.form},createItems:function(){var a=new Ext.XTemplate('<tpl for="."><p class="cchat-line-'+this.id+'"><b><a href="javascript:void(0)" onclick="Ext.getCmp(\''+this.id+"').putName('{un}')\">{un}</a></b>: {[this.preProcessLine(values.t,values.uid)]}</p></tpl>",{convertSmileCodes:function(b){var c=chatOptions.chatPath+"resources/images/emotions/";b=b.replace(/:-?\)/g,'<img alt="" height="23" src="'+c+'01.png" width="23" />');b=b.replace(/;-?\)/g,'<img alt="" height="24" src="'+c+'13.png" width="23" />');b=b.replace(/:-?\(/g,'<img alt="" height="24" src="'+c+'24.png" width="23" />');b=b.replace(/:-?[0o]/ig,'<img alt="" height="24" src="'+c+'15.png" width="24" />');b=b.replace(/:-?D/g,'<img alt="" height="23" src="'+c+'21.png" width="24" />');b=b.replace(/:-?p/ig,'<img alt="" height="24" src="'+c+'31.png" width="24" />');return b},preProcessLine:function(c,d){var b=(d!=0);if(b){c=Ext.util.Format.htmlEncode(c)}c=this.convertSmileCodes(c);return c}});a.compile();this.linesPanel=new Ext.Panel({xtype:"panel",autoScroll:true,bodyBorder:false,bodyStyle:"padding: 10px 0",id:"chat-scroller-"+this.id,items:{xtype:"dataview",itemSelector:"tr.cchat-line-"+this.id,store:this.store,tpl:a},region:"center"});this.items=[this.linesPanel];if(chatOptions.userId>0){this.items.push(this.createInputForm())}else{this.items.push(this.createLoginText())}},createLoadTask:function(){this.loadTask=new Ext.util.DelayedTask(this.loadNewLines,this)},createLoginText:function(){return{xtype:"panel",bodyBorder:false,bodyStyle:"padding-top: 10px",height:32,html:tx_calischat_labels.loginPrompt,region:"south"}},createStore:function(){this.recordConstructor=Ext.data.Record.create([{name:"id",type:"int"},{name:"uid",type:"int"},"un","on","t"]);this.store=new Ext.data.JsonStore({autoLoad:(this.userId!=0),baseParams:{action:"getlines",eID:"calis_chat",id:chatOptions.id,protocol:1,start:0,userId:this.userId},fields:this.recordConstructor,listeners:{load:function(b,a){this.scrollToBottom();this.isLoading=false;this.loadTask.delay(this.getUpdateDelay())},loadexception:function(){this.isLoading=false;this.loadTask.delay(this.getUpdateDelay())},scope:this},root:"lines",url:"/index.php"})},getUpdateDelay:function(){return this.isActive?this.updateDelays.activated:this.updateDelays.deactivated},containsUrl:function(a){a=a.toLowerCase();if(a.indexOf("http://")>=0||a.indexOf("https://")>=0||a.match(/www\s*\./,a)||a.match(/\.\s*(com|lv|ru|net|org|cc|to)/,a)){return true}return false},initComponent:function(){this.closable=(this.userId!=0);this.isActive=(this.userId==0);this.createStore();this.createItems();this.addEvents("linesupdated");this.on("activate",this.onActivate,this);this.on("deactivate",this.onDeactivate,this);if(this.closable){Ext.getCmp("chat-tabs").on("remove",this.onRemove,this)}calislv.chat.LinesPanel.superclass.initComponent.call(this,arguments);this.createLoadTask()},loadNewLines:function(){this.isLoading=true;var d=this.store.getCount();while(--d>=0){var b=this.store.getAt(d);var a=b.get("id");if(a>0){this.store.baseParams.start=a;break}}var c=this;this.store.load({add:true,callback:function(e){this.removeOldRecordsFromStore();this.scrollToBottom();this.isLoading=false;this.loadTask.delay(this.getUpdateDelay());if(this.userId!=0&&e.length>0){this.fireEvent("linesupdated",this)}},scope:c})},onActivate:function(a){if(this==a){this.isActive=true;this.scrollToBottom();if(!this.isLoading){this.loadTask.delay(100)}}},onDeactivate:function(a){if(this==a){this.isActive=false}},onRemove:function(b,a){if(a==this){this.loadTask.cancel();Ext.Ajax.request({failure:Ext.emptyFn,params:{action:"closechat",eID:"calis_chat",id:chatOptions.id,protocol:1,start:0,userId:this.userId},success:Ext.emptyFn,url:"/index.php"})}},putName:function(b){var c=this.form.getForm();var a=c.getValues();var d=a.lineraw;if(d==tx_calischat_labels.inputEmptyText){d=""}else{d=textArea.trim()}a.lineraw=(d!=""?d+" ":"")+b+", ";c.setValues(a);Ext.getCmp(this.id+"-text").focus()},removeOldRecordsFromStore:function(){var a=this.userId==0?100:1000;var b=this.store.getCount()-a;if(b>0){this.store.suspendEvents();while(b--){this.store.removeAt(0)}this.store.resumeEvents()}},scrollToBottom:function(){var a=Ext.getCmp("chat-scroller-"+this.id);if(a&&a.body){a.body.scroll("bottom",2000)}},submitForm:function(){var c=this.form.getForm();var b=c.getValues();var d=b.lineraw.trim();if(d!=""){if(chatOptions.userAge>=7||!this.containsUrl(d)){b.line=d;b.lineraw="";c.setValues(b);c.submit()}else{b.lineraw="";c.setValues(b)}var a=new this.recordConstructor({id:-1,uid:chatOptions.userId,un:chatOptions.userName,on:this.userId,t:d});this.store.add([a]);this.store.commitChanges();this.removeOldRecordsFromStore();this.scrollToBottom()}}});var chatWindow;calislv.chat.Chat=Ext.extend(Ext.Panel,{dataLoaded:false,hidden:true,iconCls:"chat-icon",isHidden:true,pollTask:undefined,jumpTask:undefined,layout:"border",renderTo:"cchat-panel",stores:{friendsStore:null,usersStore:null,commonLinesStore:null},title:tx_calischat_labels.chatTitle,beforeShow:function(){Ext.get(this.renderTo).show();if(!this.dataLoaded){this.initiateDataLoading();this.dataLoaded=true}this.isHidden=false;this.jumpTask.cancel();return true},bounceBar:function(){if(this.isHidden){Ext.get("cchat-bar").highlight();this.jumpTask.delay(2000)}},checkForNewPrivateTalks:function(){Ext.Ajax.request({failure:function(){this.pollTask.delay(60000)},params:{action:"poll",eID:"calis_chat",id:chatOptions.id,protocol:1},success:function(response){try{var data=eval("("+response.responseText+")");if(data&&data.open&&data.open.length>0){this.openPrivateChatList(data.open)}}catch(e){}this.pollTask.delay(51000)},scope:this,url:"/index.php"})},createCommonLinesPanel:function(){var a=new calislv.chat.LinesPanel({title:tx_calischat_labels.commonChatTitle});this.stores.commonLinesStore=a.store;return a},createItems:function(){this.items=[this.createUsersPanel(),this.createLinesPanel()]},createFriendsPanel:function(){var a=new calislv.chat.FriendsPanel();this.stores.friendsStore=a.store;return a},createLinesPanel:function(){var a=[this.createCommonLinesPanel()];return{xtype:"tabpanel",activeItem:0,bodyBorder:false,bodyStyle:"padding: 5px",enableTabScroll:true,id:"chat-tabs",items:a,region:"center",split:true}},createOnlinePanel:function(){var a=new calislv.chat.UsersPanel();this.stores.usersStore=a.store;return a},createTools:function(){this.tools=[{id:"minimize",handler:function(){this.hide();this.onHide()},scope:this}]},createUsersPanel:function(){var a=[];if(chatOptions.userId!=0){a.push(this.createFriendsPanel())}a.push(this.createOnlinePanel());return{xtype:"tabpanel",activeTab:0,bodyBorder:false,items:a,region:"west",split:true,width:200}},initComponent:function(){this.createItems();this.createTools();this.setInitialHeight();this.registerListeners();this.jumpTask=new Ext.util.DelayedTask(this.bounceBar,this);this.pollTask=new Ext.util.DelayedTask(this.checkForNewPrivateTalks,this);calislv.chat.Chat.superclass.initComponent.call(this,arguments)},initiateDataLoading:function(){if(this.stores.friendsStore){this.stores.friendsStore.load()}this.stores.usersStore.load({callback:function(){if(chatOptions.userId!=0){this.openPrivateChatList(chatOptions.open);this.pollTask.delay(51000)}},scope:this});this.stores.commonLinesStore.load()},onHide:function(){this.isHidden=true;Ext.get(this.renderTo).hide()},onPrivateChatUpdate:function(a){if(this.isHidden){this.bounceBar()}},openPrivateChat:function(d,f,e){var g="private-chat-"+d;var b=Ext.getCmp(g);if(!b){b=new calislv.chat.LinesPanel({closeable:true,id:g,userId:d,title:f});var c=Ext.getCmp("chat-tabs");var a=c.getActiveTab();c.add(b).show();b.doLayout();if(e){c.setActiveTab(b);c.scrollToTab(b,true)}else{c.setActiveTab(a);c.scrollToTab(a,true)}b.on("linesupdated",this.onPrivateChatUpdate,this)}},openPrivateChatList:function(d){var c,b,a;for(c=0;c<d.length;c++){b=this.stores.usersStore.find("uid",d[c]);if(b>=0){a=this.stores.usersStore.getAt(b);this.openPrivateChat(d[c],a.get("un"),false)}}},registerListeners:function(){this.on("hide",this.onHide,this);this.on("beforeshow",this.beforeShow,this)},setInitialHeight:function(){var a=Ext.get("cchat-panel").getHeight();this.setHeight(a)}});Ext.onReady(function(){Ext.BLANK_IMAGE_URL="/typo3/gfx/clear.gif";Ext.QuickTips.init();Ext.MessageBox.minWidth=250;chatWindow=new calislv.chat.Chat()});
