var shop = function() {
    return {
	parse: function(xml) {
	    var products = [];
	    $('#shop').html('');
	    $(xml).find('product').each(function() {
		var product = {
		    name: $(this).attr('name'),
		    description: $(this).find(
			'description').text(),
		    price: $(this).find('price').text(),
		    available: true,
		    image: null,
		};
		var image = $(this).find('image');
		if (image) {
		    product.image = {
			path: $(this).find('path').text(),
			thumb: $(this).find('thumb').text(),
			caption: $(this).find('caption').text()
		    };
		};
		if (product.available) {
		    products.push(product);
		}
	    });
	    if (products.length == 0) {
		$('#shop').html('There are no products listed today.');
	    } else {
		for (var p in products) {
		    var product = products[p];
		    var product_image;
		    product_image = product.image.path || 'missing.jpg';
		    product_thumb = product.image.thumb || 'missing.jpg';
		    var title = product.name + ' - ' + product.price;
		    $('#shop').append(
			'<div class="shopfloor">' +
			    '<a href="' + product_image + '"' +
			    '   title="' + title + '">' +
			    '<img src="' + product_thumb + '"' +
			    '     class="left"/></a>' +
			    '<p class="right">' + product.price +
			    '</p>' +
			    '<p class="description">' +
			    product.description + '</p>' +
			    '</div>');
		}
	    }
	},
	load: function(filename, complete) {
	    var that = this;
	    $.ajax({url: filename,
		    dataType: "xml",
		    error: function(request, textStatus, errorThrown) {
			$('#shop').html(
			    '<p class="error">Error loading product' +
				' information. Please try again later.</p>');
		    },
		    success: function(xml) {
			that.parse(xml);
			complete();
		    }});
	}
    };
}();
