/* Copyright (c) 2009 Anant Garg (anantgarg.com | inscripts.com) */
///START VB - per evento "test close win" (click fuori pagina)
var DLTSESS = true;
var FRCSESS = false;
function doUnload()
{
 if(DLTSESS==true || FRCSESS==true)
  deletesession(FRCSESS);
}
function doClick() {
DLTSESS=false;
var murl=document.location.href;
if(murl.indexOf("payback")==-1) FRCSESS=false; else FRCSESS=true; // se agg.anche "accedi": -> nologin
}
function deletesession(frc) {
 if (frc == true) // force logout, (se pback). forza, se clickbackhistory after pback: bypass confirmlogout
 {
    $.ajax({
     url: "main.php?logout=1",
	 cache: false,
     async: false,
	 dataType: "json",
     success: function(data) {
     }});
 }
 else
 {
  res = confirm("Logout SILAE_Live?"); //logout silae chat 2. 1=logout totale
   if (res == true)
	 $.ajax({
       url: "main.php?logout=2",
	   cache: false,
       async: false,
	   dataType: "json",
   	   success: function(data) {
       }});
 }
}
///END

var windowFocus = true;
var username;
var chatHeartbeatCount = 0;
var minChatHeartbeat = 2400; /* 1000 millisecs*/
var maxChatHeartbeat = 33000; /* 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;
	});
});

function loadthumb(iid,url) {
       $("#"+iid).attr('src',url);
/*con getfile.php, ma + lento	$.ajax({
	  url: url,
	  cache: false,
      async: false,
	  dataType: "json",
  	  success: function(data) {
       $("#"+iid).attr('src','imgs/utenti/tmb/'+data.how);
      }});*/
}

function restructureChatBoxes() {
	align = 0;
    maxwin = Math.round(screen.width/252);
	for (x in chatBoxes) {
		chatboxtitle = chatBoxes[x];

		if ($("#chatbox_"+chatboxtitle).css('display') != 'none') {
			if (align == 0) {
				$("#chatbox_"+chatboxtitle).css('right', '26px');
			} else {
/* width + gap tra chatwin + right */
                if (align<maxwin)
                {
				    width = (align)*(225+26)+1;
				    $("#chatbox_"+chatboxtitle).css('right', width+'px');
                }
                else
                {
				    $("#chatbox_"+chatboxtitle).css('left', '2px');
                }
			}
			align++;
		}
	}
}

function chatWith(chatuser,cognome,nome,email,uid,nazione) {
//alert("D: "+chatuser+','+cognome+',\n'+nome+','+email+','+uid+','+nazione);
chatuser = chatuser.replace(/ /g, "_");
	createChatBox(chatuser,0,cognome,nome,email,uid,nazione);
	$("#chatbox_"+chatuser+" .chatboxtextarea").focus();
}

function ToggleSmiles(chatboxtitle) {
	var ogg = $("#smiles_"+chatboxtitle).css('display');
	if (ogg == "block")
		$("#smiles_"+chatboxtitle).css('display','none');
	else
		$("#smiles_"+chatboxtitle).css('display','block');
}

function CopyIt(titt) {
    $("#chatbox_"+titt+" .hiddenmsg").focus();
    $("#chatbox_"+titt+" .hiddenmsg").select();
    content = $("#chatbox_"+titt+" .hiddenmsg").val();
//alert(content);
    var form = document.createElement("form");
    form.setAttribute("method", "post");
    form.setAttribute("action", "chat.php?action=getdown");

    var hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden");
    hiddenField.setAttribute("name", "chatbox");
    hiddenField.setAttribute("value", titt);
    form.appendChild(hiddenField);

    var hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden");
    hiddenField.setAttribute("name", "cbtx");
    hiddenField.setAttribute("value", content);
    form.appendChild(hiddenField);

    document.body.appendChild(form);    // Not entirely sure if this is necessary
    form.submit();
}

function ChatSmiles(message){
	message = message.replace(/:\)/gi,"<img src='imgs/icons/chat/smile.png' alt=')'/>");
	message = message.replace(/:p/gi,"<img src='imgs/icons/chat/tongue.png' alt='P'/>");
	message = message.replace(/:d/gi,"<img src='imgs/icons/chat/laugh.png' alt='D'/>");
	message = message.replace(/;\)/gi,"<img src='imgs/icons/chat/wink.png' alt='w'/>");
	message = message.replace(/\*\-\)/gi,"<img src='imgs/icons/chat/thinking.png' alt='t'/>");
	message = message.replace(/:w/gi,"<img src='imgs/icons/chat/waiting.png' alt='w'/>");
	message = message.replace(/:l/gi,"<img src='imgs/icons/chat/sleepy.png' alt='s'/>");
	message = message.replace(/:\(/gi,"<img src='imgs/icons/chat/sad.png' alt='d'/>");
	message = message.replace(/:s/gi,"<img src='imgs/icons/chat/confused.png' alt='e'/>");
	message = message.replace(/:a/gi,"<img src='imgs/icons/chat/angel.png' alt='A'/>");
	message = message.replace(/:4/gi,"<img src='imgs/icons/chat/embarrassed.png' alt='4'/>");
	message = message.replace(/:h/gi,"<img src='imgs/icons/chat/glasses-nerdy.png' alt='h'/>");
return message;
}

function createChatBox(chatboxtitle,minimizeChatBox,cognome,nome,email,uid,nazione) {
	if ($("#chatbox_"+chatboxtitle).length > 0) {
		if ($("#chatbox_"+chatboxtitle).css('display') == 'none') {
			$("#chatbox_"+chatboxtitle).css('display','block');
			restructureChatBoxes();
		}
		$("#chatbox_"+chatboxtitle+" .chatboxtextarea").focus();
		return;
	}

    if (cognome=='' || nome=='' || email=='' || uid=='')
     anag="main.php?action=anagrafica&bfget="+chatboxtitle;
    else
     anag="main.php?action=anagrafica&cognome="+cognome+"&nome="+nome+"&email="+email+"&uid="+uid+"&flag="+nazione+"&usrtype=DEF&tempfl=0";

	$(" <div />" ).attr("id","chatbox_"+chatboxtitle)
	.addClass("chatbox")
	.html('<div class="chatboxhead">\
			<div id="smiles_'+chatboxtitle+'"></div>\
            <div class="chatboxtitle"><a href="'+anag+'" title="anagrafica">'+chatboxtitle+'</a></div>\
            <div class="chatboxoptions"><a href="javascript:void(0);" onclick="javascript:ToggleSmiles(\''+chatboxtitle+'\');"><img src="imgs/icons/chat/smile.png" alt="I" width="20px" height="20px"/></a>&nbsp;<a href="javascript:void(0);" onclick="javascript:CopyIt(\''+chatboxtitle+'\');"><img src="imgs/icons/page_copy.png" alt="S"/></a>&nbsp;&nbsp;&nbsp;<a href="javascript:void(0)" onclick="javascript:toggleChatBoxGrowth(\''+chatboxtitle+'\')">_</a> <a href="javascript:void(0)" onclick="javascript:closeChatBox(\''+chatboxtitle+'\')"><img src="imgs/icons/cancel.png" alt="X"/></a></div>\
            <br clear="all"/>\
           </div>\
    <div class="chatboxcontent"><textarea id="txt_'+chatboxtitle+'" class="hiddenmsg"></textarea><table id="cta_'+chatboxtitle+'" class="chatboxcontenttab style4n left"></table></div>\
    <div class="chatboxinput"><textarea class="chatboxtextarea" onkeydown="javascript:return checkChatBoxInputKey(event,this,\''+chatboxtitle+'\');"></textarea></div>')
	.appendTo($( "body" ));
	$("#smiles_"+chatboxtitle).append("<div class='csmiles'>\
	<img src='imgs/icons/chat/smile.png' alt=':)'/>:) \
	<img src='imgs/icons/chat/tongue.png' alt=':P'/>:P \
	<img src='imgs/icons/chat/laugh.png' alt=':D'/>:D \
	<img src='imgs/icons/chat/wink.png' alt='w'/>;)<br/>\
	<img src='imgs/icons/chat/thinking.png' alt='t'/>*-) \
	<img src='imgs/icons/chat/waiting.png' alt='w'/>:W \
	<img src='imgs/icons/chat/sleepy.png' alt='s'/>:L \
	<img src='imgs/icons/chat/sad.png' alt='d'/>:(<br/>\
	<img src='imgs/icons/chat/confused.png' alt='e'/>:S \
	<img src='imgs/icons/chat/angel.png' alt='A'/>:A \
	<img src='imgs/icons/chat/embarrassed.png' alt='4'/>:4 \
	<img src='imgs/icons/chat/glasses-nerdy.png' alt='h'/>:H\
	</div>");		   
	$("#smiles_"+chatboxtitle).css('display','none');
	
	$("#chatbox_"+chatboxtitle).css('bottom', '0px');
	
	chatBoxeslength = 0;

	for (x in chatBoxes) {
		if ($("#chatbox_"+chatBoxes[x]).css('display') != 'none') {
			chatBoxeslength++;
		}
	}

    maxwin = Math.round(screen.width/252);
	if (chatBoxeslength == 0) {
		$("#chatbox_"+chatboxtitle).css('right', '26px');
	} else {
/* width + startgap + gap tra chatwin */
        if (chatBoxeslength<maxwin)
        {
    		width = chatBoxeslength*225+26+chatBoxeslength*1;
	    	$("#chatbox_"+chatboxtitle).css('right', width+'px');
        }
        else
	    	$("#chatbox_"+chatboxtitle).css('left', '2px');
	}
	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+" .chatboxcontent").click(function() {
		if ($('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display') != 'none') {
			$("#chatbox_"+chatboxtitle+" .chatboxtextarea").focus();
		}
	});
    
	$("#chatbox_"+chatboxtitle+" .chatboxtitle").click(function() {
		if ($('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display') != 'none') {
			$("#chatbox_"+chatboxtitle+" .chatboxtextarea").focus();
		}
	});
    
	$("#chatbox_"+chatboxtitle+" .chatboxoptions").click(function() {
		if ($('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display') != 'none') {
			$("#chatbox_"+chatboxtitle+" .chatboxcontent").focus();
		}
	});
    
	$("#chatbox_"+chatboxtitle).show();
}

function xplay(file) {
 if (navigator.appName == "Microsoft Internet Explorer")
 {
  var embed = document.createElement("embed"); 
  embed.setAttribute('src', file);
  embed.setAttribute('hidden', true);
  embed.setAttribute('autostart', true);
  document.body.appendChild(embed);
 }
 else
 {
  var snd = new Audio(file);
  snd.play();
 }
}

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) {
                    xplay("http://www.silae.it/scripts/utopiacr.wav");
					document.title = x+' says...';
					titleChanged = 1;
					break;	
				}
			}
		}
		
		if (titleChanged == 0) {
			document.title = originalTitle;
			blinkOrder = 0;
		} else {
			++blinkOrder;
		}

	} else {
//       if (newMessagesWin.length>0)
		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) {
// VB SHOW LIST
    $("#mainchat").html("<p class='style5b'><img src='imgs/logos/silae_logo_mini.png' alt='silae' /> Live - <span class='style6b cdred'>"+data.date+"</span><br/><span class='cevidg'> Active Users:</span> <span class='style4b cred'> "+data.active+" </span><br/></p><hr/>");
    if (data.mlista)
    {
     if (data.logged == '1')
     {
      $.each(data.mlista, function(i,ele){
       if (ele['idle'] == 1) imgs = "<img class='chatimg2' src='imgs/sleep.png' alt='sleep'/>";
       else imgs = "<img class='chatimg2' src='imgs/icons/tick.png' alt='wakeup' width='10px' height='10px'/>";

//caricata dinamicamente a ogni beat. diverso da inizio.
       $("#mainchat").append('<div class="padd0 alt0 vmid clear"><a href="main.php?action=anagrafica&cognome='+ele.cognome+'&nome='+ele.nome+'&email='+ele.email+'&uid='+ele.uid+'&usrtype=DEF" title="'+ele.uname+'-'+ele.pathphoto+'"><img class="bord0 chatimg" src="imgs/utenti/tmb/'+ele.pathphoto+'" alt="img"/></a><a class="vbmess" onclick="javascript:chatWith(\''+ele.uname+'\',\''+ele.cognome+'\',\''+ele.nome+'\',\''+ele.email+'\',\''+ele.uid+'\',\'\');"> '+ele.uname+' '+imgs+'</a></div>');
      });
     }
     else
      $("#mainchat").append('<div class="alt0 clear"><a class="style5b cevidg" href="main.php?action=accedi">--Login to chat--</a></div>');
    }
    else
     if (data.logged == '1')
      $("#mainchat").append('<div class="alt0 clear"><p class="style5b vbmess">No users online</p></div>');
     else
     {
      $("#mainchat").append('<div class="alt0 clear"><a class="style5b cevidg" href="main.php?action=accedi">--Login to chat--</a></div>');
     }

    $("#mainchat").scrollTop($("#mainchat").scrollHeight);
// VB  aggiornamento dinamico mainchat
    if (data.active == 0) linee=1; else linee = data.active;
    altc = (linee * 24) + 52; //24 height img+bord
    if (altc > 210) altc = 210;
  	$("#mainchat").css('height',altc+'px');

// VB SHOW LIST

		$.each(data.items, function(i,item){
			if (item)	{ // fix strange ie bug

				chatboxtitle = item.f;

				if ($("#chatbox_"+chatboxtitle).length <= 0) {
					createChatBox(chatboxtitle,0,0,0,0,0,0); //added 7nd par, 0 VB
				}
				if ($("#chatbox_"+chatboxtitle).css('display') == 'none') {
					$("#chatbox_"+chatboxtitle).css('display','block');
					restructureChatBoxes();
				}
				
				if (item.s == 1) {
					item.f = username;
				}

				$("#smiles_"+chatboxtitle).css('display','none');
				if (item.s == 2) {
    				$("#chatbox_"+chatboxtitle+" .hiddenmsg").append('&lt;b&gt;'+item.m+'&lt;/b&gt;&lt;br/&gt;');
					$("#chatbox_"+chatboxtitle+" .chatboxcontenttab").append('<tr><td class="style6b" colspan="2">'+item.m+'</td></tr>');
//					$("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxinfo">'+item.m+'</span></div>');
				} else {
					newMessages[chatboxtitle] = true;
					newMessagesWin[chatboxtitle] = true;
					item.m = ChatSmiles(item.m);
    				$("#chatbox_"+chatboxtitle+" .hiddenmsg").append('&lt;b&gt;'+item.f+'&lt;/b&gt; 2('+item.t+'): &nbsp; &lt;i&gt;'+item.m+'&lt;/i&gt;&lt;br/&gt;');
					$("#chatbox_"+chatboxtitle+" .chatboxcontenttab").append('<tr><td class="style6b">'+item.f+':</td><td class="style6n cevidg right">1('+item.t+')</td></tr><tr><td colspan="2" class="msg">'+item.m+'</td></tr>');
//					$("#chatbox_"+chatboxtitle+" .chatboxcontent").append('&bull;'+item.f+'&bull;1 '+item.t+':\n'+item.m+'\n');
//					$("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxmessagefrom">'+item.f+':&nbsp;&nbsp;</span><span class="chatboxmessagecontent">'+item.m+'</span></div>');
				}

        		$("#mainchat").css('display','none');
				$("#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 += minimizedChatBoxes[i]+'|';
            }
        }
/* correzione da sito orig. VB
		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');
	}
	
}

function checkChatBoxInputKey(event,chatboxtextarea,chatboxtitle) {
	 
	if(event.keyCode == 13 && event.shiftKey == 0) {
		message = $(chatboxtextarea).val();
		message = message.replace(/^\s+|\s+$/g,"");
        adesso = '';
		$(chatboxtextarea).val('');
		$(chatboxtextarea).focus();
/*		$(chatboxtextarea).css('height','44px'); VB */
		if (message != '') {
         $.ajax({
	        url: "chat.php?action=gettime",
	        cache: false,
            async: false,
	        dataType: "json",
	        success: function(data) {
            adesso = data.t;
         }});
			$.post("chat.php?action=sendchat", {to: chatboxtitle, message: message} , function(data){
				message = message.replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\"/g,"&quot;");
				message = ChatSmiles(message);
				$("#smiles_"+chatboxtitle).css('display','none');
				$("#chatbox_"+chatboxtitle+" .hiddenmsg").append('&lt;b&gt;'+username+'&lt;/b&gt; 2('+adesso+'): &nbsp; &lt;i&gt;'+message+'&lt;/i&gt;&lt;br/&gt;');
				$("#chatbox_"+chatboxtitle+" .chatboxcontenttab").append('<tr><td class="style6b">'+username+':</td><td class="style6n cevidg right"> 2('+adesso+')</td></tr><tr><td colspan="2" class="msg">'+message+'</td></tr>');
//				$("#chatbox_"+chatboxtitle+" .chatboxcontent").append('&bull;'+username+'&bull;2 '+adesso+':\n'+message+'\n');
//				$("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<span class="chatboxmessagefrom">&bull;'+username+'&bull;:'+adesso+'</span>\n<span class="chatboxmessagecontent">'+message+'</span>');
				$("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .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');
	}
	 
}

function startChatSession(){
	$.ajax({
	  url: "chat.php?action=startchatsession",
	  cache: false,
	  dataType: "json",
	  success: function(data) {
		username = data.username;

		$.each(data.items, function(i,item){
			if (item)	{ // fix strange ie bug

				chatboxtitle = item.f;
                content = $("#chatbox_"+chatboxtitle+" .hiddenmsg").val();

				if ($("#chatbox_"+chatboxtitle).length <= 0) {
					createChatBox(chatboxtitle,1,0,0,0,0,0);
				}
				
				if (item.s == 1) {
					item.f = username;
				}

				$("#smiles_"+chatboxtitle).css('display','none');
				if (item.s == 2) { //sent at xxx
                    $("#txt_"+chatBoxes['"+chatboxtitle+"']).append("1."+item.m+"\n");
    				$("#chatbox_"+chatboxtitle+" .hiddenmsg").append('&lt;b&gt;'+item.m+'&lt;/b&gt;&lt;br/&gt;');
					$("#chatbox_"+chatboxtitle+" .chatboxcontenttab").append('<tr><td colspan="2" class="style5bi cevidg">'+item.m+'</td></tr><tr><td colspan="2">&nbsp;</td></tr>');
//					$("#chatbox_"+chatboxtitle+" .chatboxcontent").append(item.m+'\n');
//					$("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<span class="chatboxinfo">'+item.m+'</span>');
				} else {
					item.m = ChatSmiles(item.m);
                    $("#txt_"+chatBoxes['"+chatboxtitle+"']).append("2."+item.f+": "+item.m+"\n");
    				$("#chatbox_"+chatboxtitle+" .hiddenmsg").append('&lt;b&gt;'+item.f+'&lt;/b&gt; 2(-): &nbsp; &lt;i&gt;'+item.m+'&lt;/i&gt;&lt;br/&gt;');
					$("#chatbox_"+chatboxtitle+" .chatboxcontenttab").append('<tr><td class="style6b">'+item.f+':</td><td class="style6n cevidg right">3</td></tr><tr><td colspan="2" class="msg">'+item.m+'</td></tr>');
//					$("#chatbox_"+chatboxtitle+" .chatboxcontent").append('&bull;'+item.f+'&bull;3:\n'+item.m+'\n');
//					$("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<span class="chatboxmessagefrom">&bull;'+item.f+'&bull;:</span>\n<span class="chatboxmessagecontent">'+item.m+'</span><br/>');
				}
			}
		});
		
		for (i=0;i<chatBoxes.length;i++) {
			chatboxtitle = chatBoxes[i];
			setTimeout('$("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);',100);
// yet another strange ie bug
		}
	
	setTimeout('chatHeartbeat();',chatHeartbeatTime);		
//VB adattamento dinamico all'apertura; dopo è in chatbt
    if (data.nit == 0) linee=1; else linee = data.nit;
    altc = (linee * 24) + 52;
    if (altc > 210) altc = 210;
  	$("#mainchat").css('height',altc+'px');
    $("#mainchat").scrollTop(500);
	}});

}

/**
 * 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;
    }
};

