/* 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 = 4000; 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 timer; var user_status = 0; //var ajax_url = 'chat.php'; // This variable is loaded in the template/layout/head.tpl file function set_user_status(status) { $.ajax({ url: ajax_url+"?action=set_status", data: "status="+status, cache: false, success: function(data) { } }); user_status = status; } $(document).ready(function() { originalTitle = document.title; startChatSession(); $([window, document]).blur(function() { windowFocus = false; }).focus(function(){ windowFocus = true; document.title = originalTitle; }); /* "On" conditions, divs are created dynamically */ // User name header toogle $('body').on('click', '#chatboxtitlemain', function() { if (user_status == 1) { stopChatHeartBeat(); $('.user_status_main').html(offline_button); $('#chatboxtitlemain').html(disconnect_lang); set_user_status(0); } else { startChatHeartBeat(); $('.user_status_main').html(online_button); $('#chatboxtitlemain').html(connect_lang); set_user_status(1); } }); // User name header toogle $('body').on('click', '.chatboxtitle', function(){ chatbox = $(this).parents(".chatbox"); var chat_id = chatbox.attr('id'); chat_id = chat_id.split('_')[1]; toggleChatBoxGrowth(chat_id); }); // Minimize button $('body').on('click', '.chatboxhead .togglelink', function(){ var chat_id = $(this).attr('rel'); toggleChatBoxGrowth(chat_id); }); // Close button $('body').on('click', '.chatboxhead .closelink', function(){ var chat_id = $(this).attr('rel'); closeChatBox(chat_id); }); }); function showChatConnect() { if (user_status == 1) { button = online_button; label = connect_lang; } else { button = offline_button; label = disconnect_lang; } $("
" ).attr("id","chatmain") .addClass("chatboxmain") .html('
'+button+'
'+label+'
') .appendTo($( "body" )); } /** * Start chat session */ function startChatSession() { /* fix bug BT#5728 whereby users cannot move to the next question in IE9 */ if (typeof ajax_url != 'undefined') { $.ajax({ url: ajax_url+"?action=startchatsession", cache: false, dataType: "json", success: function(data) { if (data) { username = data.me; user_status = data.user_status; showChatConnect(); if (user_status == 1) { startChatHeartBeat(); } else { stopChatHeartBeat(); } $.each(data.items, function(my_user_id, user_items) { my_items = user_items['items']; $.each(my_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, user_items.user_info.user_name, 1, user_items.user_info.online); } 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('
\n\ '+item.username+':  \n\ '+item.m+'
'); } } }); }); for (i=0;i= blinkOrder) { document.title = newMessagesWin[x].username+' says...'; titleChanged = 1; break; } } } if (titleChanged == 0) { document.title = originalTitle; blinkOrder = 0; } else { ++blinkOrder; } } else { for (x in newMessagesWin) { newMessagesWin[x].status = 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(my_user_id, user_items) { my_items = user_items['items']; $.each(my_items, function(i, item) { if (item) { // fix strange ie bug if ($("#chatbox_"+my_user_id).length <= 0) { createChatBox(my_user_id, user_items.user_info.user_name, 0, user_items.user_info.online); } if ($("#chatbox_"+my_user_id).css('display') == 'none') { $("#chatbox_"+my_user_id).css('display','block'); restructureChatBoxes(); } if (item.s == 1) { //item.f = username; } update_online_user(my_user_id, user_items.user_info.online); if (item.s == 2) { $("#chatbox_"+my_user_id+" .chatboxcontent").append('
'+item.m+'
'); } else { newMessages[my_user_id] = {'status':true, 'username':item.username}; newMessagesWin[my_user_id] = {'status':true, 'username':item.username}; $("#chatbox_"+my_user_id+" .chatboxcontent").append('
\n\ '+item.username+':  \n\ '+item.m+'
'); } $("#chatbox_"+my_user_id+" .chatboxcontent").scrollTop($("#chatbox_"+my_user_id+" .chatboxcontent")[0].scrollHeight); if ($('#chatbox_'+my_user_id+' .chatboxcontent').css('display') == 'none') { $('#chatbox_'+my_user_id+' .chatboxhead').toggleClass('chatboxblink'); } itemsfound += 1; } }); }); chatHeartbeatCount++; if (itemsfound > 0) { chatHeartbeatTime = minChatHeartbeat; chatHeartbeatCount = 1; } else if (chatHeartbeatCount >= 10) { chatHeartbeatTime *= 2; chatHeartbeatCount = 1; if (chatHeartbeatTime > maxChatHeartbeat) { chatHeartbeatTime = maxChatHeartbeat; } } //timer = setTimeout('chatHeartbeat();',chatHeartbeatTime); } }); //ajax } function closeChatBox(user_id) { $('#chatbox_'+user_id).css('display','none'); restructureChatBoxes(); $.post(ajax_url+"?action=closechat", {chatbox: user_id} , function(data){ }); } function restructureChatBoxes() { align = 0; for (x in chatBoxes) { user_id = chatBoxes[x]; if ($("#chatbox_"+user_id).css('display') != 'none') { if (align == 0) { $("#chatbox_"+user_id).css('right', '245px'); } else { width = (align)*(225+7)+20 + 225; $("#chatbox_"+user_id).css('right', width+'px'); } align++; } } } /** * Function that fires the chat with an user (creates a chat bloclk) * @param int user id * @param string user's firstname + lastname * @param status * **/ function chatWith(user_id, user_name, status, userImage) { createChatBox(user_id, user_name, 0, status, userImage); $("#chatbox_"+user_id+" .chatboxtextarea").focus(); } function chatNotYetWith(message) { $("#js_alerts").html(message); $("#js_alerts").css('display', 'block'); $("#js_alerts").attr('class', 'alert alert-warning'); $('#js_alerts').alert() } /** * Creates a div */ function createChatBox(user_id, chatboxtitle, minimizeChatBox, online, userImage) { if ($("#chatbox_"+user_id).length > 0) { if ($("#chatbox_"+user_id).css('display') == 'none') { $("#chatbox_"+user_id).css('display','block'); restructureChatBoxes(); } $("#chatbox_"+user_id+" .chatboxtextarea").focus(); return; } user_is_online = return_online_user(user_id, online, userImage); var chatbox = $('
') .attr({ id: 'chatbox_' + user_id }) .addClass('chatbox') .css('bottom', 0); var chatboxHead = $('
') .addClass('chatboxhead') .append(user_is_online); $('
') .addClass('chatimage') .append('') .appendTo(chatboxHead); $('
') .addClass('chatboxtitle') .append(chatboxtitle) .appendTo(chatboxHead); var chatboxoptions = $('
') .addClass('chatboxoptions') .appendTo(chatboxHead); if (!!Modernizr.prefixed('RTCPeerConnection', window)) { $('') .addClass('btn btn-xs') .attr({ href: '#' }) .html('') .on('click', function(e) { e.preventDefault(); var createForm = $.get( ajax_url, { action: 'start_video', to: user_id } ); $.when(createForm).done(function(response) { $('#global-modal') .find('.modal-dialog') .removeClass('modal-lg'); $('#global-modal') .find('.modal-body') .html(response); $('#global-modal').modal('show'); }); }) .appendTo(chatboxoptions); } $('') .addClass('btn btn-xs togglelink') .attr({ href: 'javascript:void(0)', rel: user_id }) .html('') .appendTo(chatboxoptions); $('') .addClass('btn btn-xs closelink') .attr({ href: 'javascript:void(0)', rel: user_id }) .html('') .appendTo(chatboxoptions); $('
') .attr('clear', 'all') .appendTo(chatboxHead); var chatboxContent = $('
') .addClass('chatboxcontent'); var chatboxInput = $('
') .addClass('chatboxinput'); $('