$(document).ready(function() {
	var twitterUser = 'tweeclare';
	var maxTweets = 7;
	var maxTweetsJSON = maxTweets + 5; 	//must be a few more than maxTweets to account for Twitter ReTweet count bug

	var twitterJsonUrl = 'http://twitter.com/status/user_timeline/' + twitterUser + '.json?callback=?&count=' + maxTweetsJSON;
	var myTweets = new Array(maxTweets);
	var twitterDisplayIndex = 0;

    // Enable caching
    $.ajaxSetup({ cache: true });
	
	//Get JSON of tweets
    $.getJSON(
        twitterJsonUrl, function(data) {
		//Loop through results and display each tweet
           $.each(data, function(i, tweet) {
		  
               // Check tweet contains data
                if((tweet.text !== undefined) && (i<maxTweets) ) {
                    // Calculate how many hours ago was the tweet posted

					var baddate = tweet.created_at.split(' ');
					var date_tweet = baddate[0]+" "+baddate[1]+" "+baddate[2]+" "+baddate[5]+" "+baddate[3]+" "+baddate[4];

					var date_tweet_dt   = new Date(date_tweet);
                    var date_now   = new Date();
                    var date_diff  = date_now - date_tweet_dt;
                    var hours      = Math.floor(date_diff/(1000*60*60));
					var days	   = Math.floor(hours/24);

                    // Build the html string for the current tweet
                    var tweet_html = '<div class="tweet_text">';
					tweet_html    += '<a target="_blank" class="username" href="http://twitter.com/' + twitterUser + '">' + twitterUser + '<\/a>';
                    tweet_html    += '<a target="_blank" class="text" href="http://twitter.com/';
                    tweet_html    += twitterUser + '/status/' + tweet.id_str + '">';
                    tweet_html    += tweet.text + '<\/a><\/div>';
					
						if (hours > 0) {
							if (days < 1) {
								tweet_html    += '<div class="tweet_hours">about ' + hours;
								if (hours == 1) {
									tweet_html    += ' hour ago<\/div>';
								}
								else {
									tweet_html    += ' hours ago<\/div>';
								}
							}
							else {
								tweet_html    += '<div class="tweet_hours">about ' + days;
								if (days == 1) {
									tweet_html    += ' day ago<\/div>';
								}
								else {
									tweet_html    += ' days ago<\/div>';
								}
							}
						}
						else {
							tweet_html    += '<div class="tweet_hours">less than an hour ago<\/div>';
						}

                    // Write it!
					myTweets[i] = tweet_html;
                }
			}
			);
			WriteAllTwitter();
	});
	
	//Separate tweets with fade
	function WriteTwitter(){
		if (twitterDisplayIndex > maxTweets-1) {
			twitterDisplayIndex = 0;
		}
		
		$('#twitter_container').fadeOut('fast', function(){
			$('#twitter_container').html(myTweets[twitterDisplayIndex]).fadeIn('slow');
			twitterDisplayIndex++;

			//call recursively to change ticker
			setTimeout(WriteTwitter, 5000);
		});
	}
	
	//All tweets
	function WriteAllTwitter(){
		if (myTweets.length > 0)
		{
			var i=0;
			for (i=0;i<maxTweets;i++)
			{
				$('#twitter_container').html($('#twitter_container').html() + myTweets[i]);
			}
		}
	}
	

});


