function setupItemInteractions() {
	$(".item").each(function() {
		var thisItem = $(this);
		$(".close", this).click(function() { thisItem.closeItem(); });
		$(".descriptionTitle", this).click(function() {
			if($(".descriptionText:visible", thisItem).length) {
				thisItem.closeDescription();
			} else {
				thisItem.openDescription();
			}
		});
		$(".imageNavItem", this).each(function(index) {
			$(this).click(function() {
				thisItem.goToImage(index);
			});
		});
		$(".imageContainer", this).each(function(index) {
			$(this).click(
				function() {
					thisItem.goToImage(index);
				}
			).hover(
				function() {
					if($(this).hasClass("selected")) $(this).showCaption();
				},
				function() {
					if($(this).hasClass("selected")) $(this).hideCaption();
				}
			);
		});
	});
	$("#overlay").click(function() { getCurrentItem().closeItem(); });
}

$.fn.extend({
	openItem : function() {
		if(this.hasClass("item")) {
			duration = globalAnimations ? 300 : 0;
			$("#overlay").fadeIn(duration);
			this.resetItem().fadeIn(duration);
			location.hash = "#"+this.attr('id').substr(5);
		} else if(this.hasClass("preview")) {
			var thisItem = $("#item-"+this.attr('id').substr(8));
			duration = globalAnimations ? 300 : 0;
			$("#overlay").fadeIn(duration);
			thisItem.resetItem().fadeIn(duration);
			location.hash = "#"+this.attr('id').substr(8);
		}
		return this;
	},
	closeItem : function() {
		if(this.hasClass("item")) {
			duration = globalAnimations ? 300 : 0;
			this.fadeOut(duration);
			$("#overlay").fadeOut(duration);
			location.hash = "#home";
		}
		return this;
	},
	resetItem : function() {
		if(this.hasClass("item")) {
			if($(".imageContainer", this).length) {
				this.closeDescription();
				// Deselect the image
				$(".imageContainer", this).each(function() { $(this).deselectImageContainer(); });
				$(".imageNavItem.selected", this).removeClass("selected");
				this.goToImage(0);
			} else {
				this.openDescription();
			}
		}
		return this;
	},
	openDescription : function() {
		if(this.hasClass("item")) {
			if(globalAnimations) {
				$(".descriptionText", this).slideDown(300, function() {
					$(".plusminus img", this).removeClass("plus").addClass("minus");
				});
			} else {
				$(".descriptionText", this).show();
				$(".plusminus img", this).removeClass("plus").addClass("minus");
			}
		}
		return this;	
	},
	closeDescription : function() {
		if(this.hasClass("item")) {
			if(globalAnimations) {
				$(".descriptionText", this).slideUp(300, function() {
					$(".plusminus img", this).removeClass("minus").addClass("plus");
				});
			} else {
				$(".descriptionText", this).hide();
				$(".plusminus img", this).removeClass("minus").addClass("plus");
			}
		}
		return this;
	},
	goToImage : function(index) {
		if(this.hasClass("item")) {
			if(index >= 0 && index < $(".imageNavItem", this).length && !$(".imageNavItem", this).eq(index).hasClass("selected")) {
				//console.log("Attempting to slide to image "+index+" of item "+this.attr('id')+".");
				var imageTray = $(".imageTray", this);
				var increments = imageTray.data("increments")
				if(globalAnimations) {
					imageTray.animate({"left":increments[index]}, 300, "swing");
				} else {
					imageTray.css({"left":increments[index]});
				}
				$(".imageContainer.selected", this).deselectImageContainer();
				$(".imageContainer", this).eq(index).selectImageContainer();
				
				$(".imageNavItem.selected", this).removeClass("selected");
				$(".imageNavItem", this).eq(index).addClass("selected");
			}
		}
		return this;
	},
	selectImageContainer : function() {
		if(this.hasClass("imageContainer")) {
			duration = globalAnimations ? 300 : 0;
			this.fadeTo(duration, 1).addClass("selected");
			$(".captionTeaser", this).fadeTo(duration, 1);
		}
		return this;
	},
	deselectImageContainer : function() {
		if(this.hasClass("imageContainer")) {
			duration = globalAnimations ? 300 : 0;
			this.fadeTo(duration, 0.4).removeClass("selected").hideCaption();
			$(".captionTeaser", this).fadeTo(duration, 0);
		}
		return this;
	},
	showCaption : function() {
		if(this.hasClass("imageContainer")) {
			if(globalAnimations) {
				$(".captionContainer", this).slideDown(300);
				$(".captionTeaser", this).slideUp(300);
			} else {
				$(".captionContainer", this).show();
				$(".captionTeaser", this).hide();
			}
		}
		return this;
	},
	hideCaption : function() {
		if(this.hasClass("imageContainer")) {
			if(globalAnimations) {
				$(".captionContainer", this).slideUp(300);
				$(".captionTeaser", this).slideDown(300);
			} else {
				$(".captionContainer", this).hide();
				$(".captionTeaser", this).show();
			}
		}
		return this;
	}
	
});
