
function apriChat(){
	if (chatOpened == 0) {
		chatOpened =1;
		
		$('#chattina').fadeIn()
		
		startChatSimba = 1;
		
		//torno alla chattina
		shiftUtenti(1);
		
		//Movimento e ridimensionamento chat
		if(!$('#chattina').mb_getState('closed')){
			var w = 280
			var h = 350
			if (!w) w=$('#chattina').outerWidth();
			if (!h) h=$('#chattina').outerHeight();
			var elHeight= h-$('#chattina').find(".n:first").outerHeight()-($('#chattina').find(".s:first").outerHeight());
			$('#chattina').find(".c:first , .mbcontainercontent:first").animate({"height": elHeight},200);
			$('#chattina').animate({"height":h,"width":w, "top":-360, "left":0},300,function(){
				//$('#chattina').adjastPos();
			});
		}
		
		//riposiziono il cursore
		document.getElementById("simbaContentChat2").scrollTop = document.getElementById("simbaContentChat2").scrollHeight;
		//alert(document.getElementById("simbaContentChat2").scrollHeight);
		//document.getElementById("simbaContentChat2").scrollHeight;
		$('#chatButton').attr("class","chatButtonSelected");
		$('#tizioScrive').attr("class","tizioScriveOff");
		
		
		
	}else{
		if (chatOpened == 1) {
			chatOpened =0;
			$('#chattina').fadeOut();
			$('#chatButton').attr("class","chatButtonUnselected");
			$('#tizioScrive').attr("class","tizioScriveOn");
			startChatSimba = 0;
		}
	}

	//setto il parametro
	$.ajax({
		type: "GET",
		url: "/interactive/simbachat/chat_setta_aperta.asp",
		data:({cod3 : chatOpened}),
		dataType: "html",
		success: function(msg){
			//recupera i messaggi
			//alert(msg);
			//chatSimba();
			
		}
	});
}


//salva il messaggio e recupera la chat
function handleEnter (field, event) {
	var myTesto = document.getElementById("simbaInputChat");
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13) {
		//break;
		if (field.value != ""){
			//salvaMsgChatSimba();
			//alert(myTesto.value);
			/*$.ajaxSetup({
				'beforeSend' : function(xhr) {
					//xhr.overrideMimeType('text/html; charset=iso-8859-1');
				},
			});*/
			$.ajax({
				type: "POST",
				url: "/interactive/simbachat/chat_addcomm.asp",
				contentType: "application/x-www-form-urlencoded; charset=iso-8859-1",
				//scriptCharset: "iso-8859-1",
				//data: "name=John&location=Boston",
				//data: "nomignolo=" + nomignolo + "&testo=" + myTesto.value + "&nbsp;d&data=10/01/08",
				//testo: 'ciao',
				data:({testo : myTesto.value}),
				dataType: "html",
				success: function(msg){
					//recupera i messaggi
					//alert(msg);
					chatSimba();
					
				}
			});
		}
		myTesto.value='';
		myTesto.focus();
		return false;
	} 
	else
	return true;
}      



//Recupera i messaggi e li mette nel box
function chatSimba(){
	if (startChatSimba == 1){
		/*$.ajaxSetup({
			'beforeSend' : function(xhr) {
				xhr.overrideMimeType('text/html; charset=utf-8');
			},
		});*/
		$.ajax({
		   type: "GET",
		   url: "/interactive/simbachat/chat_recupera.asp",
		   scriptCharset : "utf-8",
		   //data: "name=John&location=Boston",
		   cache : false,
		   dataType: "html",
		   success: function(msg){
			 //alert( "Data Saved: " + msg );
				var myChat2 = document.getElementById("simbaContentChat2");
				
				
				
				$('#simbaContentChat2').height(($('#mbcontainercontentChat').innerHeight()-26) + 'px');
				//alert($('chattina').innerHeight());
				$('#simbaContentChat2').css('overflow','auto');
				
				//myChat2.style.height = (myChatMbContent.clientHeight - 26) + "px"
				//myChat2.style.overflow = "auto";
				
				
				//controlla sel il cursore � in fondo
				
				var myScroll = 1;
				if (myChat2.scrollTop == myChat2.scrollHeight - myChat2.clientHeight){
					myScroll = 1
				}else{
					myScroll = 0
				}
				
				myChat2.innerHTML = msg;
				
				//se il cursore � in fondo lo riporta in fondo
				if (myScroll == 1) myChat2.scrollTop = myChat2.scrollHeight;
				setTimeout("chatSimba()",0.5*1000);
		   }
		 });
	}	
	
}


function shiftUtenti(panel){
	if (panel==0){
		//aperta la chat
		
		$.ajax({
			type: "POST",
			url: "/interactive/simbachat/chat_recupera_utenti.asp",
			cache : false,
			//data: "name=John&location=Boston",
			data: "",
			success: function(msg){
				//alert( "Data Saved: " + msg );
				//shiftChatUtenti = 1;
				startChatSimba = 0;
				$('#linkChattina').css("color","#000000")
				//$('#linkChattina').attr("onclick","shiftUtenti()");
				$('#linkUtenti').css("color","#990000");
				//$('#linkUtenti').attr("onclick","");
				$('#mbcontainercontentChat').html(msg);
			}
		})
	}else{
		//aperto box utenti
		//shiftChatUtenti = 0
		$('#mbcontainercontentChat').html('<div id="simbaContentChat2" style=""></div><input type="text" id="simbaInputChat" onKeyPress="return handleEnter(this, event)" style="margin-top:5px;"/>');
		startChatSimba = 1
		chatSimba();
		$('#linkChattina').css("color","#990000")
		$('#linkUtenti').css("color","#000000");
		
	}
}

