var request;

function inittweet() {
	// temporary function, until the real one can be created
	texttooutput = "<h1>Vincent Lords Tweets</h1>\n";
	texttooutput = texttooutput + "<p>Getting Tweets...</p>";
	document.getElementById( 'twitter' ).innerHTML = texttooutput;
	gettweet();
	interval = setInterval( 'gettweet()', '60000' );
}

function gettweet() {
	var url = "/gettweets.php";
	request = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject( "Microsoft.XMLHTTP" );
	request.open( "Get", url, true );
	request.onreadystatechange = updatetweet;
	request.send(null);
}

function updatetweet() {
	if( request.readyState == 4 ) {
		if( request.status == 200 ) {
			texttooutput = "<h1>Vincent Lords Tweets</h1>\n";
			myJSONdoc = request.responseText;
			myObject = eval('(' + myJSONdoc + ')');
			for ( var i in myObject ) {
				if( i == 0 ) {
					texttooutput = texttooutput + "<p class=\"tweet_ico\"><img src=\"" + myObject[0].user.profile_image_url + "\" alt=\"twitter image\" /></p>\n";
				}
				texttooutput = texttooutput + "<p class=\"tweet_tweets\">" + myObject[i].text + "<br />\n<span class=\"tweet_date\">";
				var textdate = new Date( myObject[i].created_at );
				var tdhours = textdate.getHours();
				var tdap = ( tdhours < 12 ) ? "AM" : "PM";
				var outhours = ( tdhours < 1 ) ? 12 : ( ( tdhours > 12 ) ? tdhours - 12 : tdhours );
				var tdmins = textdate.getMinutes();
				var outmins = ( ( tdmins < 10 ) ? "0" : "" ) + tdmins;
				texttooutput = texttooutput + outhours + ':' + outmins + ' ' + tdap + ' on ';
				var montharray = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
				texttooutput = texttooutput + montharray[ textdate.getMonth() ] + ' ' + textdate.getDate() + ', ' + textdate.getFullYear() + "</span></p>\n";
			}
			document.getElementById( 'twitter' ).innerHTML = texttooutput;
		}
	}
}