// JavaScript Document

function recibirPensamientos(){
new Ajax.Request('recibirPensamientos.php',
  {
	  parameters: $("frmPensamientos").serialize(),
	  onSuccess:function(transport){
		  $('txtPensamiento').value='';
		  cargarHomeOnline();
	  }
  });
}

function recibirEventoItem(lista_id){
new Ajax.Request('recibirEventoItem.php?lista_id='+lista_id,
	{
		onLoading: showLoad,
		onSuccess: function(transport){
			$('divLoad').innerHTML="";
			cargarVerMisListas();
		}
  });	
}

function cargarMisEventos(id, estaOnline){
new Ajax.Request('cargarMisEventos.php?usr_id='+id,
	{
		onSuccess: function(transport){
		var datos = transport.responseText.evalJSON() || "Se produjo un error en el servidor";
		$('divEventos').innerHTML="";
		if(estaOnline){
			armarEventos(datos, 0, '#E3F2F2', true);
		}else{
			armarEventos(datos, 0, '#FFFFFF', false);
		}
	}
  });
}

function cargarEventos(){
new Ajax.Request('cargarEventos.php',
	{
		onSuccess: function(transport){
		var datos = transport.responseText.evalJSON() || "Se produjo un error en el servidor";
		$('divEventos').innerHTML="";
		armarEventos(datos, 0, '#E3F2F2', true);
	}
  });	
}

function armarEventos(datos, i, bgcolor, estaOnline){
html="<table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">";
j=0;
i2=i;
while(j<5 && i<datos.length){
	var img="";
	var texto="";
	var pensamiento = "";
	var btnBorrar="";
	var invitacion="";
	pensamiento = datos[i].pensamiento;
	img="<a href=\"#\" id=\"lnkVerPerfil"+i+"\">";
	if(datos[i].id2!="no" && datos[i].invitacion != 1){
		texto =  "<a href=\"#\" id=\"lnkVerPerfilAmigo1_"+i+"\" class=\"enlaceg\">"+datos[i].nombre+"</a> y ";
		texto += "<a href=\"#\" id=\"lnkVerPerfilAmigo2_"+i+"\" class=\"enlaceg\">"+datos[i].nombre2+"</a> "+datos[i].pensamiento;
		pensamiento = "";
		btnBorrar = "";
	}else{
		img+=datos[i].thumb;
		if(datos[i].pregunta!="no"){
			if(datos[i].nombre != ""){
				texto=datos[i].nombre+"<br>"+datos[i].pregunta;
			}else{
				texto=datos[i].pregunta;
			}
		}else{
			if(datos[i].nombre != ""){
				texto=datos[i].nombre;
			}

		}
		
		if(datos[i].idevento != ""){
			btnBorrar  = "<a href=\"#\" id=\"lnkBorrarEvento"+datos[i].idevento+"\" class=\"enlaceg\">";
			btnBorrar += "<img src=\"img/btn_eliminar.gif\" border=\"0\"></a>";
		}else{
			btnBorrar = "";
		}
	}
	
	// Invitacion a ver las listas del usuario (por cumpleaņos o alguna otra razon)
	if(datos[i].invitacion == 1){
		invitacion = "<br>Mira sus listas haciendo <a href=\"#\" id=\"lnkVerPerfilInvitacion"+i+"\" class=\"enlace\"><b>click aqu&iacute;</b></a>";
		pensamiento += invitacion;
	}
	
	img+="</a>";
	html+="	<tr>";
	html+="     <td align=\"center\">";
	html+="     <hr class=\"lineaSeparadora\">";

if(datos[i].lista_id == 0 || datos[i].datosLista == ""){
		html+="     	<table bgcolor=\""+bgcolor+"\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">";
		html+="             <tr>";
		html+="             	<td rowspan=\"2\" style=\"padding:5px;\" valign=\"top\"><a name=\""+i+"\">"+img+"</a></td>";
		html+="					<td width=\"100%\" style=\"padding-left:10px;padding-right:10px;\" align=\"left\">";
		html+="						<span class=\"grandebolder\">"+texto+"</span></td>";
		html+="					<td style=\"padding-left:10px;padding-right:10px;\">";
		html+="						"+btnBorrar+"</td>";
	//	html+="					<td bgcolor=\"#cdecee\" style=\"vertical-align:top;padding-right:10px;\" align=\"right\">"+btnBorrar+"</td>";
		html+="             </tr>";
		html+="             <tr>";
		html+="             	<td colspan=\"2\" style=\"padding:10px 10px 0px 10px;\" align=\"left\"><span class=\"texto\">"+pensamiento+"</span></td>";
		html+="             </tr>";
		html+="         </table>";
	}else{
		html+="     	<table bgcolor=\"#B6DBE1\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">";
		html+="             <tr>";
		html+="             	<td style=\"padding:5px;\" valign=\"top\"><a name=\""+i+"\">"+img+"</a></td>";
		html+="					<td width=\"41%\" style=\"padding:10px 5px 0px 10px;\" align=\"left\" valign=\"top\">";
		html+="						<span class=\"textobmbolder\">"+texto+"</span><br><span class=\"textob\">"+pensamiento+"</span></td>";
		html+="					<td width=\"59%\" style=\"padding:5px;\">";
		html+="						"+datos[i].datosLista+"</td>";
	//	html+="					<td bgcolor=\"#cdecee\" style=\"vertical-align:top;padding-right:10px;\" align=\"right\">"+btnBorrar+"</td>";
		html+="             </tr>";
		html+="         </table>";
	}
	
	html+="  	</td>";
	html+="	</tr>";
	j++;
	i++;
}
html+="</table>";
$('divEventos').innerHTML+=html;

if(i<datos.length){
	divVerMas = "<div align=\"right\" style=\"padding:0px;\"><hr class=\"lineaSeparadora\"><a id=\"lnkVerMas\" style=\"cursor:pointer\">";
	if(estaOnline){
		divVerMas += "<img src=\"img/btn_vermas.jpg\" border=\"0\" /></a></div>";
	}else{
		divVerMas += "<img src=\"img/vermasc.gif\" border=\"0\" /></a></div>";
	}
	$('divVerMas').innerHTML=divVerMas;
}else{
	$('divVerMas').innerHTML="";
}

j=0;
while(j<5 && i2<datos.length){
	if(datos[i2].invitacion == 1){
		Event.observe('lnkVerPerfil'+i2, 'click', recibirPerfilP.curry(datos[i2].id2, 'home', ""), false);
		Event.observe('lnkVerPerfilInvitacion'+i2, 'click', recibirPerfilP.curry(datos[i2].id2, 'home', ""), false);
	}else{
		Event.observe('lnkVerPerfil'+i2, 'click', recibirPerfilP.curry(datos[i2].id, 'home', datos[i2].nombre), false);
		if(datos[i2].id2!="no"){
			Event.observe('lnkVerPerfilAmigo1_'+i2, 'click', recibirPerfilP.curry(datos[i2].id, 'home', ""), false);
			Event.observe('lnkVerPerfilAmigo2_'+i2, 'click', recibirPerfilP.curry(datos[i2].id2, 'home', ""), false);
		}
	}
	if(datos[i2].idevento != ""){
		Event.observe('lnkBorrarEvento'+datos[i2].idevento, 'click', validarEliminarEvento.curry(datos[i2].idevento, "home"), false);
	}
	j++;
	i2++;
}

Event.stopObserving('lnkVerMas', 'click');
Event.observe('lnkVerMas', 'click', armarEventos.curry(datos, i, bgcolor, estaOnline),false);
//		Event.observe('lnkResponderPensamiento', 'click', cargarResponder);
}

function validarEliminarEvento(idevento, ira){
	new Ajax.Request('cargarFlotanteMensajeSINO.php', {
//		onLoading: showLoad,
		onSuccess: function(transport){
			var response = transport.responseText || "Se produjo un error en el servidor";
			mostrarVentanaFlotante(response);
			
			$('txtMensaje').innerHTML="Est&aacute;s seguro que quieres eliminar el mensaje?";
			
			Event.observe('btnSi', 'click', eliminarEvento.curry(idevento, ira), false);
			Event.observe('btnSi', 'click', function(){ocultarVentanaFlotante();}, false);
			Event.observe('btnNo', 'click', function(){ocultarVentanaFlotante();}, false);
		}
	});
}

function eliminarEvento(idevento, ira){
	new Ajax.Request('recibirEliminarEvento.php?evento_id='+idevento,
		{
			onSuccess: function(transport){
			switch (ira){
				case "home":
					cargarHomeOnline();
					break;
				case "miseventos":
					cargarMisEventos(0, true);
					break;
			}
			}
	});	
}

function validarEliminarComentarioLista(idComentario, usr_id){
	new Ajax.Request('cargarFlotanteMensajeSINO.php', {
//		onLoading: showLoad,
		onSuccess: function(transport){
			var response = transport.responseText || "Se produjo un error en el servidor";
			mostrarVentanaFlotante(response);
			
			$('txtMensaje').innerHTML="Est&aacute;s seguro que quieres eliminar el comentario?";
			
			Event.observe('btnSi', 'click', eliminarComentarioLista.curry(idComentario, usr_id), false);
			Event.observe('btnSi', 'click', function(){ocultarVentanaFlotante();}, false);
			Event.observe('btnNo', 'click', function(){ocultarVentanaFlotante();}, false);
		}
	});
}


function eliminarComentarioLista(idcomentario, usr_id){
new Ajax.Request('recibirEliminarComentarioLista.php?comentario_lista='+idcomentario,
	{
		onSuccess: function(transport){
			cargarListas(0, usr_id);
		}
  	});	
}