$(document).ready(function() {
	
	getGalleryCats();
	
	var currGallery = "";
	
	function getGalleryCats() {
		$.ajax({
			type: "GET",
			url: "/lib/gallery-cats.php",
			dataType: "xml",
			success: function(xml) {
				var x = 0;
				$(xml).find("type").each(function() {
					if (x == 0) { currGallery = $(this).attr("id"); }
					$("#gallery-selector #gallery-nav").append("<li rel=\""+$(this).attr("id")+"\"><a href=\"javascript:void(0);\" rel=\""+$(this).attr("id")+"\" class=\"navLink\">"+$(this).text()+"</a></li>");
					x++;
				});
				
				getGallery(currGallery);
				
				$(".navLink").live("click", function(e) {
					var s = e.currentTarget.rel;
					getGallery(s);
				});
				
				$('.galleries').jScrollPane({
					showArrows:true, 
					scrollbarWidth: 17,
					dragMaxHeight: 300,
					maintainPosition: false,
					reinitialiseOnImageLoad: true
				});
			}
		});
	}
	
	function getGallery(g) {
		currGallery = g;
		
		$.ajax({
			type: "GET",
			url: "/lib/gallery-images.php?t="+g,
			dataType: "xml",
			success: function(xml) {
				$("#gallery .content").empty();
				
				var id = $(xml).find("gallery").attr("id");
				var title = $(xml).find("gallery").attr("type");

				$("#gallery .content").append("<ul id=\"gallery-"+id+"\" class=\"imageWall\" title=\""+title+"\"></ul>");
				
				$(xml).find("image").each(function() {
					$("#gallery .content #gallery-"+id).append("<li><a href=\""+$(this).attr("full")+"\" title=\""+$(this).find("title").text()+"\"><img src=\""+$(this).attr("thumb")+"\" alt=\""+$(this).find("caption").text()+"\" title=\""+$(this).find("title").text()+"\" border=\"0\" /></a></li>");
					
					$("#gallery-"+id).imageWall({
						showNumbers: false,
						showCaptions: false,
						inStyle: 'slide',
						inSpeed: 'fast',
						outStyle: 'slide',
						outSpeed: 'fast'
					});
					
					$("#gallery-"+id+" a").lightBox();
				});
				
				$("#gallery .content #gallery-"+id).append("<div class=\"clearFix\"></div>");
				
				$('#gallery .content').jScrollPane({
					showArrows:true, 
					scrollbarWidth: 17,
					dragMaxHeight: 300,
					maintainPosition: false,
					reinitialiseOnImageLoad: true
				});

				highlightCurrent();
			}
		});
	}
	
	function highlightCurrent() {
		$("#gallery-nav li").removeClass("bgShade");
		
		if (currGallery == "") {
			currGallery = $("#gallery-nav li:first a").attr('rel');
			$("#gallery-nav li:first").addClass("bgShade");
		} else {
			$("#gallery-nav li[rel="+currGallery+"]").addClass("bgShade");
		}
	}
	
});

	
