
var hash = "";

var menu;

var section;
var sections;

var last_project;

var positions = new Array();

var vector;

init();
setInterval(loop, 1000/60);

function init()
{
	menu = document.getElementById('menu');
	sections = document.getElementById('sections');
	
	separateSections();
	getPositions();
	
	vector = new Vector2D();
	vector.x = window.innerWidth;
	
	sections.style.marginLeft = ((window.innerWidth / 2) + Number(vector.x)) + "px";
	sections.style.visibility = "visible";
	
	TWEEN.start();
	
	if (getHash() == "")
		window.location.hash = "/projects/" + last_project;
}

function loop()
{
	if (getHash() != hash)
	{
		hash = getHash();
		section = hash.split("/");
		
		menuOptionSelect(section[1]);
		
		if (section[1] == "projects")
		{
			if (section[2] != null)
			{
				section[1] += "/" + section[2];
			}
			else
			{
				window.location.hash = "/" + section[1] + "/" + last_project;
				return;
			}
		}
		
		for (var i = 0; i < positions.length; i++)
		{
			if (positions[i][0] == section[1])
			{
				new TWEEN.Tween(vector)
					.to({x: -(positions[i][1] + (positions[i][2] / 2))}, 1500)
					.easing(TWEEN.Easing.Exponential.EaseOut)
					.start();
			}
		}
	}

	sections.style.marginLeft = ((window.innerWidth / 2) + Number(vector.x)) + "px";
}

//

function Vector2D()
{
	this.x = 0;
	this.y = 0;
}

function menuOptionSelect(option)
{
	var nodes = menu.getElementsByTagName('LI');

	for (var i = 0; i < nodes.length; i++)
	{
		if (nodes[i].getAttribute('name') == option)
		{
			nodes[i].className = "selected";
		}
		else
		{
			nodes[i].className = "";
		}
	}
}

function separateSections()
{
	var nodes = sections.getElementsByTagName('DIV');
	
	for (var i = 0; i < nodes.length; i++)
	{
		if (nodes[i].parentNode == sections)
		{
			nodes[i].style.marginRight = (window.innerWidth / 2) + "px";
		}
	}
}

function getPositions()
{
	var nodes = sections.getElementsByTagName('DIV');

	var j = 0;
	
	for (var i = 0; i < nodes.length; i++)
	{
		if (nodes[i].parentNode == sections)
		{
			positions[j++] = [ nodes[i].getAttribute('name'), nodes[i].offsetLeft, nodes[i].offsetWidth ];
			
			var subnodes = nodes[i].getElementsByTagName('DIV');
			
			for (var k = 0; k < subnodes.length; k++)
			{
				if (subnodes[k].parentNode == nodes[i])
				{
					if (last_project == null)
						last_project = subnodes[k].getAttribute('name');
						
					positions[j++] = [ nodes[i].getAttribute('name') + "/" + subnodes[k].getAttribute('name'), subnodes[k].offsetLeft, subnodes[k].offsetWidth ];
					
					subnodes[k].addEventListener("click", function(evt){ window.location.hash = "/projects/" + this.getAttribute('name') }, false);
				}
			}
		}
	}
}

function getHash()
{
	return window.location.hash;
}

