$(function(){

  $(window).hashchange( function(){
    
    hash = (window.location.hash).replace("#","");
	if(hash && hash != "") {
		$("#loader").animate({opacity:1,left:"0px"},400);
		$("#main").animate({opacity:0,left:"-980px"},400,function() {
			
			$("#main").load("./content.php?page=" + hash,function() {
				$("#main").animate({left:0,opacity:1},300);
				$("#loader").animate({opacity:0,left:"980px"},300);
				
				resetContentHeight();
			});
			
		});
	} else {
		$("#loader").animate({opacity:1,left:"0px"},400);
		$("#main").animate({opacity:0,left:"-980px"},400,function() {
			
			$("#main").load("./content.php?page=portfolio",function() {
				$("#main").animate({left:0,opacity:1},300);
				$("#loader").animate({opacity:0,left:"980px"},300);
				
				resetContentHeight();
			});
			
		});
	}
    
  })
  
  $(window).hashchange();
  
});


jQuery(window).load(function() {
	$.getJSON('http://api.twitter.com/1/statuses/user_timeline.json?screen_name=aeliox&count=1&exclude_replies=1&callback=?', function(data) {
	  $('#twitterTime').html("tweeted " + relativeTime(data[0].created_at) + " by <a href='http://twitter.com/aeliox' target='_blank'>@aeliox</a>");
	  $('#twitterText').html(parseTwitter(data[0].text));
	});
});

function fetchDribbbleFollowersCount() {
	$.jribbble.getPlayerById('aeliox', function (player) {
	    var html = [];
	
	    $('#dribbbleCount').html(" &middot; " + player.followers_count + " followers &middot; " + player.shots_count + " shots");
	});
}


function changeMain(item) {
	link = $(item).attr("href");
	

	window.location.hash = link;
	
	$("#loader").animate({opacity:1,left:"0px"},400);
	$("#main").animate({opacity:0,left:"-980px"},400,function() {
	
		$.ajax({
			type: "POST",  
			url: "./content.php?page="+link,
			success: function(msg) {
				$("#main").html(msg);
				$("#main").animate({left:0,opacity:1},300);
				$("#loader").animate({opacity:0,left:"980px"},300);
			
				resetContentHeight();
			}
		});
		
	});
}

function resetContentHeight() {
	height = $('#main').height();
	$("#mainHolder").animate({height:height+'px'}, 500);
}

function devanceImages() {
	item = $("#imagesControls a.on");
	length = $("#imagesControls a").size();
	index = $(item).parent().children().index($(item));
	
	if(index == 0) {
		newIndex = (length - 1);
	} else {
		newIndex = index - 1;
	}
	
	newItem = $("#imagesControls a").eq(newIndex);
	slideImages(newItem);
}

function advanceImages() {
	item = $("#imagesControls a.on");
	length = $("#imagesControls a").size();
	index = $(item).parent().children().index($(item));
	
	if(index == (length - 1)) {
		newIndex = 0;
	} else {
		newIndex = index + 1;
	}
	
	newItem = $("#imagesControls a").eq(newIndex);
	slideImages(newItem);
}

function slideImages(item) {
	index = $(item).parent().children().index($(item));
	$("#imagesHolder").animate({left:(index * -620)+"px"},300);
	
	$("span#imageControlsCount").html(index+1);
	
	$(item).siblings().each(function() {
		$(this).removeClass("on");
	});
	
	$(item).addClass("on");
}



function relativeTime(pastTime) {
	// Generate a JavaScript relative time for the tweets

	var origStamp = Date.parse(pastTime);
	var curDate = new Date();
	var currentStamp = curDate.getTime();
	var difference = parseInt((currentStamp - origStamp)/1000);
	
	if(difference < 0) return false;
	
	if(difference <= 20)         return "seconds ago";
	if(difference <= 60)         return "a minute ago";
	if(difference < 3600)        return parseInt(difference/60)+" minutes ago";
	if(difference <= 1.5*3600)   return "one hour ago";
	if(difference < 23.5*3600)   return Math.round(difference/3600)+" hours ago";
	if(difference < 1.5*24*3600) return "one day ago";
	
	// If the tweet is older than a day, show an absolute date/time value;
	var dateArr = pastTime.split(' ');
	
	return dateArr[4].replace(/\:\d+$/,'')+' '+dateArr[2]+' '+dateArr[1]+
	(dateArr[3]!=curDate.getFullYear()?' '+dateArr[3]:'');
}

function parseTwitter(text) {
    // Parse URIs
    text = text.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(uri) {
		return uri.link(uri);
	});
 
    // Parse Twitter usernames
    text = text.replace(/[@]+[A-Za-z0-9-_]+/, function(u) {
		var username = u.replace("@","")
		return u.link("http://twitter.com/"+username);
	});
 
	// Parse Twitter hash tags
	text = text.replace(/[#]+[A-Za-z0-9-_]+/, function(t) {
		var tag = t.replace("#","%23")
		return t.link("http://search.twitter.com/search?q="+tag);
	});
    
    return text;
}
