/*!
 * jQuery PerthWeb Slideshow Plugin v1.0
 * http://www.perthweb.com.au/
 *
 * Copyright 2011, PerthWeb Pty Ltd
 *
 * Date: Wed June 29 14:00:00 2011 -0800
 */
(function( $ ) {

	var defaultOptions = {
		'albumPath': '/slideshow/xml/album_id/',
		'albumId' : 1,
		'itemClass': 'pwslideshow',
		'cycleOptions': {}
	};

	// Encapsulate public methods in an object literal to keep them within a single namespace.
	var methods = {

		/**
		 * Initialises the slideshow and generates the slideshow items
		 *
		 * @param {jQuery} options User defined plugin options
		 * @returns {jQuery}
		 */
		init : function( options ) {

			return this.each( function() {

				var $this = $(this),
					data = $this.data('pwslideshow');

				// If the plugin hasn't been initialized yet
				if( !data ) {
					// Initialise slideshow data
					$this.data('pwslideshow', defaultOptions);

					// Merge default options with user options
					if( options ) {
						$.extend($this.data('pwslideshow'), options);
					}

					// Load the xml file containing album information
					_loadXml.call(this, function( xml ) {
						$this.data('pwslideshow').xml = xml;
					});

					//data = $this.data('pwslideshow');
				}
				
			});
		},

		/**
		 * Commences the slideshow
		 * 
		 * @returns {jQuery}
		 */
		start : function() {

			return this.each( function() {

				var $this = $(this),
					data = $this.data('pwslideshow');
					
			});
		}
	};

	/**
	 * Loads xml data for slideshow
	 *
	 * @param function onLoadFunction Callback function when xml is loaded
	 */
	function _loadXml( onLoadFunction ) {

		var $this = $(this),
			data = $this.data('pwslideshow');
			xmlPath = data.albumPath + data.albumId;

		$.ajax({
			type: "GET",
			url: xmlPath,
			dataType: "xml",
			success: function( xml ) {
				if( typeof onLoadFunction == 'function') {
					onLoadFunction.call($this, xml);
				}
			},
			error: function( ) {
				$.error('Failed to load xml from ' + xmlPath + ' in jQuery.pwslideshow');
			}
		});
	}


	/**
	 * PerthWeb Slideshow
	 */
	$.fn.pwslideshow = function( method ) {

		// Method calling logic
		if( methods[method] ) {
			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
		}
		else if ( typeof method === 'object' || !method ) {
			return methods.init.apply(this, arguments);
		}
		else {
			$.error('Method ' +  method + ' does not exist on jQuery.pwslideshow');
		}
	};

})(jQuery);


