$(function () {
	function k() {
		$("#slideshow").unbind("mouseenter").bind("mouseenter", l).andSelf().unbind("mouseleave").bind("mouseleave", m)
	}
	function l() {
		$("#controls").stop().animate({
			right: "15px"
		}, 300)
	}
	function m() {
		$("#controls").stop().animate({
			right: "-110px"
		}, 2E3)
	}
	function n() {
		i();
		$("#pause_play").addClass("pause").removeClass("play");
		o = setInterval(i, t)
	}
	function g() {
		$("#pause_play").addClass("play").removeClass("pause");
		clearTimeout(o)
	}
	function i() {
		++d;
		j("r")
	}
	function j(b) {
		p();
		var a = $("#thumbs .thumb_wrapper:nth-child(" + c + ")").find("a:nth-child(" + parseInt(d - h * (c - 1)) + ")").find("img");
		if (a.length) {
			var q = a.attr("alt"),
				b = $("#wrapper").find("img");
			b.length ? b.fadeOut(function () {
				$(this).remove();
				$("<img />").load(function () {
					var a = $(this);
					r(a);
					a.hide();
					$("#wrapper").empty().append(a.fadeIn())
				}).attr("src", q)
			}) : $("<img />").load(function () {
				var a = $(this);
				r(a);
				a.hide();
				$("#wrapper").empty().append(a.fadeIn())
			}).attr("src", q)
		} else b == "r" ? --d : b == "l" && ++d, p()
	}
	function p() {
		$("#thumbs").find(".thumb_wrapper:nth-child(" + c + ")").hide();
		c = Math.ceil(d / h);
		c > s ? d = c = 1 : c == 0 && (c = s, d = c * h);
		$("#thumbs").find(".thumb_wrapper:nth-child(" + c + ")").show()
	}
	function r(b) {
		var a = new Image;
		a.src = b.attr("src");
		var c = a.width,
			d = a.height;
		if (c > 836) {
			var e = 836,
				f = d / (c / 836);
			f > 420 ? (a.width = e / (f / 420), a.height = 420) : (a.width = e, a.height = f)
		} else if (d > 420) f = 420, e = c / (d / 420), e > 836 ? (a.height = f / (e / 836), a.width = 836) : (a.width = e, a.height = f);
		b.css({
			width: a.width,
			height: a.height
		})
	}
	var t = 4E3,
		o, d = 0,
		c = 0,
		s = 1,
		h = 6;
	n();
	k();
	$("#grid").bind("click", function (b) {
		m();
		$("#slideshow").unbind("mouseenter").unbind("mouseleave");
		g();
		$("#thumbs").stop().animate({
			top: "0px"
		}, 500);
		b.preventDefault()
	});
	$("#thumb_close").bind("click", function (b) {
		l();
		k();
		$("#thumbs").stop().animate({
			top: "-230px"
		}, 500);
		b.preventDefault()
	});
	$("#pause_play").bind("click", function (b) {
		$(this).hasClass("play") ? n() : g();
		b.preventDefault()
	});
	$("#next").bind("click", function (b) {
		g();
		i();
		b.preventDefault()
	});
	$("#prev").bind("click", function (b) {
		g();
		--d;
		j("l");
		b.preventDefault()
	});
	$("#thumbs .thumb_wrapper > a").bind("click", function (b) {
		var a = $(this);
		$("#thumb_close").trigger("click");
		var c = a.index(),
			a = a.parent().index();
		d = parseInt(a * h + c + 1);
		j();
		b.preventDefault()
	}).bind("mouseenter", function () {
		$(this).stop().animate({
			opacity: 1
		})
	}).bind("mouseleave", function () {
		$(this).stop().animate({
			opacity: 0.5
		})
	})
});
