$(document).ready(function() {
   // put all your jQuery goodness in here.
   //alert('jquery');

	
	$('#twitter a:first').mouseover(function(){
		$('.overlay').fadeIn();
				
		$('.tweet').fadeIn(function(){
			$('.tweet').animate({top: '+=10'});
		});
		
		
	});
	
	$('.overlay, .close').click(function(){
		$('.overlay').fadeOut();
		$('.tweet').animate({top: '-=10'});
		$('.tweet').fadeOut();
	});
	
	
	get_tweets('vaultstudios', 1);
	
	//Get latest tweets and show them with correct time stamp
	function get_tweets(twittername, numTweets){
	   var tweetURL = 'http://api.twitter.com/1/statuses/user_timeline/' + twittername +'.json?callback=?';
	   
	   var numTweets = numTweets;
	   
	   jQuery.getJSON(tweetURL, function(twitter){
	   		//twitter is the callback function to return the tweets
	   		
	   		//console.log(twitter);
	   		
	   		for (var i = 0; i < numTweets ; i++){
	   			//grab and show tweets
	   			var tweet = linkify(twitter[i].text);
	   			var tweetID = twitter[i].id_str;
	   			var timeago = relative_time(twitter[i].created_at);
	   			var username = twitter[i].user.screen_name;
	   			var author = twitter[i].user.name;
	   			var userlink = twitter[i].user.url;
	   			var profileimg = twitter[i].user.profile_image_url;
	   
	   			$('.tweet').append('<div class="profilepic"><a href="' + userlink + '"><img src="'+ profileimg + '" width="48" height="48" alt="' + userlink + '"></a></div><div class="tweetcontent"><p class="author"><a href="http://twitter.com/'+ username +'" class="user">' + username + '</a> ' + author + '</p><p>' + tweet + '</p><ul class="webintents"><li><img src="img/bird.png" width="16" height="16"><a href="http://twitter.com/'+ username +'/status/'+ tweetID +'">' + timeago + '</a></li><li><a href="https://twitter.com/intent/tweet?in_reply_to='+ tweetID +'"><img src="img/reply.png" width="16" height="16">Reply</a></li><li><a href="https://twitter.com/intent/retweet?tweet_id='+ tweetID +'"><img src="img/retweet.png" width="16" height="16">Retweet</a></li><li><a href="https://twitter.com/intent/favorite?tweet_id='+ tweetID +'"><img src="img/favorite.png" width="16" height="16">Favourite</a></li></ul><img src="img/arrow.png" class="arrow"></div>');
	   		};
	   		
	   });
	   
	}/* end of get_tweets */
	
	function linkify(tweet) {
			    tweet = tweet.replace(/(https?:\/\/\S+)/gi, function (s) {
			        return '<a href="' + s + '">' + s + '</a>';
			    });
			
			    tweet = tweet.replace(/(^|)@(\w+)/gi, function (s) {
			        return '<a href="http://twitter.com/' + s + '">' + s + '</a>';
			    });
			
			    tweet = tweet.replace(/(^|)#(\w+)/gi, function (s) {
			        return '<a href="http://search.twitter.com/search?q=' + s.replace(/#/,'%23') + '">' + s + '</a>';
			     });
			    return tweet;
			}
			
			// Convert Twitter API Timestamp to "Time Ago"
			
			function relative_time(time_value) {
			  var values = time_value.split(" ");
			  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
			  
			  var parsed_date = Date.parse(time_value);
			  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
			  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
			  
			  delta = delta + (relative_to.getTimezoneOffset() * 60);
			
			  var r = '';
			  if (delta < 60) {
			        r = 'a minute ago';
			  } else if(delta < 120) {
			        r = 'couple of minutes ago';
			  } else if(delta < (45*60)) {
			        r = (parseInt(delta / 60)).toString() + ' minutes ago';
			  } else if(delta < (90*60)) {
			        r = 'an hour ago';
			  } else if(delta < (24*60*60)) {
			        r = '' + (parseInt(delta / 3600)).toString() + ' hours ago';
			  } else if(delta < (48*60*60)) {
			        r = '1 day ago';
			  } else {
			        r = (parseInt(delta / 86400)).toString() + ' days ago';
			  }
			
			      return r;
			}
});








