jQuery(document).ready(function() {
	// Custom Function(s)
	jQuery.fn.fadeToggle = function(s, fn) {
		return (this.is(":visible"))
		? this.fadeOut(s, fn)
		: this.fadeIn(s, fn);
	};
	
	jQuery.fn.log = function(msg) {
		console.log("%s: %o", msg, this);
		return this;
	};
	
	// Enable the LavaLamp navigation
	jQuery("#Nav").lavaLamp({ fx: "jswing", speed: 250 });

	
	// Default speed for animations
	var speed = 'fast';
	var searchText = 'Search this site...';
	
	// Animate the captions on thumbnails
	jQuery('.thumb').hover(function() {
		jQuery('.caption', this).stop().animate({
			height: '40px'		// visible
		}, {queue:false, duration:speed});
	}, function() {
		jQuery('.caption', this).stop().animate({
			height: '0px'		// hidden
		}, {queue:false, duration:speed});
	});
	
	// Twitter
	jQuery.getJSON('http://twitter.com/status/user_timeline/rthaut.json?count=1&callback=?', function(data){
		var regexp1 = /((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g;
		var regexp2 = /\B@([_a-z0-9]+)/ig;
		var regexp3 = /\B#([a-z0-9][\-_a-z0-9]+)/ig;
		jQuery.each(data, function(index, item){
			var status = item.text;
			status = status.replace(regexp1, '<a href="$1">$1</a>');
			status = status.replace(regexp2, '<a href="http://twitter.com/$1">@$1</a>');
			status = status.replace(regexp3, '<a href="http://twitter.com/#search?q=%23$1">#$1</a>');
			jQuery('blockquote', '#Twitter').html(status + '<cite></cite>');
			jQuery('cite', '#Twitter').html('<i>Tweeted ' + relative_time(item.created_at) + '</i> | <a href="http://twitter.com/?status=%40' + item.user.screen_name + '%20&amp;in_reply_to_status_id=' + item.id + '&amp;in_reply_to=' + item.user.screen_name +'">Reply</a> | <a href="http://twitter.com/' + item.user.screen_name + '">Follow Me!</a>');
		});
	});

});

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);

	if (delta < 60) {
		return 'less than a minute ago';
	} else if(delta < 120) {
		return 'about a minute ago';
	} else if(delta < (60*60)) {
		return 'about ' + (parseInt(delta / 60)).toString() + ' minutes ago';
	} else if(delta < (120*60)) {
		return 'about an hour ago';
	} else if(delta < (24*60*60)) {
		return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
	} else if(delta < (48*60*60)) {
		return '1 day ago';
	} else {
		return (parseInt(delta / 86400)).toString() + ' days ago';
	}
}