// NOTE TO ANYONE THINKING OF STEALING THIS RADIAL MENU AWESOMENESS:
// 1. Don't, it's mine.
// 2. It doesn't work beyond one level yet, due to laziness.

len1 = 216;
len2 = len1;
del1 = 0;
spread_scale = 1;
dur = 400;
dur2 = 125;
color1 = "";
color2 = "";
color2a = "";
color3 = "";
color3a = "";
color4 = "";

root_menu = null;

radials = 
new menu("<div style=\"margin-top: -0.5em;\">adam rezich</div>",
	new menu("social",
		new link("facebook", "http://facebook.com/adamrezich"),
		new link("twitter", "http://twitter.com/adamrezich"),
		//new menu("<div style=\"margin-top: -0.5em;\">whiskey media</div>",
			new link("<div class='two'>giant bomb</div>", "http://www.giantbomb.com/profile/takua108/"),
		//	new link("tested", "http://www.tested.com/profile/takua108/"),
		//	new link("screened", "http://www.screened.com/profile/takua108/"),
		//	new link("comic vine", "http://www.comicvine.com/profile/takua108/"),
		//	new link("anime vice", "http://www.animevice.com/profile/takua108/")
		//),
		new link("steam", "http://steamcommunity.com/id/108"),
		new link("last.fm", "http://last.fm/user/takua108"),
		new link("getglue", "http://getglue.com/adamrezich"),
		new link("wakoopa", "http://wakoopa.com/takua108")
	),
	new menu("websites",
		//new link("interior", "http://rezich.com/interior/"),
		new link("<div class='two'>almost illustrated</div>", "http://almostillustrated.com"),
		new link("<div class='three'>i'm<br />calling<br />it</div>", "http://imcalling.it"),
		new link("gbvd", "http://api.imcalling.it/venn/"),
		new link("<div class='three'>the inevitable seven</div>", "http://inevitableseven.com")
	),
	/*new menu("games",
		new link("interior", "http://rezich.com/interior/"),
		new link("z-day", "http://rezich.com/zday.zip")
		//new link("<div class='two'>dwarf cannon</div>", "http://dwarfcannon.com"),
		//new link("perclona", "http://rezich.com/Perclona.zip")
	),*/
	new menu("etcetera",
		new link("music", "http://thesixtyone.com/wtf"),
		new link("rap", "http://inevitableseven.com"),
		new link("films", "http://rezich.com/flash")
	),
	new menu("contact",
		new link("e-mail", "mailto:adam@rezich.com"),
		new link("twitter", "http://twitter.com/?status=%40adamrezich"),
		//new link("formspring", "http://formspring.me/adamrezich"),
		new link("skype", "skype:takua108?call")
	)
);

function radial(e) {
	if ($(e).data("open")) {
		radial_close($(e));
	}
	else {
		radial_open($(e));
	}
}
function radial_close(e) {
	if (e.parent().get(0).className == "radial") {
		var allclosed = true;
		for (i = 0; i < e.children("li").length; i++) {
			var f = e.children("li")[i];
			var g = $(f).children("ul")[0];
			if ($(g).data("open")) allclosed = false;
		}
		if (allclosed) {
			e.data("open", false);
			e.children("li").each(function(i) {
				rad($(this), del1 * i, 0, i * (Math.PI * 2 / ($(this).parent().children("li").length)));
			});
			e.children("a").each(function() {
				$(this).removeClass();
				$(this).animate({
					backgroundColor: color2
				}, dur);
			});
			return;
		}
	}
	
	if (e.parent().parent().parent().get(0).className != "radial") {
		e.parent().siblings().each(function() {
			$(this).removeClass("disabled");
		});
		$("#radial-back").siblings("div").each(function() {
			$(this).fadeIn(dur);
		});
		$("#radial-back").fadeOut(dur);
	}

	e.children("li").each(function() {
		$(this).children("ul").each(function() {
			if ($(this).data("open")) {
				$(this).data("open", false);
				$(this).parent().siblings("li").each(function() {
					$(this).fadeIn(dur);
				});
				shift(len2, (Math.PI * 2 / $(this).parent().parent().children("li").length * $(this).parent().index()) + Math.PI);
				$(this).children("li").each(function(i) {
					rad($(this), del1 * i, 0, i * (Math.PI * 2 / ($(this).parent().children("li").length)));
				});
				$(this).children("a").each(function() {
					$(this).css("cursor", "pointer");
					$(this).animate({
						backgroundColor: color2
					}, dur, function() {
						$(this).addClass("back");
					});
				});
			}
		});
	});
}
function radial_open(e) {
	e.data("open", true);
	if (e.parent().get(0).className == "radial") {
		e.children("li").each(function(i) {
			rad($(this), del1 * i, len1, (i * (Math.PI * 2 / ($(this).parent().children("li").length))));
		});
		e.children("a").each(function() {
			$(this).animate({
				backgroundColor: color3
			}, dur, function() {
				$(this).addClass("back");
			});
		});
	}
	else {
		e.parent().siblings("li").each(function() {
			$(this).fadeOut("slow");
		});
		shift(len2, Math.PI * 2 / $(e).parent().parent().children("li").length * e.parent().index());
		e.parent().siblings("a").each(function() {
			$(this).append($("#radial-back"));
			$(this).children("div").not("#radial-back").each(function() {
				$(this).fadeOut(dur);
			});
			$("#radial-back").fadeIn(dur);
			/*$(this).animate({
				backgroundColor: color3
			}, dur);*/
		});
		e.children("a").each(function() {
			$(this).css("cursor", "default");
			$(this).addClass("disabled");
			$(this).css("background-color", color2);
			$(this).animate({
				backgroundColor: color4
			}, dur);
		});
		e.children("li").each(function(i) {
			var unit = Math.PI * 2 / $(this).parent().parent().parent().children("li").length;
			var sector = $(this).parent().parent().index();
			var items = $(this).parent().children("li").length;
			var scale = (items + 1) / items;
			var suboffset = $(this).parent().parent().parent().parent().parent().data("offset");
			if (typeof(suboffset) == "undefined") suboffset = 0;
			var offset = (unit * sector) + ((i + 1) * (2 * Math.PI / (items + 1))) + Math.PI;
			offset += suboffset;
			rad($(this), del1 * i, len2, offset);
		});
	}
}
function initialize_radials() {
	// this is a pretty funny hack imho to get the colors
	// from the sass stylesheet into javascript
	$("body").after('<div id="fake-hover"></div>');
	color2 = $(".radial ul a").css("background-color");
	color2a = $("#fake-hover").css("background-color");
	$("#fake-hover").remove();
	
	$("body").after('<div id="fake-back"></div>');
	color3 = $("#fake-back").css("background-color");
	color3a = $("#fake-back").css("color");
	$("#fake-back").remove();
	
	$("body").after('<div id="fake-disabled"></div>');
	color4 = $("#fake-disabled").css("background-color");
	$("#fake-disabled").remove();
}
function rad(e, del, length, ang) {
	$(e).delay(del).animate({
		left:	(Math.sin(ang) * length).toString() + "px",
		top:	(Math.cos(ang) * length).toString() + "px"/*,
		rotate:	radtodeg(ang).toString() + "deg"*/
	}, dur);
}
function radtodeg(rads) {
	rads = (rads + Math.PI) * 180 / Math.PI;
	while (rads > 360) rads -= 360;
	return rads;
}
function shift(length, dir) {
	$("#container").animate({
		marginLeft:	parseInt($("#container").css("margin-left")) - (Math.cos(dir + 3 * Math.PI / 2) * length).toString() + "px",
		marginTop:	parseInt($("#container").css("margin-top")) - (Math.sin(dir + Math.PI / 2) * length).toString() + "px"
	}, dur);
}

function RadialMenu(root, items) {
	this.root = root;
	this.items = items;
	this.build($(root), items);
	$(this.root).append('<div id="radial-back" style="display: none;">back</div>');
}
RadialMenu.prototype.build = function(e, v) {
	if (typeof(v.children) == "undefined") {
		$(e).append("<li><a href=\"" + v.url + "\" target=\"_blank\"><div>" + v.title + "</div></a></li>");
		return;
	}
	if (v.children.length > 0) {
		if ($(e).get(0).tagName.toLowerCase() == "ul") {
			$(e).append("<li><ul></ul></li>");
			e = $($(e).children("li").last()).children("ul").last();
		}
		else {
			$(e).append("<ul></ul>");
			e = $(e).children("ul").last();
		}
		var i;
		for (i= 0; i < v.children.length; i++) {
			this.build(e, v.children[i]);
		}
		$(e).data("offset", Math.PI * 2 / i);
		$(e).data("open", false);
		//$(e).data("offset", 2 * Math.PI / $(this).
		$(e).append("<a href=\"#\"><div>" + v.title + "</div></a>");
		$(e).children("a").each(function() {
			$(this).click(function(event) {
				event.preventDefault();
				radial($(this).parent());
			});
			/*$(this).hover(function() {
				radialhover_over(this);
			}, function() {
				radialhover_out(this);
			});*/
		});
		return;
	}
}
RadialMenu.prototype.set_offsets = function(e, v) {
	if (typeof(v.children) == "undefined") return;
	if (v.children.length > 0) {
		if ($(e).get(0).tagName.toLowerCase() == "ul") {
			e = $($(e).children("li").last()).children("ul").last();
		}
		else {
			e = $(e).children("ul").last();
		}
		var suboffset = $(e).parent().parent().data("offset");
		if (typeof(suboffset) == "undefined") suboffset = 0;
		$(e).data("offset", $(e).data("offset") + suboffset);
		var i;
		for (i= 0; i < v.children.length; i++) {
			this.set_offsets(e, v.children[i]);
		}
	}
}

function radialhover_over(e) {
	if ($(e).hasClass("disabled")) return;
	$(e).animate({
		backgroundColor: ($(e).hasClass("back") ? color3a : color2a)
	}, {
		duration: dur2,
		queue: false
	});
}

function radialhover_out(e) {
	if ($(e).hasClass("disabled")) return;
	$(e).animate({
		backgroundColor: ($(e).hasClass("back") ? color3 : color2)
	}, {
		duration: dur2,
		queue: false
	});
}

function steal_colors() {
	$("body").after('<div id="fake-hover"></div>');
	color2 = $("#fake-hover").css("background-color");
	color2a = $("#fake-hover").css("color");
	$("#fake-hover").remove();
	
	$("body").after('<div id="fake-back"></div>');
	color3 = $("#fake-back").css("background-color");
	color3a = $("#fake-back").css("color");
	$("#fake-back").remove();
	
	$("body").after('<div id="fake-disabled"></div>');
	color4 = $("#fake-disabled").css("background-color");
	$("#fake-disabled").remove();
}

function menu() {
	this.title = arguments[0];
	this.children = new Array();
	for (var i = 0; i < arguments.length - 1; i++) {
		this.children[i] = arguments[i + 1];
	}
}
function link(title, url) {
	this.title = title;
	this.url = url;
}
