window.onload = initAll;
var xhr = false;

function initAll() {
	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	}
	else {
		if (window.ActiveXObject) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) { }
		}
	}

	if (xhr) {
		pedirDatos();
	}
	else {
		alert("Lo siento, pero no he podido crear un XMLHttpRequest");
	}
}

function getTwits() {
	xhr.open("GET", "flickrfeed.xml", true);
	xhr.onreadystatechange = showPictures;
	xhr.send(null);

	setTimeout("getTwits()",5 * 1000);
}

function pedirDatos(){

	var obj = document.getElementById("contenedor");

	xhr.open("GET", "ajax.php", true);

	xhr.onreadystatechange = function(){

	if (xhr.readyState == 4 && xhr.status == 200) {

		obj.innerHTML = xhr.responseText;

		}
	}

	xhr.send(null);

	setTimeout("pedirDatos()",5 * 1000);
	
}


function showPictures() {
	var tempDiv = document.createElement("div");
	var tempDiv2 = document.createElement("div");
			
	if (xhr.readyState == 4) {
		if (xhr.status == 200) {
			tempDiv.innerHTML = xhr.responseText;
			var allLinks = tempDiv.getElementsByTagName("a");
			
			for (var i=1; i<allLinks.length; i+=2) {
				tempDiv2.appendChild(allLinks[i].cloneNode(true));
			}

			allLinks = tempDiv2.getElementsByTagName("a");
			var randomImg = Math.floor(Math.random() * allLinks.length);
			document.getElementById("pictureBar").innerHTML = allLinks[randomImg].innerHTML;
		}
		else {
			alert("Hay un problema con la solicitud: " + xhr.status);
		}
	}
}

