var g = 0; // current gallery
var gl, row;
var galleries = [];
var loader = 'http://static.letmypeoplego.com/images/loading.gif#';
var gp = 'http://photos.letmypeoplego.com/gallery/';
var gpp = 'http://photos.letmypeoplego.com/photos/';

function load_gallery() {
	if (galleries.length == 0) return;
	var i, photo, gal = galleries[g], temp = [];
	$("#galleryURI")
		.attr("href", gp + gal["smugmug_id"])
		.html(gal["name"]);
	for (i in gal["photos"]) {
		photo = gpp + gal["photos"][i];
		temp.push('<a class="thickbox" rel="gallery" href="' + photo + '-L.jpg"><img src="' + loader + photo + '-Ti.jpg" height="58" alt="" /></a>');
	}
	gl.html(temp.join('\n')).find('img').each(function(){
		this.src=this.src.replace(/.*#/, '');
	});
	tb_init('a.thickbox'); //bind thickbox
}

function init_gallery(){
	if (!galleries || galleries.length == 0)
		return;
	gl = $("div#galleryPhotoList");
	row = gl.children().eq(0);
	
	$("li#galleryBrowsePrevious > a").click(function(){
		g = (--g + galleries.length) % galleries.length;
		load_gallery();
		return false;
	});
	
	$("li#galleryBrowseNext > a").click(function(){
		g = ++g % galleries.length;
		load_gallery();
		return false;
	});
	load_gallery();
};

$(window).load(init_gallery);