$(document).ready(function(){
	
	hideSections();
	
	$(".heading").each(function(){
		$(this).css({"opacity":0.5});
	});
	
	$(".heading").live('hover', function(e) {
	            if (e.type == 'mouseover') {
	                $(this).stop().animate({'opacity':1, 'padding-left':'0.6em', 'letter-spacing':'2px'}, 400);
	            }

	            if (e.type == 'mouseout') {
	                $(this).stop().animate({'opacity':'0.5', 'padding-left':'0.05em', 'letter-spacing':'0'}, 400);
	            }
				})
				.live('click', function(){
					var me = $(this);
					hideSections();
					$('.active').removeClass('active').addClass('heading').animate({'opacity':'0.5', 'padding-left':'0.05em', 'letter-spacing':'0'}, 400);
					me.removeClass('heading').addClass('active').parent().next().show('normal');
					return(false);
				});
				
	var hoverColor = "#fa826e";
	$(".tweet").live('hover', function(e){
		if(e.type == 'mouseover'){
			$(this).stop().animate({'opacity':1}, 400);
			$(this).find(".tweet-link").animate({'color': hoverColor}, 500);
		}
		if (e.type == 'mouseout'){
			$(this).stop().animate({'opacity':'0.5'}, 400);
			$(this).find(".tweet-link").stop().animate({'color':'#585457'}, 500);
		}
	});
	
	$.ajax({
			url : "http://twitter.com/statuses/user_timeline/colmbritton.json?count=6&callback=?",
			dataType : "json",
			timeout:15000,
			
			success : function(data){
					var date_parts;
					for(var i=0; i<data.length; i++){;
						
						date_parts = data[i].created_at.split('+');
						$('<li>', {"class": "tweet"}).appendTo("#twitter-feed")
							.append($('<p>', {"class":"twitter-text", "text":data[i].text}))
							.append($('<p>', {"class":"twitter-time", "text":date_parts[0]}))
							.css({"opacity":0.5});
						
					}
					tweetsToLink();
				},
			
			error : function() {
					alert("Failure: Couldn't get data from twitter");
				},
		});
	
});

function hideSections() {
	$(".section").each(function(){
		$(this).hide('normal');
	});
}

function linkify(text) {
	var regex = /(https?\:\/\/[\w\.\/]+)/g;
	var twitterReg = /\@([\w]+)/g;
	text = text.replace(regex, '<a class="tweet-link" href="$1" target="_blank">$1</a>');
	text = text.replace(twitterReg, '<a class="tweet-link" href="http://twitter.com/$1" target="_blank">@$1</a>');
	return text;
}

function tweetsToLink(){
	$(".twitter-text").each(function(){
	    var texts = $(this).text();
	    texts = linkify(texts);
	    $(this).html(texts);
	});
}
