//----------------------------------------------------------------------------------------------------------------------
// Archivo: megusta.js
// Versión: 1.0
// Fecha: Marzo 2010
// Autor: elRevoltijo-Network.com
// Descripción: funciones relacionadas con los megustas's
//----------------------------------------------------------------------------------------------------------------------

//----------------------------------------------------------------------------------------------------------------------
// megusta_megusta(). Funcion primaria para anadir un megusta
//----------------------------------------------------------------------------------------------------------------------
function megusta_megusta(id) {
	// Obtencion del XmlHttpObject
	xmlHttpObject = ajax_obtenerXmlHttpObject();
	
	// Comprobacion de campos y procesamiento Ajax
	if (xmlHttpObject == null) {
		alert ("ERROR: Tu navegador no soporta 'HTTP Request'");
		return;
	}

	document.getElementById("span_megusta_enlace_" + id).innerHTML = "<img src='imagenes/ajax-load.gif'>";

	var url = "../modulos/megusta/ajax/megusta_megusta.php";

	xmlHttpObject.onreadystatechange = megusta_megusta_respuesta;
	xmlHttpObject.open("POST", url, true);
	xmlHttpObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttpObject.send("id=" + id);
}

//----------------------------------------------------------------------------------------------------------------------
// megusta_megusta_respuesta(). Funcion secundaria para anadir un megusta
//----------------------------------------------------------------------------------------------------------------------
function megusta_megusta_respuesta() {
	if (xmlHttpObject.readyState == 4 || xmlHttpObject.readyState == "complete") {
		var xmlDoc = xmlHttpObject.responseXML;
		if(xmlDoc.getElementsByTagName("resultado")[0].firstChild != null) { var resultado = xmlDoc.getElementsByTagName("resultado")[0].firstChild.nodeValue; }
		if(xmlDoc.getElementsByTagName("id")[0].firstChild != null) { var id = xmlDoc.getElementsByTagName("id")[0].firstChild.nodeValue; }
		if(xmlDoc.getElementsByTagName("numero")[0].firstChild != null) { var numero = xmlDoc.getElementsByTagName("numero")[0].firstChild.nodeValue; }

		document.getElementById("span_megusta_enlace_" + id).innerHTML = "";

		if (resultado == "1" || resultado == "3") {
			// Actualizar el mensaje
			if(numero == "") { document.getElementById("span_megusta_numero_" + id).innerHTML = "A ninguna persona le gusta todav&iacute;a."; }
			else if(numero == "1") { document.getElementById("span_megusta_numero_" + id).innerHTML = "A 1 persona le gusta."; }
			else { document.getElementById("span_megusta_numero_" + id).innerHTML = "A " + numero + " personas les gusta."; }
		}
	}
}
