/*jshint bitwise: true, curly: true, eqeqeq: true, forin: true, immed: true,
newcap: true, noarg: true, noempty: true, nonew: true, nomen: true,
onevar: true, plusplus: true, regexp: true, undef: true, strict: true,
white: true, maxerr: 200*/

/*global DD_belatedPNG*/

(function () {
	"use strict";

	var TIMEOUT = 250,
		bind_link_top,
		bind_top_nav,
		bind_aside_nav,
		bind_ajax_loader,
		bind_content_links,
		bind_h1_link,
		refresh;

	bind_link_top = function () {
		var $top = jQuery("<a href=\"#top\" class=\"top\">menu</a>"),
			$aside = jQuery("aside:first, #top");

		$top.click(function (e) {
			$aside.animate({
					top: jQuery(this).after().position().top - 43 + "px"
				},
				TIMEOUT * 2, "easeInCubic");
			e.preventDefault();
		});

		jQuery("section").find("img").before($top);
	};

	bind_top_nav = function () {
		var $top_nav = jQuery("#top nav a"),
			$top_p = jQuery("#top div.text"),
			$top_p_close = jQuery(
				"<a href=\"javascript:void(0)\" class=\"close\">x</a>"),
			$aside = jQuery("aside:first, #top");

		jQuery("#content section:not(:first)").hide();
		jQuery("#top div.text").html(jQuery("#content section:first div.text").html());

		$top_nav
			.prepend("<span>+</span> ")
			.click(function (e) {
				var $this = jQuery(this),
					i = $top_nav.index(this),
					is_sel = $this.closest("li").hasClass("sel"),
					is_closed = jQuery("#top div.text:hidden").length;

				if (!is_sel || is_closed) {
					jQuery("#top nav li:not(:eq(" + i + "))")
						.removeClass("sel")
						.find("span").text("+");
					$this.closest("li")
						.addClass("sel")
						.find("span").text("-");
					jQuery("#top div.text").slideUp(TIMEOUT, function () {
						window.setTimeout(function () {
							jQuery("#top div.text").html(
								jQuery("#content section:eq(" + i + ") div.text").html());
							jQuery("#top div.text").slideDown(TIMEOUT);
							jQuery("#top h1").slideDown(TIMEOUT);
							if (!is_closed) {
								$aside.animate({top: "0"}, TIMEOUT * 2, "easeInCubic");
								jQuery("html,body").animate({scrollTop: 0}, TIMEOUT, "easeInCubic");
							}
						}, TIMEOUT);
					});
					jQuery("#top a.close").fadeOut(TIMEOUT,
						function () {
						window.setTimeout(function () {
							jQuery("#top a.close").fadeIn(TIMEOUT);
						}, TIMEOUT);
					});
					jQuery("#content section:eq(" + i + ")")
						.slideDown(TIMEOUT * 4, "easeInCubic");
					jQuery("#content section:not(:eq(" + i + "))")
						.slideUp(TIMEOUT * 4, "easeInCubic");
				}

				e.preventDefault();
			})
			.first().find("span").text("-");

		$top_p_close
			.click(function (e) {
				jQuery(this).fadeOut(TIMEOUT);
				jQuery("#top h1, #top div.text").slideUp(TIMEOUT);
				jQuery("#top span").text("+");
				e.preventDefault();
			})
			.appendTo(jQuery("#top"));
		$top_p.not(":first").hide();
	};

	bind_aside_nav = function () {
		var $links = jQuery("aside:first > nav > ul > li > a"),
			$sublinks = jQuery("aside:first > nav > ul > li > ul > li > a"),
			$uls = jQuery("aside:first > nav > ul > li > ul"),
			$sel;

		$sublinks.click(function (e) {
			jQuery("#wrap").loadShiv(jQuery(this).attr("href") + " " + "#content",
				function () {
				refresh();
			});
			$sublinks.removeClass("sel");
			jQuery(this).addClass("sel");
			e.preventDefault();
		});

		$links
			.prepend("<span>+</span> ")
			.click(function (e) {
				var $ul = jQuery(this).closest("li").find("ul:first"),
					i = $uls.index($ul.get(0));
				$uls.not(":eq(" + i + ")")
					.slideUp()
					.closest("li").find("span").text("+");
				$ul
					.slideDown()
					.closest("li").find("span").text("-");
				jQuery("#wrap").loadShiv(jQuery(this).attr("href") + " " + "#content",
					function () {
					refresh();
				});
				$sublinks.removeClass("sel");
				$ul.find("a:first").addClass("sel");
				e.preventDefault();
			});

		$uls.hide();
		$sel = $uls.find("a.sel");
		if ($sel.length === 1) {
			$sel.closest("ul:hidden")
				.slideDown(TIMEOUT)
				.closest("li").find("span").text("-");
		}
	};

	bind_content_links = function () {
		jQuery("#content a.img[target!='_blank']").click(function (e) {
			jQuery.get(jQuery(this).attr("href"), function (data) {
				jQuery("#content").replaceWith(jQuery("#content", data));
				refresh();
			});
			e.preventDefault();
		});
	};

	bind_h1_link = function () {
		var $sublinks = jQuery("aside:first > nav > ul > li > ul > li > a"),
			$uls = jQuery("aside:first > nav > ul > li > ul");
		jQuery("h1 a:first").click(function (e) {
			jQuery("#wrap").loadShiv(jQuery(this).attr("href") + " " + "#content",
				function () {
				refresh();
			});
			$sublinks,removeClass("sel");
			$uls.hide();
			$uls.find("a:first")
				.addClass("sel")
				.closest("ul:hidden").slideDown(TIMEOUT)
				.closest("li").find("span").text("-");
			e.preventDefault();
		});
	};

	bind_ajax_loader = function () {
		jQuery("<div id=\"loader\"><div></div></div>")
			.appendTo(jQuery("body"))
			.ajaxStart(function () {
				jQuery(this).fadeIn(TIMEOUT);
			})
			.ajaxComplete(function () {
				jQuery(this).fadeOut(TIMEOUT);
			});
	};

	refresh = function () {
		var $aside = jQuery("aside:first, #top");

		bind_link_top();
		bind_top_nav();
		bind_content_links();
		DD_belatedPNG.fix("#top, #loader");
		$aside.animate({top: "0"}, TIMEOUT * 2, "easeInCubic");
		jQuery("html,body").animate({scrollTop: 0}, TIMEOUT * 2, "easeInCubic");
	};

	jQuery(function () {
		bind_aside_nav();
		bind_ajax_loader();
		bind_h1_link();
		refresh();
	});
}());

