Advertisement
jamboljack

chat.js

Dec 22nd, 2014
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var windowFocus         = true;
  2. var username;
  3. var chatHeartbeatCount  = 0;
  4. var minChatHeartbeat    = 1000;
  5. var maxChatHeartbeat    = 33000;
  6. var chatHeartbeatTime   = minChatHeartbeat;
  7. var originalTitle;
  8. var blinkOrder          = 0;
  9.  
  10. var chatboxFocus        = new Array();
  11. var newMessages         = new Array();
  12. var newMessagesWin      = new Array();
  13. var chatBoxes           = new Array();
  14.  
  15. jQuery.noConflict();
  16.  
  17. jQuery(document).ready(function(){
  18.     originalTitle = document.title;
  19.     startChatSession();
  20.  
  21.     jQuery([window, document]).blur(function(){
  22.         windowFocus = false;
  23.     }).focus(function(){
  24.         windowFocus = true;
  25.         document.title = originalTitle;
  26.     });
  27. });
  28.  
  29.  
  30.  
  31.  
  32.  
  33. //**********************************************************************************************************
  34.     // restructureChatBoxes
  35. //**********************************************************************************************************
  36.  
  37. function restructureChatBoxes() {
  38.     align = 0;
  39.     for (x in chatBoxes) {
  40.         chatboxtitle = chatBoxes[x];
  41.  
  42.         if (jQuery("#chatbox_"+chatboxtitle).css('display') != 'none') {
  43.             if (align == 0) {
  44.                 jQuery("#chatbox_"+chatboxtitle).css('right', '20px');
  45.             } else {
  46.                 width = (align)*(225+7)+20;
  47.                 jQuery("#chatbox_"+chatboxtitle).css('right', width+'px');
  48.             }
  49.             align++;
  50.         }
  51.     }
  52. }
  53.  
  54.  
  55.  
  56. //**********************************************************************************************************
  57.     // chatWith
  58. //**********************************************************************************************************
  59.  
  60. function chatWith(chatuser) {
  61.     createChatBox(chatuser);
  62.     jQuery("#chatbox_"+chatuser+" .chatboxtextarea").focus();
  63. }
  64.  
  65.  
  66. //**********************************************************************************************************
  67.     // createChatBox
  68. //**********************************************************************************************************
  69.  
  70. function createChatBox(chatboxtitle,minimizeChatBox) {
  71.     if (jQuery("#chatbox_"+chatboxtitle).length > 0) {
  72.         if (jQuery("#chatbox_"+chatboxtitle).css('display') == 'none') {
  73.             jQuery("#chatbox_"+chatboxtitle).css('display','block');
  74.             restructureChatBoxes();
  75.         }
  76.         jQuery("#chatbox_"+chatboxtitle+" .chatboxtextarea").focus();
  77.         return;
  78.     }
  79.  
  80.     jQuery(" <div />" ).attr("id","chatbox_"+chatboxtitle)
  81.     .addClass("chatbox")
  82.     .html('<div class="chatboxhead"><div class="chatboxtitle">'+chatboxtitle+'</div><div class="chatboxoptions"><a href="javascript:void(0)" onclick="javascript:toggleChatBoxGrowth(\''+chatboxtitle+'\')">-</a> <a href="javascript:void(0)" onclick="javascript:closeChatBox(\''+chatboxtitle+'\')">X</a></div><br clear="all"/></div><div class="chatboxcontent"></div><div class="chatboxinput"><textarea class="chatboxtextarea" onkeydown="javascript:return checkChatBoxInputKey(event,this,\''+chatboxtitle+'\');"></textarea></div>')
  83.     .appendTo(jQuery( "body" ));
  84.                
  85.     jQuery("#chatbox_"+chatboxtitle).css('bottom', '0px');
  86.    
  87.     chatBoxeslength = 0;
  88.  
  89.     for (x in chatBoxes) {
  90.         if (jQuery("#chatbox_"+chatBoxes[x]).css('display') != 'none') {
  91.             chatBoxeslength++;
  92.         }
  93.     }
  94.  
  95.     if (chatBoxeslength == 0) {
  96.         jQuery("#chatbox_"+chatboxtitle).css('right', '20px');
  97.     } else {
  98.         width = (chatBoxeslength)*(225+7)+20;
  99.         jQuery("#chatbox_"+chatboxtitle).css('right', width+'px');
  100.     }
  101.    
  102.     chatBoxes.push(chatboxtitle);
  103.  
  104.     if (minimizeChatBox == 1) {
  105.         minimizedChatBoxes = new Array();
  106.  
  107.         if (jQuery.cookie('chatbox_minimized')) {
  108.             minimizedChatBoxes = jQuery.cookie('chatbox_minimized').split(/\|/);
  109.         }
  110.         minimize = 0;
  111.         for (j=0;j<minimizedChatBoxes.length;j++) {
  112.             if (minimizedChatBoxes[j] == chatboxtitle) {
  113.                 minimize = 1;
  114.             }
  115.         }
  116.  
  117.         if (minimize == 1) {
  118.             jQuery('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display','none');
  119.             jQuery('#chatbox_'+chatboxtitle+' .chatboxinput').css('display','none');
  120.         }
  121.     }
  122.  
  123.     chatboxFocus[chatboxtitle] = false;
  124.  
  125.     jQuery("#chatbox_"+chatboxtitle+" .chatboxtextarea").blur(function(){
  126.         chatboxFocus[chatboxtitle] = false;
  127.         jQuery("#chatbox_"+chatboxtitle+" .chatboxtextarea").removeClass('chatboxtextareaselected');
  128.     }).focus(function(){
  129.         chatboxFocus[chatboxtitle] = true;
  130.         newMessages[chatboxtitle] = false;
  131.         jQuery('#chatbox_'+chatboxtitle+' .chatboxhead').removeClass('chatboxblink');
  132.         jQuery("#chatbox_"+chatboxtitle+" .chatboxtextarea").addClass('chatboxtextareaselected');
  133.     });
  134.  
  135.     jQuery("#chatbox_"+chatboxtitle).click(function() {
  136.         if (jQuery('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display') != 'none') {
  137.             jQuery("#chatbox_"+chatboxtitle+" .chatboxtextarea").focus();
  138.         }
  139.     });
  140.  
  141.     jQuery("#chatbox_"+chatboxtitle).show();
  142.     ///////////////
  143.     //$("#chatbox_"+chatboxtitle).draggable();
  144.     //$(".chatbox_").draggable();
  145.     ///////////
  146. }
  147.  
  148.  
  149.  
  150.  
  151. //**********************************************************************************************************
  152.     // chatHeartbeat
  153. //**********************************************************************************************************
  154.  
  155.  
  156. function chatHeartbeat(){
  157.  
  158.     var itemsfound = 0;
  159.    
  160.     if (windowFocus == false) {
  161.  
  162.         var blinkNumber = 0;
  163.         var titleChanged = 0;
  164.         for (x in newMessagesWin) {
  165.             if (newMessagesWin[x] == true) {
  166.                 ++blinkNumber;
  167.                 if (blinkNumber >= blinkOrder) {
  168.                     document.title = x+' says...';
  169.                     titleChanged = 1;
  170.                     break; 
  171.                 }
  172.             }
  173.         }
  174.        
  175.         if (titleChanged == 0) {
  176.             document.title = originalTitle;
  177.             blinkOrder = 0;
  178.         } else {
  179.             ++blinkOrder;
  180.         }
  181.  
  182.     } else {
  183.         for (x in newMessagesWin) {
  184.             newMessagesWin[x] = false;
  185.         }
  186.     }
  187.  
  188.     for (x in newMessages) {
  189.         if (newMessages[x] == true) {
  190.             if (chatboxFocus[x] == false) {
  191.                 //FIXME: add toggle all or none policy, otherwise it looks funny
  192.                 jQuery('#chatbox_'+x+' .chatboxhead').toggleClass('chatboxblink');
  193.             }
  194.         }
  195.     }
  196.    
  197.     jQuery.ajax({
  198.       url: "http://teknik.hotelhomkudus.com/application/views/chat/chat.php?action=chatheartbeat",
  199.       cache: false,
  200.       dataType: "json",
  201.       success: function(data) {
  202.  
  203.         jQuery.each(data.items, function(i,item){
  204.             if (item)   { // fix strange ie bug
  205.                 chatboxtitle = item.f;
  206.  
  207.                 if (jQuery("#chatbox_"+chatboxtitle).length <= 0) {
  208.                     createChatBox(chatboxtitle);
  209.                 }
  210.                 if (jQuery("#chatbox_"+chatboxtitle).css('display') == 'none') {
  211.                     jQuery("#chatbox_"+chatboxtitle).css('display','block');
  212.                     restructureChatBoxes();
  213.                 }
  214.                
  215.                 if (item.s == 1) {
  216.                     item.f = username;
  217.                 }
  218.  
  219.                 if (item.s == 2) {
  220.                     jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxinfo">'+item.m+'</span></div>');
  221.                 } else {
  222.                     newMessages[chatboxtitle] = true;
  223.                     newMessagesWin[chatboxtitle] = true;
  224.                     jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxmessagefrom">'+item.f+':&nbsp;&nbsp;</span><span class="chatboxmessagecontent">'+item.m+'</span></div>');
  225.                 }
  226.  
  227.                 jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop(jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
  228.                 itemsfound += 1;
  229.             }
  230.         });
  231.  
  232.         chatHeartbeatCount++;
  233.  
  234.         if (itemsfound > 0) {
  235.             chatHeartbeatTime = minChatHeartbeat;
  236.             chatHeartbeatCount = 1;
  237.         } else if (chatHeartbeatCount >= 10) {
  238.             chatHeartbeatTime *= 2;
  239.             chatHeartbeatCount = 1;
  240.             if (chatHeartbeatTime > maxChatHeartbeat) {
  241.                 chatHeartbeatTime = maxChatHeartbeat;
  242.             }
  243.         }
  244.        
  245.         setTimeout('chatHeartbeat();',chatHeartbeatTime);
  246.     }});
  247. }
  248.  
  249.  
  250. //**********************************************************************************************************
  251.     // closeChatBox
  252. //**********************************************************************************************************
  253.  
  254. function closeChatBox(chatboxtitle) {
  255.     jQuery('#chatbox_'+chatboxtitle).css('display','none');
  256.     restructureChatBoxes();
  257.  
  258.     jQuery.post("http://teknik.hotelhomkudus.com/application/views/chat/chat.php?action=closechat", { chatbox: chatboxtitle} , function(data){ 
  259.     });
  260.  
  261. }
  262.  
  263. //**********************************************************************************************************
  264.     // toggleChatBoxGrowth
  265. //**********************************************************************************************************
  266.  
  267. function toggleChatBoxGrowth(chatboxtitle) {
  268.     if (jQuery('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display') == 'none') {  
  269.        
  270.         var minimizedChatBoxes = new Array();
  271.        
  272.         if (jQuery.cookie('chatbox_minimized')) {
  273.             minimizedChatBoxes = jQuery.cookie('chatbox_minimized').split(/\|/);
  274.         }
  275.  
  276.         var newCookie = '';
  277.  
  278.         for (i=0;i<minimizedChatBoxes.length;i++) {
  279.             if (minimizedChatBoxes[i] != chatboxtitle) {
  280.                 newCookie += chatboxtitle+'|';
  281.             }
  282.         }
  283.  
  284.         newCookie = newCookie.slice(0, -1)
  285.  
  286.  
  287.         jQuery.cookie('chatbox_minimized', newCookie);
  288.         jQuery('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display','block');
  289.         jQuery('#chatbox_'+chatboxtitle+' .chatboxinput').css('display','block');
  290.         jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop(jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
  291.     } else {
  292.        
  293.         var newCookie = chatboxtitle;
  294.  
  295.         if (jQuery.cookie('chatbox_minimized')) {
  296.             newCookie += '|'+jQuery.cookie('chatbox_minimized');
  297.         }
  298.  
  299.  
  300.         jQuery.cookie('chatbox_minimized',newCookie);
  301.         jQuery('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display','none');
  302.         jQuery('#chatbox_'+chatboxtitle+' .chatboxinput').css('display','none');
  303.     }
  304.    
  305. }
  306.  
  307.  
  308.  
  309.  
  310. //**********************************************************************************************************
  311.     // checkChatBoxInputKey
  312. //**********************************************************************************************************
  313.  
  314. function checkChatBoxInputKey(event,chatboxtextarea,chatboxtitle) {
  315.      
  316.     if(event.keyCode == 13 && event.shiftKey == 0)  {
  317.         message = jQuery(chatboxtextarea).val();
  318.         message = message.replace(/^\s+|\s+$/g,"");
  319.  
  320.         jQuery(chatboxtextarea).val('');
  321.         jQuery(chatboxtextarea).focus();
  322.         jQuery(chatboxtextarea).css('height','44px');
  323.         if (message != '') {
  324.             jQuery.post("http://teknik.hotelhomkudus.com/application/views/chat/chat.php?action=sendchat", {to: chatboxtitle, message: message} , function(data){
  325.                 message = message.replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\"/g,"&quot;");
  326.                 jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxmessagefrom">'+username+':&nbsp;&nbsp;</span><span class="chatboxmessagecontent">'+message+'</span></div>');
  327.                 jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop(jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
  328.             });
  329.         }
  330.         chatHeartbeatTime = minChatHeartbeat;
  331.         chatHeartbeatCount = 1;
  332.  
  333.         return false;
  334.     }
  335.  
  336.     var adjustedHeight = chatboxtextarea.clientHeight;
  337.     var maxHeight = 94;
  338.  
  339.     if (maxHeight > adjustedHeight) {
  340.         adjustedHeight = Math.max(chatboxtextarea.scrollHeight, adjustedHeight);
  341.         if (maxHeight)
  342.             adjustedHeight = Math.min(maxHeight, adjustedHeight);
  343.         if (adjustedHeight > chatboxtextarea.clientHeight)
  344.             jQuery(chatboxtextarea).css('height',adjustedHeight+8 +'px');
  345.     } else {
  346.         jQuery(chatboxtextarea).css('overflow','auto');
  347.     }
  348.      
  349. }
  350.  
  351.  
  352.  
  353. //**********************************************************************************************************
  354.     // startChatSession
  355. //**********************************************************************************************************
  356.  
  357. function startChatSession(){  
  358.     jQuery.ajax({
  359.       url: "http://teknik.hotelhomkudus.com/application/views/chat/chat.php?action=startchatsession",
  360.       cache: false,
  361.       dataType: "json",
  362.       success: function(data) {
  363.  
  364.         username = data.username;
  365.  
  366.         jQuery.each(data.items, function(i,item){
  367.             if (item)   { // fix strange ie bug
  368.  
  369.                 chatboxtitle = item.f;
  370.  
  371.                 if (jQuery("#chatbox_"+chatboxtitle).length <= 0) {
  372.                     createChatBox(chatboxtitle,1);
  373.                 }
  374.                
  375.                 if (item.s == 1) {
  376.                     item.f = username;
  377.                 }
  378.  
  379.                 if (item.s == 2) {
  380.                     jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxinfo">'+item.m+'</span></div>');
  381.                 } else {
  382.                     jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxmessagefrom">'+item.f+':&nbsp;&nbsp;</span><span class="chatboxmessagecontent">'+item.m+'</span></div>');
  383.                 }
  384.             }
  385.         });
  386.        
  387.         for (i=0;i<chatBoxes.length;i++) {
  388.             chatboxtitle = chatBoxes[i];
  389.             jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop(jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
  390.             setTimeout('jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop(jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);', 100); // yet another strange ie bug
  391.         }
  392.     // smith
  393.         //$("#chatbox_"+chatboxtitle).draggable();
  394.         //$(".chatbox_").draggable();
  395.     ////////////////
  396.     setTimeout('chatHeartbeat();',chatHeartbeatTime);
  397.    
  398.     }});
  399. }
  400.  
  401.  
  402.  
  403.  
  404. /**
  405.  * Cookie plugin
  406.  *
  407.  * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
  408.  * Dual licensed under the MIT and GPL licenses:
  409.  * http://www.opensource.org/licenses/mit-license.php
  410.  * http://www.gnu.org/licenses/gpl.html
  411.  *
  412.  */
  413.  
  414. jQuery.cookie = function(name, value, options) {
  415.     if (typeof value != 'undefined') { // name and value given, set cookie
  416.         options = options || {};
  417.         if (value === null) {
  418.             value = '';
  419.             options.expires = -1;
  420.         }
  421.         var expires = '';
  422.         if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
  423.             var date;
  424.             if (typeof options.expires == 'number') {
  425.                 date = new Date();
  426.                 date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
  427.             } else {
  428.                 date = options.expires;
  429.             }
  430.             expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
  431.         }
  432.         // CAUTION: Needed to parenthesize options.path and options.domain
  433.         // in the following expressions, otherwise they evaluate to undefined
  434.         // in the packed version for some reason...
  435.         var path = options.path ? '; path=' + (options.path) : '';
  436.         var domain = options.domain ? '; domain=' + (options.domain) : '';
  437.         var secure = options.secure ? '; secure' : '';
  438.         document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
  439.     } else { // only name given, get cookie
  440.         var cookieValue = null;
  441.         if (document.cookie && document.cookie != '') {
  442.             var cookies = document.cookie.split(';');
  443.             for (var i = 0; i < cookies.length; i++) {
  444.                 var cookie = jQuery.trim(cookies[i]);
  445.                 // Does this cookie string begin with the name we want?
  446.                 if (cookie.substring(0, name.length + 1) == (name + '=')) {
  447.                     cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
  448.                     break;
  449.                 }
  450.             }
  451.         }
  452.         return cookieValue;
  453.     }
  454. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement