/* 
 * Display latest twitter in block
 */
 var intervalID=0;
 var x=0;
 var retry_max=10;
 var delay=3000;

 $(document).ready(function(){
	// create placeholder for full LI and ensure hidden
	$('#page').append('<div id="tw_text"></div>');
 	$('#tw_text').css('display','none');
	
    // call twitter function
 	$('#tw_text').tweet({
 	  	username: 'archiecoates',
        //join_text: "auto",
        count: 1,
        loading_text: "loading tweets..."
      });

	intervalID = setInterval("check_load()",delay);

	// open twitter site in new window if clicked 
	$("#tweet-area #follow").click(function(){
	    window.open('http://twitter.com/archiecoates');
    	return false;
    });


 });		

function check_load(){
	if ($('#tw_text .loading').html() == 'loading tweets...'){
   		x++;  	   		
		//console.log('.loading='+$('#tw_text .loading').html()+' x='+x+' date='+ new Date());  
	} else {
   		show_tweet();
		//console.log('.tweet_text='+$('#tw_text .tweet_text').html());
		clearInterval(intervalID);
	}
	
	if (x==retry_max){
		console.log('tw load retries exceeded ['+x+']');
		clearInterval(intervalID);
	}
}

function show_tweet() {
	//	content: the text to display - lose extra markup
	content=$('#tw_text .tweet_text').html();
	// tweet time added at end in own span (without any markup) 
	time='&nbsp;<span id="time">'+$('#tw_text .tweet_time').text()+'</span>';
	//console.log('tw text='+content+time);
	$('#tweet-area #tweet').html(content+time);
	
}


