(function(){

	var items = {
		about: { link: "/about" },
		bio: { link: "/about/bio", parent: "about" },
		music: { link: "/about/music", parent: "about" },
		"résumé": { link: "/about/resume", parent: "about" },
		vg: { link: "/about/video-games", parent: "about" },
		contact: { link: "/contact", parent: "about" },
		tech: { link: "/about/tech", parent: "about" },

		blargh: { link: "/blargh" },

		stuff: { link: "/stuff" },
		php64: { link: "/stuff/php64", parent: "stuff" },
		code_swarm: { link: "/stuff/code-swarm", parent: "stuff" },
		sites: { link: "/stuff/sites", parent: "stuff" },
		experiments: { link: "/stuff/experiments", parent: "stuff" },
		"laserguns!": { link: "/stuff/laserguns!", parent: "stuff" },
		tcf: { link: "/stuff/ternary-continued-fractions", parent: "stuff" },

		projects: { link: "/projects" },
		argopt: { link: "/projects/argopt", parent: "projects" },
		linkurious: { link: "/projects/linkurious", parent: "projects" },
		acronym: { link: "/projects/acronymulator", parent: "projects" },
		butterfly: { link: "/projects/butterfly", parent: "projects" },
		sunlight: { link: "/projects/sunlight", parent: "projects" },
		jarvis: { link: "/projects/jarvis", parent: "projects" },
		facilius: { link: "/projects/facilius", parent: "projects" },
		"blue shift": { link: "/projects/blue-shift", parent: "projects" },
		vgquotes: { link: "/projects/vgquotes.com", parent: "projects" },
		gridiron: { link: "/projects/gridiron", parent: "projects" },
		ntestify: { link: "/projects/ntestify", parent: "projects" },
		midi: { link: "/projects/midi-parser", parent: "projects" }
	};

	//filter the links based on the URL
	var path = window.location.pathname.substring(1).replace(/\/+$/, "");
	if (path) {
		var newItems = {};
		for (var itemName in items) {
			if (items[itemName].parent || itemName === path) {
				newItems[itemName] = items[itemName];
			}
		}

		items = newItems;
	}

	var parents = [];
	var children = [];
	for (var name in items) {
		if (!items[name].parent) {
			(function(name, item){
				parents.push({ name: name, data: item, toString: function() { return name; } });
			}(name, items[name]));
		} else {
			(function(name, item){
				children.push({ name: name, data: item, toString: function() { return name; } });
			}(name, items[name]));
		}
	}

	//shuffle em up
	children = (function(arr) {
		var tmp, current, top = arr.length;

		while(--top) {
			current = Math.floor(Math.random() * (top + 1));
			tmp = arr[current];
			arr[current] = arr[top];
			arr[top] = tmp;
		}

		return arr;
	})(children);


	var width = 650,
		height = 440;

	var parentY = d3.scale.ordinal().domain(parents).rangePoints([0, height - 100], .5);

	var canvas = d3.select("#menu")
		.style("width", width + "px")
		.style("height", height + "px");

	var parentLinks = canvas.selectAll(".parent")
		.data(parents)
		.enter()
		.append("div")
		.attr("class", "parent")
		.style("top", function(item) { return parentY(item) + "px"; })
		.append("a")
		.on("click", selectParent)
		.attr("href", function(item) { return item.data.link; })
		.text(function(item) { return item.name; });

	var perRow = 6;
	var perCol = 6;
	for (var i = 0, rows = [], cols = []; i < children.length; i++) {
		var rowIndex = i % perRow;
		var colIndex = Math.floor(i / perCol);
		if (!rows[rowIndex]) {
			rows[rowIndex] = [];
		}
		if (!cols[colIndex]) {
			cols[colIndex] = [];
		}

		rows[rowIndex].push(children[i]);
		cols[colIndex].push(children[i]);
	}

	function childX(d, i) {
		return d3.scale
			.ordinal()
			.domain(cols[Math.floor(i / perCol)])
			.rangePoints([width - 80 * perRow, width], .5)(d, i);
	}

	function childY(d, i) {
		return d3.scale
			.ordinal()
			.domain(rows[i % perRow])
			.rangePoints([0, height - 100], .5)(d, i);
	}

	function selectParent(d) {
		//find child nodes, if any
		var names = children.filter(function(item) { return item.data.parent === d.name; }).map(function(obj) { return obj.name; });
		if (names.length) {
			if (d3.event) {
				d3.event.stopPropagation();
				d3.event.preventDefault();
			}

			parentLinks.attr("class", "parent");
			parentLinks.filter(function(item) { return item === d; }).attr("class", "parent active");

			var circles = canvas.selectAll(".child");

			circles
				.attr("class", "child")
				.select("a")
				.transition()
				.duration(1000)
				.style("background-color", "#FFFFFF")
				.style("color", "#FFFFFF")
				.style("box-shadow", "0px 0px 0px #FFFFFF")
				.remove()
				.each("end", function() {
					circles.filter(function(item) { return names.indexOf(item.name) < 0; }).text("?");
				});

			circles
				.filter(function(item) { return names.indexOf(item.name) >= 0; })
				.attr("class", "child active")
				.text("")
				.append("a")
				.attr("href", function(item) { return item.data.link; })
				.transition()
				.duration(1000)
				.style("background-color", "#9EB2DD")
				.style("box-shadow", "0 0 10px #9EB2DD")
				.style("color", "#000000")
				.text(function(d) { return d.name; });
		}
	}


	canvas.selectAll(".child")
		.data(children)
		.enter()
		.append("div")
		.attr("class", "child")
		.style("top", function(item, i) { return childY(item, i) + "px"; })
		.style("left", function(item, i) { return childX(item, i) + "px"; })
		.text("?");

	if (path) {
		//select the current parent
		selectParent(parents.filter(function(e) { return e.name === path; })[0]);
	}

}());
