/**
 * @author Keiran
 */
 

$(document).ready(function(){
	$.getJSON('http://twitter.com/status/user_timeline/aeliox.json?count=1&callback=?', function(data) {
	  $('span#twitterTime').html(relativeTime(data[0].created_at));
	  $('span#twitterText').html(linkify(data[0].text));
	});
	
	if(checkHash()) {
	
	}
});


function checkHash() {
	hash = (window.location.hash).replace("#","");

	if(hash && hash != "") {
		var pieces = hash.split("/");
		
		return false;
	} else {
		return true;
	}
}

function changeItem(item) {
	alt = $(item).attr("alt");
	
	$("#main").animate({opacity:0,left:"200px"},300,function() {
	
		$("#mainHolder").load("./content.php?id=" + alt,function() {
			$("#main").css({"opacity":0,"left":"-200px"});
			$("#main").animate({left:0,opacity:1},300);
		});
		
	});
}

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 * -700)+"px"},300);
	
	$(item).siblings().each(function() {
		$(this).removeClass("on");
	});
	
	$(item).addClass("on");
}


function showBlog() {
	$("#main").animate({opacity:0,left:"200px"},300,function() {
		
		$.getJSON("http://aeliox.tumblr.com/api/read/json?callback=?",function(data) { 
			posts = data.posts;
			$.post("./blog.php",posts,function(data) {
				$("#mainHolder").html(data);
				$("#main").css({"opacity":0,"left":"-200px"});
				$("#main").animate({left:0,opacity:1},300);
			}, "json");
			
		});
		
	});
}


 
//KONAMI
var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65";
$(document).keydown(function(e) {
  kkeys.push( e.keyCode );
  if ( kkeys.toString().indexOf( konami ) >= 0 ){
    //$(document).unbind('keydown',arguments.callee);
    kkeys = [];
    if($('body').css("background-color") == "rgb(255, 255, 255)") {
   	 	//$('body').css("background","black url(/images/bgInvert.png) repeat-x");   
   	} else {
   		//$('body').css("background","white url(/images/bg.png) repeat-x"); 
   	}   
  }
});

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 <= 5)          return "Just now";
	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 linkify(s) {
  var entities = {
    '"' : '&quot;',
    '&' : '&amp;',
    '<' : '&lt;',
    '>' : '&gt'
  };

  return s.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&?\/.=]+/g, function(m) {
    return m.link(m);
  }).replace(/(^|[^w])(@[dw-]+)/g, function(d, m1, m2) {
    return m1 + '@<a href="http://twitter.com/' + m2.substr(1) + '">' + m2.substr(1) + '</a>';
  }).replace(/"&<>/, function (m) {
    return entities[m];
  });
}
