var gallery = {
	"albums":[],
	"photos":[],
	"init":function() {
		gallery.albums = window.albumList;
		gallery.photos = window.photoList;
		$("#albumSelect a").click(function() {
			o = this;
			setTimeout(function() {
				gallery.showAlbums(o);
			},20);
			return false;
		});
		$("#photoDisplay").html("<img src=\""+gallery.photos[0].large+"\" />");
		$("#photoThumbs").removeClass("staticGallery").addClass("jsGallery").html("<div class=\"innerFrame\"><div class=\"innerRow\"></div></div>");
		gallery.showPhotos();
		$(document.body).click(function() {
			setTimeout(function() {
				$("#albumListBox").remove();
			},10);
		});

	},
	"showAlbums":function(el) {
		$("#albumListBox").remove();
		var list = $(document.body).append("<div id=\"albumListBox\"></div>").children("#albumListBox");
		list.css({
			'top':$(el).offset()['top']+$(el).height(),
			'left':$(el).offset()['left']
		});
		for(var m in gallery.albums) {
			var alb = gallery.albums[m];
			list.append("<div class=\"item\"><a href=\"/gallery?album="+alb.id+"\">"+alb.title+" <span>"+alb.photoCount+" "+(alb.photoCount!=1?"photos":"photo")+"</span></a></div>")
		}
	},
	"showPhotos":function() {
		$("#photoThumbs").append("<a href='javascript:;' class='back'></a>"+"<a href='javascript:;' class='next'></a>");
		$("#photoThumbs .back").mousedown(function() { gallery.scroll(-1); });
		$("#photoThumbs .next").mousedown(function() { gallery.scroll(1); });
		$("#photoThumbs .back, #photoThumbs .next").mouseup(gallery.stopScroll).mouseout(gallery.stopScroll);
		for(var m in gallery.photos) {
			var photo = gallery.photos[m];
			$("#photoThumbs .innerRow").append("<div class=\"item\"><img rel=\""+m+"\" onclick=\"gallery.selectPhoto($(this).attr('rel'))\" alt=\""+photo.caption+"\" src=\""+photo.thumb+"\" /></div>");
		}
		$("#photoThumbs .innerRow").css("width",(5+118)*gallery.photos.length);
	},
	"selectPhoto":function(index) {
		var photo = gallery.photos[index];
		$("#photoDisplay .top").remove();
		$("#photoDisplay").append("<img class=\"top\" />");
		$("#photoDisplay .top").load(function() {
			setTimeout(function() {
				$("#photoDisplay .top").fadeIn("fast",function() {
				$("#photoDisplay").html("<img src=\""+photo.large+"\" />");
				});
			},5);
		}).attr("src",photo.large);
	},
	"scroll":function(dir) {
		gallery.timer = setInterval(function() {
			var n = $("#photoThumbs .innerFrame").scrollLeft()+(5*dir);
			$("#photoThumbs .innerFrame").scrollLeft(n);
		},10);
	},
	"stopScroll":function() {
		clearTimeout(gallery.timer);
	}
};
$(document).ready(function() { gallery.init(); });