jQuery(function(){

	jQuery('#link_home').addClass('active');

	//Fetching Tweets from Twitter
	var twitter_api_url = 'http://search.twitter.com/search.json';
	var twitter_user    = 'dannykmusic';
	
	// Enable caching
	jQuery.ajaxSetup({ cache: true });
	
	// Send JSON request, The returned JSON object will have a property called "results" where we find a list of the tweets matching our request query
	jQuery.getJSON(
	    twitter_api_url + '?callback=?&rpp=5&q=from:' + twitter_user,
	    function(data) {
	
		    var tweet_html      = '';
	
	        jQuery.each(data.results, function(i, tweet) {
	            // Uncomment line below to show tweet data in Fire Bug console - Very helpful to find out what is available in the tweet objects
	            //console.log(tweet);
	
	            // Before we continue we check that we got data
	            if(tweet.text !== undefined) {

	                // Calculate how many hours ago was the tweet posted
	                var time_ago   = jQuery.timeago(new Date(tweet.created_at));
	
	                // Build the html string for the current tweet
	                tweet_html    += '<div>';
	                tweet_html    += '<h4>' + time_ago + '<\/h4>';
	                tweet_html 	  += '<p>';
	                tweet_html    += '<a href="http://www.twitter.com/' + twitter_user + '/status/' + tweet.id + '" target="_blank">' + tweet.text + '<\/a>';
	                tweet_html    += '<\/p>';
	                tweet_html    += '<\/div>';
	
	            }
	        });
	
			jQuery("#twitter_feed").empty();
			jQuery('#twitter_feed').append(tweet_html);
			jQuery('#twitter_feed').css('background', 'transparent');

			var tweets = jQuery('#twitter_feed').children(); 
		
			if(tweets.length > 0)
			{
				jQuery('#twitter_feed').cycle({
					fx: 'fade',
					timeout: 12000,		
				});
				
				jQuery('<div id="twitter_link"><a href="http://twitter.com/dannykmusic"><img src="images/twitter.png" \/><\/a><\/div>').insertAfter('#twitter_feed');
			}

    
	    }
	);

});
