$(function(){
	var movePos = [100, 174, 160, 84, 61];

	$("img.sub").hide();
	$("ul li").hover(function() {
		$("img.sub:not(:animated)", this).slideDown("fast");
	}, function() {
		$("img.sub", this).slideUp("fast");
	});

	$(".return_top a").click(function() {
		$('html,body').animate({ scrollTop : 0 }, "fast", "swing");
		return false;
	});

	$("#gnav li").hover(function() {
		$(this).animate({ backgroundPositionX : "0" }, "fast", "swing");
	}, function(){
		var index = $("#gnav li").index(this);
		var pos = "" + movePos[index] + "px";
		$(this).animate({ backgroundPositionX : pos }, "fast", "swing");
	});

	(function configureCharacterMap() {
		var charTop = $("#chara_top"), lastGroup, timers = [], hitRects = [
			{group: "hero", x: [0, 383, 468, 0], y: [69, 69, 320, 356]},
			{group: "hero", x: [-43, 211, 211, -43], y: [276, 276, 308, 308]},
			{group: "council", x: [399, 899, 899, 481], y: [60, 60, 278, 312]},
			{group: "council", x: [773, 994, 994, 773], y: [198, 198, 229, 229]},
			{group: "etc", x: [0, 287, 247, 0], y: [379, 357, 512, 512]},
			{group: "media", x: [300, 899, 899, 260], y: [369, 324, 524, 524]}
		];

		function hitTest(x, y, rx, ry) {
			var i, j, c = 0;
			for (i = 0; i < 4; ++i) {
				j = (i + 1) % 4;
				c += (rx[j] - rx[i]) * (y - ry[i]) - (x - rx[i]) * (ry[j] - ry[i]) < 0 ?
				     1 : -1;
			}
			return Math.abs(c) === 4;
		}
		function hit(event) {
			var x = (event.offsetX || event.layerX || event.clientX) - $("#main").offset().left,
			    y = (event.offsetY || event.layerY || event.clientY), i;

			for (i = 0; i < hitRects.length; ++i) {
				if (hitTest(x, y, hitRects[i].x, hitRects[i].y)) {
					return hitRects[i].group;
				}
			}
			return;
		}
		function hover(group) {
			var file = "./img/character/top_on_" + group + ".png",
			    charTopHover = $("#chara_top_hover");
			if (!charTopHover.length) {
				$("<div></div>").appendTo(charTop)
				        .attr("id", "chara_top_hover")
				        .hide();
			}
			if (group !== lastGroup) {
				if (group) {
					charTopHover.show().css("background-image", "url('" + file + "')");
				} else {
					charTopHover.hide();
				}
				lastGroup = group;
			}
		}
		function charSlideIn(group) {
			if (!group) return;
			$(".chara_list:visible").stop().hide();

			for (var i = 0; i < timers.length; ++i) {
				clearTimeout(timers[i]);
			}
			timers = [];

			$("." + group).each(function(i, e) {
				timers.push(setTimeout(function() {
					var x = $(e).height() + parseInt($(e).css("margin-bottom"));
					$(e).show().css({
					        opacity: 0,
					        top: "-" + x + "px",
					        zIndex: -i
					    }).animate({
					        opacity: 1,
					        top: "0px",
					        opacity: 1
					    }, 150, "linear", function() {
					    	$(e).css("z-index", 0);
					    });
				}, i * 150));
			});
		}

		charSlideIn("media");

		charTop.click(function(e) {
			charSlideIn(hit(e));
		}).mousemove(function(e) {
			hover(hit(e));
		}).mousedown(function(e) {
			$("#chara_top_hover").animate({marginLeft: "4px", paddingTop: "4px"}, 60, "swing");
		}).mouseup(function(e) {
			$("#chara_top_hover").animate({marginLeft: "0px", paddingTop: "0px"}, 60, "swing")
		});
	})();
});
