// JavaScript Document

/*
Función para evitar errores en terminales sin el plugin FireBug del Mozilla.
*/
function _mylog(que){	
	if ( typeof loadFirebugConsole == 'function' ) {
		console.log(que); // Mostrar el log
	} 
}

function rp_cerrar_session() {
$("#cerrar_sesion").click(function() {
		var datos="accion=logout"
		$.post("ajax.gateway.php", datos,
			function (resp) { 			
				if (resp[0]=="OK") {
					// window.location(document.URL);
					_mylog(document.URL);
					location.href=document.URL;					
				}
				
		}, "json");
		return false;
	});
}


/* =CHAT */ 
/*
rp_parsearComentarios()

@param   nothing	nothing
@return  nothing	nothing
@access  public
*/ 
function rp_parsearComentarios( matriz ) {
	var idx_mensaje=0;
	mensajes="";
	$.each(matriz, function(i,item){
		if (idx_mensaje==0) {
			chatID=item["chatID"];
		}
		mensajes+='<li>';
		mensajes+='<h3><strong>';
		mensajes+=item["nombre"];
		mensajes+='</strong> dice:</h3>';				
		mensajes+=item["comentario"];	
		// mensajes+=' ('+item["tiempo_transcurrido"]+")";									
		mensajes+='</li>\r\n';												
		idx_mensaje++;
	})
	return (mensajes) ;
}

/*
rp_listarChat()

@param   nothing	nothing
@return  nothing	nothing
@access  public
*/ 
function rp_listarChat() {
		$('#mensajes_chat').html("");
		var datos="accion=rp_listarChat"
		$.post("ajax.gateway.php", datos,
			function (resp) { 			
				if (resp[0]=="OK") {
					mensajes=rp_parsearComentarios( resp[1] );
					$('#mensajes_chat').hide().html(mensajes).show();
				}
				
		}, "json");
}

/*
rp_actualizarChat()

@param   nothing	nothing
@return  nothing	nothing
@access  public
*/ 
function rp_actualizarChat() {
			var datos="accion=rp_actualizarChat&chatID="+chatID;
			$.post("ajax.gateway.php", datos,
				function (resp) { 			
					if (resp[0]=="OK") {
						mensajes=rp_parsearComentarios( resp[1] );
						mensajesviejos=$('#mensajes_chat').html()
						$('#mensajes_chat').hide().html( mensajes + mensajesviejos).show();
					}
					
			}, "json");
	
}
	
/*
rp_nuevoComentario()

@param   nothing	nothing
@return  nothing	nothing
@access  public
*/ 
function rp_nuevoComentario() {
	$("#enviar_mensaje").submit(function() {
		usuario=$('#usuario').val();
		texto_mensaje=$('#texto_mensaje').val();
		var datos="accion=rp_nuevoComentario&chatID="+chatID+"&usuario="+usuario+"&texto_mensaje="+texto_mensaje
		$.post("ajax.gateway.php", datos,
			function (resp) { 			
				if (resp[0]=="OK") {
					mensajes=rp_parsearComentarios( resp[1] );
					mensajesviejos=$('#mensajes_chat').html()
					$('#mensajes_chat').hide().html( mensajes + mensajesviejos).show();
					$('#texto_mensaje').val("");
					$('#texto_mensaje').focus();
				}
				
		}, "json");
	
		return false
	});
}

