jQuery(function( $ ){
				
	/**************************/
	/* ACCIONES INPUT MENSAJE */
	/**************************/
	$('input#expresarse_txt').focus(function() {
		var value=$(this).val();
		if( escape(value) == 'Expr%E9sate...' )
			$(this).val('');
	}).blur(function() {
		var value=$(this).val();
		if(value == '') $(this).val(unescape('Expr%E9sate...'));
	});
	
	/**************************/
	/*  SCROLL BAR DEL CHAT   */
	/**************************/
	/*$('div#expresate-mensajes').jScrollPane({showArrows:false, scrollbarWidth: 24});
	var oldscrollHeight = $('div#expresate-mensajes').attr("scrollHeight") - 20;
	$('div#expresate-mensajes')[0].scrollTo(oldscrollHeight);*/
	
	/****************************************/
	/*  FUNCION PARA LEER MENSAJES NUEVOS   */
	/****************************************/
	$.PeriodicalUpdater({
		url : 'includes/readmsg.php',
		method : 'post',
		minTimeout: 10000,
		maxTimeout: 5000,
		multiplier: 2
	},
	function(datos){
		var oldscrollHeight = $('div#expresate-mensajes').attr("scrollHeight") - 20;
		$('ul#mensajes-expresados').html(datos);
		$('div#expresate-mensajes').jScrollPane({showArrows:false, scrollbarWidth: 24});
		//Auto-scroll			
		var newscrollHeight = $('div#expresate-mensajes').attr("scrollHeight") - 20;
		if( newscrollHeight >= 210 && newscrollHeight > oldscrollHeight){
			$('div#expresate-mensajes')[0].scrollTo(newscrollHeight);
		}
	});
	
	/****************************/
	/* OVER DEL SCROLL DEL CHAT */
	/****************************/
	$('div.jScrollPaneDrag').mouseover(function(){
		$(this).css('backgroundPosition', '0 -39px');
	}).mouseleave(function(){
		$(this).css('backgroundPosition', '0 0');
	});
	
	$('input#expresarse_txt').keyup(function(e) {
		if(e.keyCode == 13) {
			addMSG();
		}
	});
	
	$('#expresate_aviso').css({ display: 'none', visibility: 'visible' });
	
});
/********************************/
/* AGREGAMOS EL MENSAJE AL CHAT */
/*************************** ****/
function addMSG()
{
	var mensaje = $('input#expresarse_txt').val();
	if( mensaje == "" || mensaje == "Expr&eacute;sate..." )
	{
		expresateAviso(false, 'Escribe un mensaje.');
	} else {
		$.ajax({
				type: "POST",
				url: "includes/addmsg.php",
				cache: false,
				data: "mensaje="+mensaje,
				success: function(datos){
					if( datos.substr(0, 5) == "Error" )
					{
						expresateAviso(false, datos.substr(7));
					} else {
						$('input#expresarse_txt').val('');
						readMSG();
					}
			  }
		});
	}
}
/********************************/
/* LEEMOS LOS MENSAJES DEL CHAT */
/*************************** ****/
function readMSG()
{
	var oldscrollHeight = $('div#expresate-mensajes').attr("scrollHeight") - 20;
	$.ajax({
			type: "POST",
			url: "includes/readmsg.php",
			cache: false,
			success: function(datos){
				$('ul#mensajes-expresados').html(datos);
				$('div#expresate-mensajes').jScrollPane({showArrows:false, scrollbarWidth: 24});
				//Auto-scroll			
				var newscrollHeight = $('div#expresate-mensajes').attr("scrollHeight") - 20;
				if( newscrollHeight >= 210 && newscrollHeight > oldscrollHeight){
					$('div#expresate-mensajes')[0].scrollTo(newscrollHeight);
				}
		  }
	});
}
/*******************************/
/* MOSTRAR EL MENSAJE DE AVISO */
/*******************************/
function expresateAviso(forzar, msg)
{
	var estadoActual = 'none';
	if( forzar == true ) estadoActual = "block";
	$('#expresate_aviso_msg').html(msg);
	if( estadoActual == "none" ) {
		if($.browser.msie) // si es IE
			$('#expresate_aviso').show();
		else
			$('#expresate_aviso').fadeIn("slow");
	} else {
		if($.browser.msie) // si es IE
			$('#expresate_aviso').hide();
		else
			$('#expresate_aviso').fadeOut("slow");
	}
}