$(document).ready(function() {
	// adding button listeners to each item
	var listItem = $('#exhibit ul').children('li');
	jQuery.each(listItem, function() {
		var item = $(this);
		item.click(function() { changeSlide(item); return false; });
	});
	
	$(".exhibit_frame img").each(function() {
		if($(this).height() > 230) {
			$(this).css({ height: 230 + "px" });
		}
	});
	
	$("#tour-info .dateRow").each(function() {
		if(!$(this).hasClass("active")) { $(this).children(".background").fadeTo(200, 0.5); }
	});
	
	$("#tour-info .dateRow").hover(
		function() { if(!$(this).hasClass("active")) { $(this).children(".background").fadeTo(200, 1.0); } },
		function() { if(!$(this).hasClass("active")) { $(this).children(".background").fadeTo(200, 0.5); } }
	);
	
	$("#tour-info .dateRow").click(function() {
		window.location.href	= "/tour/info?city=" + $(this).attr("id");
	});
	
	$("#tour-info .dateRow .info .right img").click(function(e) { e.stopPropagation(); });
	
	$("#tour .thumbs img").click(function() {
		url	= $(this).parent().attr("href");
		$o	= $("<div>").attr("id", "overlay");
		$("body").append($o);
		$("#overlay")
			.css({ display: "block" })
			.fadeTo(1, 0.8)
			.animate({ height: $(document).height() > $(window).height() ? $(document).height() : $(window).height() }, 300, function() { loadPic(url); });
		return false;
	});
	
	function loadPic(url) {
		$p	= !exists("#viewer") ? $("<div>").attr("id", "viewer") : $("#viewer");
		if(!$("#overlay").siblings("#viewer").length > 0) { $p.insertAfter($("#overlay")); }
		$p.children("img").each(function() { $(this).fadeOut(150, function() { $(this).remove(); })	});
		$p.append($("<a>").attr("href", "#").text("[x] Close").fadeTo(150, 0.6));
		$p.append($("<img>").attr("src", url));
		$p.css({ marginTop: -($("#viewer img").height()), top: -200 }).animate({ width: $("#viewer img").width(), height: $("#viewer img").height() });
		attachEvents();
	}
	

	
});

function changeSlide(elem) {
	var item = $('#'+elem[0].id);
	var slide = item.children(0).attr('href');
	
	moveSlide(slide);
	moveDecoration(elem);
}

function moveDecoration(elem) {
	// calculating contained offset
	var containerOffset = $('.exhibit_controls ul').offset().top;
	var itemOffset = elem.offset().top;
	var realOffset = itemOffset - containerOffset;

	// applying offset to decoration
	var decoration = $('.decoration');
	decoration.animate({'top': realOffset}, 'slow', 'easeOutBounce');
}

function moveSlide(slide) {
	var slideObj = $(slide);
	var slideWindow = $('.exhibit_slider');

	var containerOffset = slideWindow.offset().top;
	var slideOffset = slideObj.offset().top;
	var realOffset = containerOffset - slideOffset;

	slideWindow.animate({'top': realOffset}, 'slow');
}

function attachEvents() {
	$("#overlay, #viewer a").click(function(e) {
		$("#viewer").fadeOut(150, function() {
			$("#overlay").animate({ height: 0 }, function() {
				$(this).remove();
			})
			$(this).remove();
		});
		return false;
		e.stopPropagation();
	});
}

function exists(e) {
	return $(e).length > 0
}