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();
$(document).ready(function(){
originalTitle = document.title;
startChatSession();
$([window, document]).blur(function(){
windowFocus = false;
}).focus(function(){
windowFocus = true;
document.title = originalTitle;
});
});
var RequestObject = false;
//directorio donde tenemos el archivo ajax.php
var Archivo = 'consulta_chat.php';
// el tiempo X que tardará en actualizarse
window.setInterval("actualizacion_reloj()", 1000);
if (window.XMLHttpRequest) RequestObject = new XMLHttpRequest();
if (window.ActiveXObject) RequestObject = new ActiveXObject("Microsoft.XMLHTTP");
function ReqChange() {
// Si se ha recibido la información correctamente
if (RequestObject.readyState==4) {
// si la información es válida
if (RequestObject.responseText.indexOf('invalid') == -1) {
// Buscamos la div con id online
document.getElementById("cajon_chat").innerHTML = RequestObject.responseText;
} else {
// Por si hay algun error document.getElementById("online").innerHTML = "Error llamando";
}
}
}
function llamadaAjax() {
// Mensaje a mostrar mientras se obtiene la información remota...
var previousContent = '';
// Preparamos la obtención de datos
RequestObject.open("GET", Archivo+"?"+Math.random() , true);
RequestObject.onreadystatechange = ReqChange;
// Enviamos
RequestObject.send(null);
}
function actualizacion_reloj() {
llamadaAjax();
}
function restructureChatBoxes() {
align = 0;
for (x in chatBoxes) {
chatboxtitle = chatBoxes[x];
if ($("#chatbox_"+chatboxtitle).css('display') != 'none') {
if (align == 0) {
$("#chatbox_"+chatboxtitle).css('right', '20px');
} else {
width = (align)*(225+7)+20;
$("#chatbox_"+chatboxtitle).css('right', width+'px');
}
align++;
}
}
}
function chatWith(chatuser, chatid) {
createChatBox(chatid, chatuser);
$("#chatbox_"+chatid+" .chatboxtextarea").focus();
}
function createChatBox(chatid, chatboxtitle,minimizeChatBox) {
if ($("#chatbox_"+chatboxtitle).length > 0) {
if ($("#chatbox_"+chatboxtitle).css('display') == 'none') {
$("#chatbox_"+chatboxtitle).css('display','block');
restructureChatBoxes();
}
$("#chatbox_"+chatboxtitle+" .chatboxtextarea").focus();
return;
}
$(" <div />" ).attr("id","chatbox_"+chatboxtitle)
.addClass("chatbox")
.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+'\','+chatid+');"></textarea></div>')
.appendTo($( "body" ));
$("#chatbox_"+chatboxtitle).css('bottom', '0px');
chatBoxeslength = 0;
for (x in chatBoxes) {
if ($("#chatbox_"+chatBoxes[x]).css('display') != 'none') {
chatBoxeslength++;
}
}
if (chatBoxeslength == 0) {
$("#chatbox_"+chatboxtitle).css('right', '20px');
} else {
width = (chatBoxeslength)*(225+7)+20;
$("#chatbox_"+chatboxtitle).css('right', width+'px');
}
chatBoxes.push(chatboxtitle);
if (minimizeChatBox == 1) {
minimizedChatBoxes = new Array();
if ($.cookie('chatbox_minimized')) {
minimizedChatBoxes = $.cookie('chatbox_minimized').split(/\|/);
}
minimize = 0;
for (j=0;j<minimizedChatBoxes.length;j++) {
if (minimizedChatBoxes[j] == chatboxtitle) {
minimize = 1;
}
}
if (minimize == 1) {
$('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display','none');
$('#chatbox_'+chatboxtitle+' .chatboxinput').css('display','none');
}
}
chatboxFocus[chatboxtitle] = false;
$("#chatbox_"+chatboxtitle+" .chatboxtextarea").blur(function(){
chatboxFocus[chatboxtitle] = false;
$("#chatbox_"+chatboxtitle+" .chatboxtextarea").removeClass('chatboxtextareaselected');
}).focus(function(){
chatboxFocus[chatboxtitle] = true;
newMessages[chatboxtitle] = false;
$('#chatbox_'+chatboxtitle+' .chatboxhead').removeClass('chatboxblink');
$("#chatbox_"+chatboxtitle+" .chatboxtextarea").addClass('chatboxtextareaselected');
});
$("#chatbox_"+chatboxtitle).click(function() {
if ($('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display') != 'none') {
$("#chatbox_"+chatboxtitle+" .chatboxtextarea").focus();
}
});
$("#chatbox_"+chatboxtitle).show();
}
function chatHeartbeat(){
var itemsfound = 0;
if (windowFocus == false) {
var blinkNumber = 0;
var titleChanged = 0;
for (x in newMessagesWin) {
if (newMessagesWin[x] == true) {
++blinkNumber;
if (blinkNumber >= blinkOrder) {
document.title = x+' dice...';/* AKI ES LO DE ARRIBA DEL TITULO*/
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: "chat.php?action=chatheartbeat",
cache: false,
dataType: "json",
success: function(data) {
$.each(data.items, function(i,item){
if (item) { // fix strange ie bug
chatboxtitle = item.f;
if ($("#chatbox_"+chatboxtitle).length <= 0) {
createChatBox(chatboxtitle);
}
if ($("#chatbox_"+chatboxtitle).css('display') == 'none') {
$("#chatbox_"+chatboxtitle).css('display','block');
restructureChatBoxes();
}
if (item.s == 1) {
item.f = username;
}
if (item.s == 2) {
$("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxinfo">'+item.m+'</span></div>');
} else {
newMessages[chatboxtitle] = true;
newMessagesWin[chatboxtitle] = true;
$("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxmessagefrom">'+item.f+': </span><span class="chatboxmessagecontent">'+item.m+'</span></div>');
}
$("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .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("chat.php?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<minimizedChatBoxes.length;i++) {
if (minimizedChatBoxes[i] != chatboxtitle) {
newCookie += chatboxtitle+'|';
}
}
newCookie = newCookie.slice(0, -1)
$.cookie('chatbox_minimized', newCookie);
$('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display','block');
$('#chatbox_'+chatboxtitle+' .chatboxinput').css('display','block');
$("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
} else {
var newCookie = chatboxtitle;
if ($.cookie('chatbox_minimized')) {
newCookie += '|'+$.cookie('chatbox_minimized');
}
$.cookie('chatbox_minimized',newCookie);
$('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display','none');
$('#chatbox_'+chatboxtitle+' .chatboxinput').css('display','none');
}
}