// Photo Gallery Class
var Gallery = {};
Gallery.images = null;
Gallery._category = null;
Gallery._tScroll = 0;
Gallery._scrollStep = 1;
Gallery._imgCount = 0;
Gallery._imgWidth = 0;
Gallery._IMGNO = 0;
Gallery.THUMB_SLIDE_KEEP_OPEN = false;

Gallery.preloadImages = function(_imageArray, _category) {
	Gallery._category = _category;
	Gallery.images = _imageArray;
	for(i=0; i<Gallery.images.length; i++) {
		var _img = $("<img />");
		$(_img).attr("src", "/lib/photos/" + Gallery._category + "/thumbs/" + Gallery.images[i].key);
		$(_img).attr("key", i);
		$(_img).click(function() {
			Gallery.loadImage($(this).attr("key"));
		});
		$(_img).hover(
			function() {
				$(this).next().css("display","block");
			},
			function() {
				$(this).next().css("display","none");
			}
		);

		$("div#thumbsContainer").append(_img);
		$("div#thumbsContainer").append("<div class='balloon'>" + (i + 1) + "</div>");
	}
	Gallery.loadImage(0); // Automaticaly show first picture in gallery
}

Gallery.loadImage = function (_key) {
	Gallery._IMGNO = parseInt(_key);
	switch (Gallery._IMGNO) {
		case -1:
			Gallery._IMGNO = Gallery.images.length - 1;
			break;
		case Gallery.images.length:
			Gallery._IMGNO = 0;
			break;
	}
	$("img#mainImage").attr("src", "/lib/photos/" + Gallery._category + "/photos/" + Gallery.images[Gallery._IMGNO].key);
	$("div#counter").html((Gallery._IMGNO + 1) + "/" + Gallery.images.length);
}

Gallery._scroll = function() {
	$("div#thumbsScrollBox").animate(
		{scrollTop: "+=" + Gallery._scrollStep},
		{
			easing: "linear",
			duration: 50,
			complete: function() {
				if (Gallery._tScroll == 1) {
					Gallery._scroll();
				}
			}
		}
	);
}
Gallery.Thumbs = {};

Gallery.Thumbs.Marker = {};
Gallery.Thumbs.Marker.pause = false;
Gallery.Thumbs.Marker.pulse = function () {
	if (Gallery.Thumbs.Marker.pause == true) {
		return;
	}
	else {
		$("div#thumbsMarker").fadeOut(2000).fadeIn(2000, function() {
			Gallery.Thumbs.Marker.pulse();
		});
	}
}

// Start pulse on load
Gallery.Thumbs.Marker.pulse();

$("div#thumbsScrollBox").mousemove(function(e) {
	var _w = 600;
	var _x = (e.pageY - $(this).offset().top) - (_w/2);
	var _xPerc = (_x/_w) * 2;
	var _speedMax = 10;
	var _speed = _speedMax * _xPerc;
	Gallery._scrollStep = _speed;
	if (Gallery._tScroll == 0) {
		Gallery._tScroll = 1;
		Gallery._scroll();
	}
});

$("div#thumbsScrollBox").hover(
	function() {
		Gallery.Thumbs.Marker.pause = true;
		$("div#thumbsMarker").stop().fadeOut(500);
		Gallery.THUMB_SLIDE_KEEP_OPEN = true;
		$(this).animate({right: "0px"});
		setTimeout(function() {Gallery.THUMB_SLIDE_KEEP_OPEN = false}, 250);
	},
	function() {
		if (Gallery.THUMB_SLIDE_KEEP_OPEN == false) {
			setTimeout(function() {
				Gallery.THUMB_SLIDE_KEEP_OPEN = false;
				$("div#thumbsScrollBox").animate({right: "-145px"});
				Gallery.Thumbs.Marker.pause = false;
				$("div#thumbsMarker").stop().animate({opacity:1.0},500, function() {
					Gallery.Thumbs.Marker.pulse();
				});
			}, 250);
		}
		Gallery._tScroll = 0;
	}
);