document.write('<div class="galpicbig"><img src="" alt="" /><div></div></div>');

$(document).ready(function() {
	setGalPos();
	
	$(".gallery img").hover(function(e) {
		var pos = $(this).parent().offset();
		var t = pos.top - 20 + "px";
		var l = pos.left - 20 + "px";
		$(this).animate({
			width: "130px",
			height: "130px",
			top: t,
			left: l
		}, 150);
	},
	function(e) {
		var pos = $(this).parent().offset();
		var t = pos.top + "px";
		var l = pos.left + "px";
		$(this).animate({
			width: "90px",
			height: "90px",
			top: t,
			left: l
		}, 150);
	});
	var nohide = false;
	var hidden = true;
	$(".gallery img").click(function(e) {
		nohide = true;
		var src = $(this).attr("src");
		var alt = $(this).attr("alt");
		src = src.replace('_sm', '');
		if ($(".galpicbig").css("display") != "none") {
			$(".galpicbig").animate({
				opacity: "hide"
			}, 300);
			$(".galpicbig").queue(function() {
				$(".galpicbig img").attr("src", src);
				$(this).dequeue();
			});
		} else {
			$(".galpicbig img").attr("src", src);
		}
		
		$(".back").remove();
		$(".forward").remove();
		
		$(".galpicbig img").attr("alt", alt);
		$(".galpicbig div").text(alt);
		
		var prevPic = $(this).parent().parent().prev().find("div.img img");
		if (prevPic.html() != null) {
			$(".galpicbig").prepend('<div class="back"></div> ');
			$(".back").click(function(e) {
				$(prevPic).click();
			});
			$(".back").hover(function(e) {
				$(this).html('<b>&larr;</b>');
			},
			function(e) {
				$(this).text('');
			});
		} else {
			$(".back").unbind();
		}
		
		var nextPic = $(this).parent().parent().next().find("div.img img");
		if (nextPic.html() != null) {
			$(".galpicbig").prepend(' <div class="forward"></div>');
			$(".forward").click(function() {
				$(nextPic).click();
			});
			$(".forward").hover(function(e) {
				$(this).html('<b>&rarr;</b>');
			},
			function(e) {
				$(this).text('');
			});
		} else {
			$(".forward").unbind();
		}
	});
	
	$(".galpicbig img").load(function() {
		setGalBigPos();
		$(".back").height($(".galpicbig").height());
		$(".back").width($(".galpicbig").width() / 2);
		$(".forward").height($(".galpicbig").height());
		$(".forward").width($(".galpicbig").width());
		$(".galpicbig").animate({
			opacity: "show"
		}, 300);
		nohide = false;
	});
	$(this).click(hideGalBig);
	$(window).resize(setGalBigPos);
	$(window).scroll(setGalBigPos);
	$(window).resize(setGalPos);
	$(window).scroll(setGalPos);
	
	function hideGalBig() {
		if (!nohide) {
			$(".galpicbig").animate({
				opacity: "hide"
			}, 300);
		} else {
			nohide = false;
		}
	}
	
	function setGalBigPos() {
		$(".galpicbig").animate({
			top: ($(window).height() - $(".galpicbig").height()) / 2 + $(window).scrollTop(),
			left: ($(window).width() - $(".galpicbig").width()) / 2 + $(window).scrollLeft()
		}, 300, "linear");
	}
	
	function setGalPos() {
		$(".gallery img").each(function() {
			var pos = $(this).parent().offset();
			var t = pos.top + "px";
			var l = pos.left + "px";
			$(this).css( { "left": l, "top": t } );
		});
	}
	
	$(document).keydown(function (e) {
		if (e.which == 37) {
			$(".back").click();
		} else if (e.which == 39) {
			$(".forward").click();
		} else if (e.which == 27) {
			hideGalBig();
		}
	});
});