$(function () {

	var jspOptions = {
		showArrows: true,
		hideFocus: true,
		verticalGutter: 0
	};

	$("div.sect-body").each(function () {
		$(this).jScrollPane(jspOptions);
	});

	$(window).bind("resize", function() {
		var windowHeight = (typeof( window.innerHeight ) == 'number') ? window.innerHeight : document.documentElement.clientHeight,
			bodyHeight = $("body").height(),
			pwHeight = (windowHeight < bodyHeight) ? windowHeight : bodyHeight,
			oFooter = $("#footer"),
			footerHeight = oFooter.height(),
			pageWrap = $("#pages-wrap"),
			allSections = $("#pages-wrap > div.section"),
			contentWidth = pageWrap.width() - (allSections.length-1)*50,
			logo = $("#logo"),
			scrollerWidth;

		pageWrap.css("height", (pwHeight - footerHeight) + "px");

		if (pageWrap.is(".expanded")) {
			var active = $("#pages-wrap > div.section.active"),
				content = $("#pages-wrap > div.section.active > div.sect-body"),
				contentWrap = content.find("div.sb-wrap"),
				stitle = content.find(".stitle"),
				dX = allSections.length - (allSections.index(active)+1),
				sections = allSections.not(active);
			sections.css("width", "50px");
			active.css("width", contentWidth + "px");
			content.css({
				width: contentWidth + "px",
				height: active.height() + "px"
			});
			stitle.css({ width: contentWidth + "px" });
			content.data('jsp').reinitialise();
			scrollerWidth = content.data('jsp').getIsScrollableV() ? 16 : 0;
			logo.css({ right: (20 + dX * 50) + scrollerWidth + "px"});
		} else {
			allSections.css("width", (100/allSections.length) + "%");
		}
	}).trigger("resize");

	$("#pages-wrap>div.section>a.title").bind({
		click: function () {
			var pageWrap = $("#pages-wrap"),
					logo = $("#logo"),
					parent = $(this).parent(),
					allSections = $("#pages-wrap > div.section"),
					dX = allSections.length - (allSections.index(parent)+1),
					sections = allSections.not(parent),
					content = parent.find(".sect-body"),
					contentWrap = content.find("div.sb-wrap"),
					stitle = content.find("div.stitle"),
					titleImg = stitle.find('img'),
					contentWidth = pageWrap.width() - (allSections.length-1)*50,
					actSect = sections.filter(".active"),
					actTitle = actSect.find("a.title"),
					actTitleBg = actTitle.find("span"),
					actTitleText = actTitle.find("em"),
					scrollerWidth;

			$(this).blur();
			actSect.removeClass("active");
			actTitle.attr("href","#");
			actTitleBg.animate({opacity: '1'}, 200);
			actTitleText.animate({opacity: '0'}, 200);
			parent.addClass("active");
			$(this).removeAttr("href");
			if (pageWrap.is(".expanded")) {
				actSect.animate({width: "50px"}, "fast");
			} else {
				pageWrap.addClass("expanded");
				sections.not(".active").animate({width: "50px"}, "fast");
			}
			content.css({
				width: contentWidth + "px",
				height: pageWrap.height() + "px"
			});
			stitle.css({
				width: contentWidth + "px",
				height: titleImg.height() + "px"
			});
			parent.animate({ width: contentWidth + "px"}, "fast", function () {
				content.data('jsp').reinitialise();
				scrollerWidth = content.data('jsp').getIsScrollableV() ? 16 : 0;
				logo.animate({ right: (20 + dX * 50) + scrollerWidth + "px"}, 200);
			});
			return false;
		},
		mouseenter: function () {
			if ($(this).parents("div.section").is(":not(.active)")) {
				var bg = $(this).find("span"),
						text = $(this).find("em");

				bg.stop(true).animate({opacity: '0'}, 200);
				text.stop(true).animate({opacity: '1'}, 200);
			}
		},
		mouseleave: function () {
			if ($(this).parents("div.section").is(":not(.active)")) {
				var bg = $(this).find("span"),
					text = $(this).find("em");

				bg.stop(true).animate({opacity: '1'}, 200);
				text.stop(true).animate({opacity: '0'}, 200);
			}
		}
	});

	$("#logo a").click(function () {
		var pageWrap = $("#pages-wrap"),
			sections = $("#pages-wrap > div.section"),
			actSect = sections.filter(".active"),
			actTitle = actSect.find("a.title"),
			actTitleBg = actTitle.find("span"),
			actTitleText = actTitle.find("em"),
			actSectContent = actSect.find(".sect-body");

		$(this).blur();
		if (pageWrap.is(".expanded")) {
			pageWrap.removeClass("expanded");
			actSect.removeClass("active");
			actTitleBg.animate({opacity: '1'}, 200);
			actTitleText.animate({opacity: '0'}, 200);
			actSectContent.fadeOut("fast");
			sections.animate({width: (pageWrap.width()/sections.length) + "px"}, 200, function () {
				$("#logo").animate({ right: "20px"}, "fast");
			});
		}
		return false;
	});

	$("div.preview-list div.thumbnails-wrap ul").width(function () {
		return 57 * $(this).children().length - 7;
	});

	$("div.filter ul.sub-menu a").click(function () {
		$(this).parents("div.sect-body").data('jsp').reinitialise();
	});

});
