
if (history.pushState) {
	window.onpopstate = function(e){ stateManager.update(); };
} else {
	var urlWithoutHash = window.location.href.replace(window.location.hash,"");
	if (urlWithoutHash !== $("base").attr("href")) {
		window.location.href = $("base").attr("href") + "#!/" + urlWithoutHash.replace($("base").attr("href"), "");
	}
	window.onhashchange = function(e){ stateManager.update(); };
	$(function(){ stateManager.update(true); });
}


$(".internal").live("click", function(e) {
	e.preventDefault();
	stateManager.push($(this).attr("href"));
});

$("nav a").live("click", function() {
	$("nav a.active").removeClass("active");
	$(this).addClass("active");	
});

infiniseLightbox.init(".lightbox a");
	
	
var stateManager = {
	
	push: function(href)
	{
		if (history.pushState) {
			history.pushState(null, null, href);
			this.update();
		} else {
			window.location.hash = "!/"+href;
		}
	},
	
	update: function(silent)
	{
		var animDuration = silent ? 0 : 500;
		
		if (history.pushState) {
			var url = window.location.href;
		} else {
			var url = window.location.href.replace("#!/","");
		}
		
		$("#content").animate({
			"height": 0,
			"opacity": 0,
		}, animDuration, function()
		{
			$.get(url+"?xhr", function(data)
			{
				$("#content").remove();
				$("header").after(data);
				
				var newHeight = $("#content").height();
				
				$("#content").css({
					"opacity": 0,
					"height": 0
				}).animate({
					"opacity": 1,
					"height": newHeight
				}, animDuration, function()
				{
					$("#content").css("height", "auto");
				});
			})	
		});
	}
}



var colorbar = {

	maxHeight: 5000,
	speed: 200,
	increment: 10,
	gradientSrc: "assets/gradient.jpg",
	
	initialized: false,
	current: 0,
	interval: null,
	canvas: null,
	canvasData: null,
	
	start: function()
	{
		// Exit if already running
		if (this.interval !== null) return;
		
		// Setting up all bindings and getting the gradient color data
		if (!this.initialized)
		{
			$(window).bind("blur", function(){ colorbar.stop() });
			$(window).bind("focus", function(){ colorbar.start() });
			
			this.canvas = document.createElement('canvas');
			this.canvas.height = this.maxHeight;
			this.canvas.width = 1;
			
			var colorbarImage = new Image();
			colorbarImage.src = this.gradientSrc;
			colorbarImage.onload = function()
			{ 
				colorbar.canvas.getContext("2d").drawImage(colorbarImage,0,0);
				colorbar.canvasData = colorbar.canvas.getContext("2d").getImageData(0,0,1,colorbar.maxHeight);
			}
			
			this.current = Math.round(Math.random()*this.maxHeight);
			this.initialized = true;
		}
		
		// Starting or resuming the interval
		this.interval = setInterval("colorbar.step()", colorbar.speed);
		
	},
	
	step: function()
	{
		this.current = (this.current + this.increment) % this.maxHeight;
		$("#colorbar").css("background-position", "0 -"+this.current+"px");
		
		if (!this.canvasData) return;
		
		var newColor = {
			r: this.canvasData.data[this.current*4 -4],
			g: this.canvasData.data[this.current*4 -3],
			b: this.canvasData.data[this.current*4 -2]
		};
		var newRGB = "rgb("+newColor.r+","+newColor.g+","+newColor.b+")";
		document.styleSheets[1].cssRules[0].style.color = newRGB;
		document.styleSheets[1].cssRules[1].style.backgroundColor = newRGB;
		$("#logo").css("background", newRGB);
	},
	
	stop: function()
	{
		clearInterval(this.interval);
		this.interval = null;
	}
};
