@ -33,13 +33,31 @@ 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 header.inc.php/ template/layout/head.tpl
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 ( ) ;
//startChatHeartBeat();
//stopChatHeartBeat();
$ ( [ window , document ] ) . blur ( function ( ) {
$ ( [ window , document ] ) . blur ( function ( ) {
windowFocus = false ;
} ) . focus ( function ( ) {
windowFocus = true ;
@ -48,6 +66,21 @@ $(document).ready(function() {
/* Live conditions */
// User name header toogle
$ ( '#chatboxtitlemain' ) . live ( 'click' , 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
$ ( '.chatboxtitle' ) . live ( 'click' , function ( ) {
chatbox = $ ( this ) . parents ( ".chatbox" ) ;
@ -67,9 +100,25 @@ $(document).ready(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 ;
}
$ ( "<div />" ) . attr ( "id" , "chatmain" )
. addClass ( "chatboxmain" )
. html ( '<div class="chatboxheadmain"><div class="user_status_main">' + button + '</div><div id="chatboxtitlemain">' + label + '</div><div class="chatboxoptions"></div><br clear="all"/></div></div>' )
. appendTo ( $ ( "body" ) ) ;
}
/ * *
* Start chat session
* /
@ -81,7 +130,13 @@ function startChatSession() {
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 ) {
//received_messages[item.f][item.id] = true;
$ . each ( user _items , function ( i , item ) {
@ -110,14 +165,18 @@ function startChatSession() {
for ( i = 0 ; i < chatBoxes . length ; i ++ ) {
my _user _id = chatBoxes [ i ] ;
$ ( "#chatbox_" + my _user _id + " .chatboxcontent" ) . scrollTop ( $ ( "#chatbox_" + my _user _id + " .chatboxcontent" ) [ 0 ] . scrollHeight ) ;
//setTimeout('$("#chatbox_"+my_user_id+" .chatboxcontent").scrollTop($("#chatbox_"+my_user_id+" .chatboxcontent")[0].scrollHeight);', 100); // yet another strange ie bug
}
setTimeout ( 'chatHeartbeat();' , chatHeartbeatTime ) ;
}
} } ) ;
}
function stopChatHeartBeat ( ) {
clearInterval ( timer ) ;
timer = null ;
}
function startChatHeartBeat ( ) {
timer = setInterval ( 'chatHeartbeat();' , chatHeartbeatTime ) ;
}
/ *
* Shows the user messages in all windows
@ -226,7 +285,7 @@ function chatHeartbeat() {
}
}
setTimeout ( 'chatHeartbeat();' , chatHeartbeatTime ) ;
//timer = setTimeout('chatHeartbeat();',chatHeartbeatTime);
}
} ) ; //ajax
}
@ -245,9 +304,9 @@ function restructureChatBoxes() {
user _id = chatBoxes [ x ] ;
if ( $ ( "#chatbox_" + user _id ) . css ( 'display' ) != 'none' ) {
if ( align == 0 ) {
$ ( "#chatbox_" + user _id ) . css ( 'right' , '20 px' ) ;
$ ( "#chatbox_" + user _id ) . css ( 'right' , '245 px' ) ;
} else {
width = ( align ) * ( 225 + 7 ) + 20 ;
width = ( align ) * ( 225 + 7 ) + 20 + 225 ;
$ ( "#chatbox_" + user _id ) . css ( 'right' , width + 'px' ) ;
}
align ++ ;
@ -259,6 +318,7 @@ function restructureChatBoxes() {
* 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 ) {
@ -305,9 +365,9 @@ function createChatBox(user_id, chatboxtitle, minimizeChatBox, online) {
}
if ( chatBoxeslength == 0 ) {
$ ( "#chatbox_" + user _id ) . css ( 'right' , '20 px' ) ;
$ ( "#chatbox_" + user _id ) . css ( 'right' , '245 px' ) ;
} else {
width = ( chatBoxeslength ) * ( 225 + 7 ) + 20 ;
width = ( chatBoxeslength ) * ( 225 + 7 ) + 20 + 225 ;
$ ( "#chatbox_" + user _id ) . css ( 'right' , width + 'px' ) ;
}
@ -349,12 +409,13 @@ function createChatBox(user_id, chatboxtitle, minimizeChatBox, online) {
$ ( "#chatbox_" + user _id + " .chatboxtextarea" ) . focus ( ) ;
}
} ) ;
$ ( "#chatbox_" + user _id ) . show ( ) ;
}
/ * *
* Creates the div user status ( green / gray button next to the user name )
* @ param int user id
* @ param int status 1 or 0
* /
function return _online _user ( user _id , status ) {
var div _wrapper = $ ( "<div />" ) ;
@ -421,7 +482,6 @@ function toggleChatBoxGrowth(user_id) {
}
function checkChatBoxInputKey ( event , chatboxtextarea , user _id ) {
if ( event . keyCode == 13 && event . shiftKey == 0 ) {
message = $ ( chatboxtextarea ) . val ( ) ;
message = message . replace ( /^\s+|\s+$/g , "" ) ;