/* Copyright (c) 2009 Anant Garg (anantgarg.com | inscripts.com) This script may be used for non-commercial purposes only. For any commercial purposes, please contact the author at anant.garg@inscripts.com Changes and Chamilo Integration: Julio Montoya THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ var windowFocus = true; var username; var chatHeartbeatCount = 0; var minChatHeartbeat = 1000; var maxChatHeartbeat = 33000; var chatHeartbeatTime = minChatHeartbeat; var originalTitle; var blinkOrder = 0; var chatboxFocus = new Array(); var newMessages = new Array(); var newMessagesWin = new Array(); var chatBoxes = new Array(); //var ajax_url = 'chat.php'; variable loaded in header.inc.php $(document).ready(function(){ originalTitle = document.title; startChatSession(); $([window, document]).blur(function(){ windowFocus = false; }).focus(function(){ windowFocus = true; document.title = originalTitle; }); }); function startChatSession(){ $.ajax({ url: ajax_url+"?action=startchatsession", cache: false, dataType: "json", success: function(data) { if (data) { username = data.username; $.each(data.items, function(i,item){ if (item) { // fix strange ie bug my_user_id = item.f; if ($("#chatbox_"+my_user_id).length <= 0) { createChatBox(my_user_id, chatboxtitle, 1); } if (item.s == 1) { item.f = username; } if (item.s == 2) { $("#chatbox_"+my_user_id+" .chatboxcontent").append('
'+item.m+'
'); } else { $("#chatbox_"+my_user_id+" .chatboxcontent").append('
'+item.f+':  '+item.m+'
'); } } }); for (i=0;i 0) { if ($("#chatbox_"+user_id).css('display') == 'none') { $("#chatbox_"+user_id).css('display','block'); restructureChatBoxes(); } $("#chatbox_"+user_id+" .chatboxtextarea").focus(); return; } $("
" ).attr("id","chatbox_"+user_id) .addClass("chatbox") .html('
\n\
'+chatboxtitle+'
\n\
_  \n\ X

\n\
\n\
') .appendTo($( "body" )); $("#chatbox_"+user_id).css('bottom', '0px'); chatBoxeslength = 0; for (x in chatBoxes) { if ($("#chatbox_"+chatBoxes[x]).css('display') != 'none') { chatBoxeslength++; } } if (chatBoxeslength == 0) { $("#chatbox_"+user_id).css('right', '20px'); } else { width = (chatBoxeslength)*(225+7)+20; $("#chatbox_"+user_id).css('right', width+'px'); } chatBoxes.push(user_id); if (minimizeChatBox == 1) { minimizedChatBoxes = new Array(); if ($.cookie('chatbox_minimized')) { minimizedChatBoxes = $.cookie('chatbox_minimized').split(/\|/); } minimize = 0; for (j=0;j= blinkOrder) { document.title = x+' says...'; titleChanged = 1; break; } } } if (titleChanged == 0) { document.title = originalTitle; blinkOrder = 0; } else { ++blinkOrder; } } else { for (x in newMessagesWin) { newMessagesWin[x] = false; } } for (x in newMessages) { if (newMessages[x] == true) { if (chatboxFocus[x] == false) { //FIXME: add toggle all or none policy, otherwise it looks funny $('#chatbox_'+x+' .chatboxhead').toggleClass('chatboxblink'); } } } $.ajax({ url: ajax_url+"?action=chatheartbeat", cache: false, dataType: "json", success: function(data) { $.each(data.items, function(i, item) { if (item) { // fix strange ie bug my_user_id = item.f; if ($("#chatbox_"+my_user_id).length <= 0) { createChatBox(my_user_id); } if ($("#chatbox_"+my_user_id).css('display') == 'none') { $("#chatbox_"+my_user_id).css('display','block'); restructureChatBoxes(); } if (item.s == 1) { item.f = username; } if (item.s == 2) { $("#chatbox_"+my_user_id+" .chatboxcontent").append('
'+item.m+'
'); } else { newMessages[my_user_id] = true; newMessagesWin[my_user_id] = true; $("#chatbox_"+chatboxtitle+" .chatboxcontent").append('
'+item.f+':  '+item.m+'
'); } $("#chatbox_"+my_user_id+" .chatboxcontent").scrollTop($("#chatbox_"+my_user_id+" .chatboxcontent")[0].scrollHeight); itemsfound += 1; } }); chatHeartbeatCount++; if (itemsfound > 0) { chatHeartbeatTime = minChatHeartbeat; chatHeartbeatCount = 1; } else if (chatHeartbeatCount >= 10) { chatHeartbeatTime *= 2; chatHeartbeatCount = 1; if (chatHeartbeatTime > maxChatHeartbeat) { chatHeartbeatTime = maxChatHeartbeat; } } setTimeout('chatHeartbeat();',chatHeartbeatTime); }}); } function closeChatBox(chatboxtitle) { $('#chatbox_'+chatboxtitle).css('display','none'); restructureChatBoxes(); $.post(ajax_url+"?action=closechat", { chatbox: chatboxtitle} , function(data){ }); } function toggleChatBoxGrowth(chatboxtitle) { if ($('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display') == 'none') { var minimizedChatBoxes = new Array(); if ($.cookie('chatbox_minimized')) { minimizedChatBoxes = $.cookie('chatbox_minimized').split(/\|/); } var newCookie = ''; for (i=0;i/g,">").replace(/\"/g,"""); $("#chatbox_"+user_id+" .chatboxcontent").append('
\n\ '+username+':  \n\ '+message+'
'); $("#chatbox_"+user_id+" .chatboxcontent").scrollTop($("#chatbox_"+user_id+" .chatboxcontent")[0].scrollHeight); }); } chatHeartbeatTime = minChatHeartbeat; chatHeartbeatCount = 1; return false; } var adjustedHeight = chatboxtextarea.clientHeight; var maxHeight = 94; if (maxHeight > adjustedHeight) { adjustedHeight = Math.max(chatboxtextarea.scrollHeight, adjustedHeight); if (maxHeight) adjustedHeight = Math.min(maxHeight, adjustedHeight); if (adjustedHeight > chatboxtextarea.clientHeight) $(chatboxtextarea).css('height',adjustedHeight+8 +'px'); } else { $(chatboxtextarea).css('overflow','auto'); } } /** * Cookie plugin * * Copyright (c) 2006 Klaus Hartl (stilbuero.de) * Dual licensed under the MIT and GPL licenses: * http://www.opensource.org/licenses/mit-license.php * http://www.gnu.org/licenses/gpl.html * */ jQuery.cookie = function(name, value, options) { if (typeof value != 'undefined') { // name and value given, set cookie options = options || {}; if (value === null) { value = ''; options.expires = -1; } var expires = ''; if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) { var date; if (typeof options.expires == 'number') { date = new Date(); date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000)); } else { date = options.expires; } expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE } // CAUTION: Needed to parenthesize options.path and options.domain // in the following expressions, otherwise they evaluate to undefined // in the packed version for some reason... var path = options.path ? '; path=' + (options.path) : ''; var domain = options.domain ? '; domain=' + (options.domain) : ''; var secure = options.secure ? '; secure' : ''; document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join(''); } else { // only name given, get cookie var cookieValue = null; if (document.cookie && document.cookie != '') { var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookie = jQuery.trim(cookies[i]); // Does this cookie string begin with the name we want? if (cookie.substring(0, name.length + 1) == (name + '=')) { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); break; } } } return cookieValue; } };