
// EL GRAN HERMANO TE VIGILA

google.load("jquery","1.3");

var raton_nota=false;
var senna_focus=false;
var click=false;
var marcar=false;

function crear_cookie(nombre,valor) {
	var fecha=new Date();
	fecha.setTime(fecha.getTime()+5000*24*60*60*1000);
	document.cookie=nombre+"="+valor+"; path=/; expires="+fecha.toGMTString();
}

function desmarcar(event) {
	event.preventDefault();
	if(marcar==true) {
		crear_cookie("marcar","false");
		marcar=false;
		$("input").attr("checked",false);
	} else {
		crear_cookie("marcar","true");
		marcar=true;
		$("input").attr("checked",true);
	}
}

function desfollowear(ev,id,usuario,senna) {
	ev.preventDefault();
	$.ajax({
		type: "GET",
		url: "follow.php",
		contentType: "application/x-www-form-urlencoded",
		dataType: "html",
		data: "a=desfollow&u="+id+"&usuario="+usuario+"&senna="+senna+"&aviso="+$("#aviso-"+id).attr("checked"),
		beforeSend: function() {
			$("#usu-"+id).replaceWith($("<img />").attr("id","usu-"+id).attr("src","ola.gif").attr("alt","Espérate"));
		},
		success: function() {
			$("#usu-"+id).replaceWith($("<span />").attr("id","usu-"+id).text("¡Guay!"));
		}			
	});
}

function followear(ev,id,usuario,senna) {
	ev.preventDefault();
	$.ajax({
		type: "GET",
		url: "follow.php",
		contentType: "application/x-www-form-urlencoded",
		dataType: "html",
		data: "a=follow&u="+id+"&usuario="+usuario+"&senna="+senna,
		beforeSend: function() {
			$("#usu-"+id).replaceWith($("<img />").attr("id","usu-"+id).attr("src","ola.gif").attr("alt","Espérate"));
		},
		success: function() {
			$("#usu-"+id).replaceWith($("<span />").attr("id","usu-"+id).text("¡Guay!"));
		}			
	});
}

window.onload=function() {
	$("#senna").focus(function() {
		$("#nota").slideToggle("fast");
	});

	$("#limita").mouseover(function() {
			click=true;
	});

	$("#limita").mouseout(function() {
		click=false;
	});

	$("#senna").blur(function() {
		if(!click) {
			$("#nota").slideToggle("fast");
		}
	});

	// Vale, y ahora las cosas chupi-AJAX.

	$("#formu").submit(function(event) {
		event.preventDefault();
		var tu_param="";
		var separa="";
		var inputs=document.getElementById("inputs").getElementsByTagName("input");

		for(var i=0;i<3;i++) {
			tu_param+=separa+inputs[i].name+"="+encodeURIComponent(inputs[i].value);
			separa="&";
		}
	
		$.ajax({ // Menucito
			type: "POST",
			url: "tu.php",
			contentType: "application/x-www-form-urlencoded",
			dataType: "html",
			data: tu_param,
			

			beforeSend: function() {
				var res=$("<img />").attr("src","res.gif").attr("alt","RES");
				$("#formu").replaceWith(res);
			},

			success: function(datos) {
				$("#tu").replaceWith(datos);
			}
		});

		$.ajax({ // Cosa del medio
			type: "POST",
			url: "procesar.php",
			contentType: "application/x-www-form-urlencoded",
			dataType: "html",
			data: tu_param,

			beforeSend: function() {
				var mostro=$("<img />").attr("src","mostro.gif").attr("alt","RES").attr("id","mostro");
				$("#texto").replaceWith(mostro);
			},

			success: function(datos) {
				$("#mostro").replaceWith(datos);
			}
		});
	});
};


/*var tu;
var ajax;

function inicio() {
	if(window.XMLHttpRequest) {
		//xhr=new XMLHttpRequest();
		tu=new XMLHttpRequest();

		ajax=true;
	} else {
		ajax=false;
	}
}


function pilla() {

	//xhr.open("POST","procesar.php",true);
	//xhr.overrideMimeType("text/xml");

	var tu_param="";
	var separa="";
	var inputs=document.getElementById("inputs").getElementsByTagName("input");

	for(var i=0;i<3;i++) {
		tu_param+=separa+inputs[i].name+"="+encodeURIComponent(inputs[i].value);
		separa="&";
	}
	alert(tu_param);

	var res=document.createElement("p");
	res.setAttribute("id","res");
	res.appendChild(document.createElement("img"));
	res.firstChild.setAttribute("src","res.gif");
	res.firstChild.setAttribute("alt","Cargando...");
	document.getElementById("formul").replaceChild(
			res,document.getElementById("tu"));

	var mostro=document.createElement("p");
	mostro.setAttribute("id","mostro");
	mostro.appendChild(document.createElement("img"));
	mostro.firstChild.setAttribute("src","mostro.gif");
	mostro.firstChild.setAttribute("alt","Cargando...");
	var nuevo_texto=document.createElement("div");
	nuevo_texto.setAttribute("id","texto");
	nuevo_texto.setAttribute("style","text-align: center;");
	nuevo_texto.appendChild(mostro);
	document.getElementById("contenido").replaceChild(
			mostro,document.getElementById("texto"));

	tu.open("POST","tu.php",true);
	tu.overrideMimeType("text/xml");
	tu.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	tu.setRequestHeader("Content-Length",tu_param.length);
	
	tu.onreadystatechange=function() { 
		if(tu.readyState==4) {
			var resp=tu.responseText;
			document.getElementById("res").innerHTML=resp;
		}							
	}

	tu.send(tu_param);

	return false;
}*/