$(document).ready(function() {

	var target = "#gallery";
	var sidebar = "#slider";

    $('#gallery span.pic a').live('mouseover', function() {
        var topAdjust = ($.browser.msie) ? -74 : -81;
        var leftAdjust = 11;
        var span = $(this).find("span.j");
        span.css("top", $(this).position().top + topAdjust).css("left", $(this).position().left + leftAdjust);
        span.show();
    }).live('mouseout', function() {
        $(this).find("span").hide();
    });	
    
    $.ajax({
		type: "GET",
		url: getGallery(),
		dataType: "xml",
		success: parseXml
	});
	
	function parseXml(xml) {
		
		$(target).empty();
		$(sidebar).empty();
		
		var c = 1; // category iterator			
		$(xml).find("category").each(function() {
					
			var p = 1; // product iterator
			$(this).find("product").each(function(){
				
				var prodname = $(this).attr("name");
				var partnum  = $(this).attr("number");
			
				var d = 1; // not really needed, but easier than editing the code below
				
				// images iterator
				$(this).find("images").each(function(){
					var i = 1; 
					$(this).find("image").each(function(){
						$(target).append("<span class='pic i"+c+p+d+i+"'></span>");
						$(target +" span.i"+c+p+d+i).append("<a href='"+$(this).find("url").text()+"' class='thickbox i"+c+p+d+i+"'></a>");
						$(target +" a.i"+c+p+d+i).append("<img src='"+$(this).find("thumb").text()+"' class='productimg' alt='Gallery Image' title='"+$(this).find("caption").text()+"' width='190' height='190'/>");
						$(target +" a.i"+c+p+d+i).append("<span class='j j"+c+p+d+i+"' style='display:none;'>"+prodname+"<br/>"+partnum+"</span>");			
					if (i%4 == 0) {
						$(target).append("<br/>");
					}
					i++;
					});
				});
				
				p++;
			});
		
			c++;
		});
			
		thumbs = $("img.productimg");
		var z = 0;
		var refreshId = setInterval(function() {
			 thumbs.eq(z).animate({ opacity:1 },300);
			 z++;
			 if (z > thumbs.length) {
			 	clearInterval(refreshId);
			 }
		}, 50);	

	tb_init('a.thickbox');
	
		
	} // fn parseXml


});
