function createRequest() {
  var request = null;
  try {
    request = new XMLHttpRequest();
  } catch (trymicrosoft) {
    try {
      request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (othermicrosoft) {
      try {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (failed) {
        request = null;
      }
    }
  }

  if (request == null) {
    mudarTweet("Erro na solicitaÃ§Ã£o Ajax.");
  } else {
    return request;
  }
}

function mudarTweet(txt) {
	var divtweet = document.getElementById("tweets");
	if(txt != null) {
		if(divtweet.childNodes) {
        	for (i = 0; i < divtweet.childNodes.length; i++) {
				var tweetvelho = divtweet.childNodes[i];
				divtweet.removeChild(tweetvelho);
			}
		}
		var novotweet = document.createTextNode(txt);
		divtweet.appendChild(novotweet);
	}
}


	var xmlhttp = createRequest();
	var url = "tweets-xml.php";
	url = url + '?random=' + new Date().getTime();
	xmlhttp.open("GET", url, true);
	xmlhttp.onreadystatechange = function(){
		if(xmlhttp.readyState == 4) {
		if(xmlhttp.status == 200) {
         	var xmlTweets = xmlhttp.responseXML;
			var tweets = xmlTweets.getElementsByTagName("texto");
			var datas = xmlTweets.getElementsByTagName("data");
			var status = xmlTweets.getElementsByTagName("status")[0].firstChild.nodeValue;
			
			if(status == 'OK') {
            tweet1 = tweets[0].firstChild.nodeValue+' - '+datas[0].firstChild.nodeValue;
			tweet2 = tweets[1].firstChild.nodeValue+' - '+datas[1].firstChild.nodeValue;
			tweet3 = tweets[2].firstChild.nodeValue+' - '+datas[2].firstChild.nodeValue;
			tweet4 = tweets[3].firstChild.nodeValue+' - '+datas[3].firstChild.nodeValue;
			tweet5 = tweets[4].firstChild.nodeValue+' - '+datas[4].firstChild.nodeValue;
			
			
			function twitterSequencia() {
			window.setTimeout(function(){mudarTweet(tweet1);}, 0);
			window.setTimeout(function(){mudarTweet(tweet2);}, 5000);
			window.setTimeout(function(){mudarTweet(tweet3);}, 10000);
			window.setTimeout(function(){mudarTweet(tweet4);}, 15000);
			window.setTimeout(function(){mudarTweet(tweet5);}, 20000);
			}
			
			window.setTimeout(twitterSequencia, 0);
			window.setInterval(twitterSequencia, 25000);
			
			}
			else { mudarTweet('Erro: '+status); }
		}
	}	
	}
	xmlhttp.send(null);
