$('document').ready(function(){

	//load nav arrow
	$('.section').before('<img src="images/arrow-down.png" />');
	
	//move arrow and load content correctly on initial page load
	setUrl();

	//re-set arrow and content on click
	$('.nav a').live('click',function(){
		url = $(this).attr('href');
		setUrl(url);
		return false;
	});
	function getContent(siteAddress,content){
		if(typeof content=="undefined")content=".section";
		$.get(
				siteAddress,
				".section",
				function(data) {
					var newContent = $(data).next(content);
					$(content).hide().html(newContent).fadeIn(); },
				"html"
			);
	};
	function navigateHash(url){
		getContent(url);
		var leftValue;
		if(url=="index.html")leftValue = '25.4%';
		if(url=="resume.html")leftValue = '45%';
		if(url=="connect.html"){
			leftValue = '65.6%';
			getTweets();
		}
		$('img').animate({marginLeft:leftValue},'slow');
	};
	function setUrl(url){
		if(typeof url == "undefined")url = window.location.href;
		var index = url.lastIndexOf("/");
		var filename = url.substr(index+1);
		if(filename == "")filename = "index.html";
		navigateHash(filename);
	};	
	function getTweets (){
		
		// Declare variables to hold twitter API url and user name
		var twitter_api_url = 'http://search.twitter.com/search.json';
		var twitter_user    = 'jmob86';
		$.ajax({
			'cache':true
		});
		// Send JSON request
		$.getJSON(
			twitter_api_url + '?callback=?&rpp=5&q=from:' + twitter_user,
			function(data) {
				$('#tweet_container').append('<ul>');
				var n = 1.1;
				$.each(data.results, function(i, tweet) {
					//Debugging line
					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 date_tweet = new Date(tweet.created_at);
						var date_now   = new Date();
						var date_diff  = date_now - date_tweet;
						if( date_diff > "50000000" ){
							var hours = Math.round(date_diff/(1000*60*60*24))+" days ago";
						}else{
							var hours      = Math.round(date_diff/(1000*60*60))+" hours ago";
						}
				
						// Build the html string for the current tweet
						var tweet_html = '<li>';
						tweet_html    += '<span class="quote">&ldquo;</span>';
						tweet_html    += '<span class="tweet">';
						tweet_html    += tweet.text + '<\/span>';
						tweet_html    += '<span class="time">' + hours;
						tweet_html    += '<\/span><\/li>';
						
						
						function removeN(){
							n = n-.1;
							return n;
						};
						// Append html string to tweet_container div	
						$('#tweet_container ul').append(tweet_html);
						
						$('#tweet_container ul li:last').fadeIn().delay(1000).css('color','rgba(0,0,0,'+removeN()+')').delay(1000);
					}
				});
			}
		);
	};
		
});
