var AjaxSlides = {

	init: function(xml_path){

	    $.ajax({

	        type: "GET",

	        url: xml_path,

	        dataType: "xml",

	        success: function(xml) {

				browseXML.init(xml);

	        }

	    });



		$("li#next a").click(function(){

			browseXML.nextProduct();

			return false

		});



		$("li#prev a").click(function(){

			browseXML.prevProduct();

			return false

		});	

	}

}











var browseXML = {

	

	current_product: 0,

	

	product_array: [],

	

	init: function(xml){

		

		

		

		// Animation doesn't exist - create it

		var ani = $("<span>").attr("id", "loading-ani").prependTo("div#imagecycler").hide();

		ani.text("Loading...");

		

		// Create ul to add products to

		$("<ul>").attr("id", "products-list").appendTo("div#imagecycler");

		

		// Parse XML

		$(xml).find('product').each(function(i){

			var product = {};


			product.img = $(this).attr('img');			

			product.url = $(this).attr('url');

			product.loaded = 0;

			browseXML.product_array.push(product);

			$("<li>").attr("id","pro-"+i).addClass("cycled-product").appendTo("div#imagecycler ul#products-list");

		});

		

		// Show first product

		this.createProduct(0);

	},

	

	showProduct: function(id){

		var product = this.product_array[id];	

		var prev_product = this.product_array[this.current_product];

		

		prev_product.obj.hide();

		

		

	

		if (product.loaded == 0) {

			this.createProduct(id);

			return;

		}



		this.current_product = id;

		this.product_array[id].obj.show();			

	},

	

	

	createProduct: function(id){

		// shorten the product obj we're using

		var product = this.product_array[id];

		

		// Build the HTML

		var linkage = $("<a>").attr("href",product.url);

		var details = $("<div>");

		

		var image = $('<img>').attr("width", 458).attr("height", 306);

		

		linkage.append(image).append(details);

		

		product.obj = $("div#imagecycler #pro-"+id).append(linkage);

		

		// Callback for ajax image loading below

		var isLoaded = function() {

			product.loaded = 1;

			image.show();

			browseXML.showProduct(id);

		};



		// Load image data via ajax then call isLoaded once complete

		image.load(product.img, null, isLoaded()).attr("src", product.img);



	},

	

	nextProduct: function(){

		var next = this.current_product < (this.product_array.length - 1) ? this.current_product + 1 : 0;

		this.showProduct(next);

	},

	

	prevProduct: function(id){

		var prev = this.current_product == 0 ? this.product_array.length - 1 : this.current_product - 1;

		this.showProduct(prev);

	}

}





var jXML = {

    getCount: function(xml,nodes) {

        var response = {};

        for (var node in nodes) {

            response[node] = $(nodes[node],xml).length;

        }

        return response;

    },

    getAttribute: function(xml,nodes) {

        var response = {};

        for (var node in nodes) {

            if (nodes[node][2] == null) {

                response[node] = $(nodes[node][0] + "[" + nodes[node][1] + "]",xml);

            } else {

                if ($(nodes[node][0],xml).attr(nodes[node][1]) == nodes[node][2]) {

                    response[node] = $(nodes[node][0],xml);

                }

            }

        }

        return response;

    }

};
