Gallery = {
	currentImage: 0,
	imageHolders: null,
	imageHolderWidth: null,
	imageCountHolder: null,
	maxCount: null,
	nextButton: null,
	prevButoon: null,
	init: function (imageHolder, imageWrapperWidth, imageCountHolder, nextButton, prevButton, descriptionHolder, descriptionWidth) {
		// set custom variables
		this.imageHolder = imageHolder;
		this.imageWrapperWidth = imageWrapperWidth;
		this.imageCountHolder = imageCountHolder;
		this.nextButton = nextButton;
		this.prevButton = prevButton;
		this.descriptionHolder = descriptionHolder;
		this.descriptionWidth = descriptionWidth;
		this.allImages = $(".image", imageHolder);
		this.maxCount = this.allImages.length;
		
		// check if a specific image has been specified in the URL
		if (document.URL.match(/#[0-9]+/)) {
			this.gotoImage(new Number(new String(document.URL.match(/#[0-9]+/)).replace("#", "")) - 1);
		} else {
			// write maxCount
			this.updateCount(0);
		}
		// 
		this.attachEvents();
		if (window.autoGallery) {
			setTimeout(function() {
				Gallery.next();
			}, 2000);
		}
	},
	attachEvents: function () {
		// write next/prev functions
		this.nextButton.click(function () {
			Gallery.next();
			this.blur();
			return false;
		});
		this.allImages.click(function () {
			Gallery.next();
			this.blur();
			return true;
		});
		this.prevButton.click(function () {
			Gallery.previous();
			this.blur();
			return false;
		});
	},
	next: function () {
		// show next image
		this.gotoImage(this.currentImage + 1);
		if (window.autoGallery) {
			setTimeout(function() {
				Gallery.next();
			}, 2000);
		}
	},
	previous: function () {
		// show previous image
		this.gotoImage(this.currentImage - 1);
	},
	updateCount: function (newCount) {
		// set current image
		this.currentImage = newCount;
		// update current image display
		if (this.imageCountHolder) {
		this.imageCountHolder.innerHTML = (newCount + 1) + "/" + this.maxCount;
		}
		// update url hash
		//window.location.hash = (newCount + 1);
	},
	gotoImage: function (num) {
		// if not too high
		if (num >= this.maxCount) {
			num = 0;
		} else if (num < 0) {
			num = this.maxCount - 1;
		}
		//animate
		this.animateContainers(num);
		// update count
		this.updateCount(num);
	},
	animateContainers: function (num) {
		var a = this.allImages.eq(num);
		this.allImages.hide();
		a.show();
		// this.imageHolder.css({
		// 	marginLeft: (num * this.imageWrapperWidth) * -1 + "px"
		// });
		// 
		// // skip attempt to animate description holder if it does not exist
		// if (!this.descriptionHolder) return;
		// 
		// this.descriptionHolder.css({
		// 	marginLeft: (num * this.descriptionWidth) * -1 + "px"
		// });
	}
};

$(function () {

	// don't init gallery if it consists of a single photo
	if ($("div.image").length > 1) {
		// wrap images
		//$("div.image").wrapAll("<div id='image-wrapper'><div id='image-holder'></div></div>");
		//$("div.image").show();

		// init gallery: Gallery.init(imageHolder, imageWrapperWidth, imageCountHolder, nextButton, prevButton)
		Gallery.init($("div#media"), 560, $("span#gallery-count").children("span")[0], $("a#next-image"), $("a#previous-image"));

	} else {
		// hide gallery count and navigation
		$("span#gallery-count, div#gallery-navigation").hide();
		// show project navigation
		$("p#project-count").show();
	}
});
