(function( $ ) {
	var methods = {
		init: function(options) {
			var default_options = {};
			return this.each(function() {
				var $this = $(this);
				var data = $this.data('slider');
				options = $.extend(default_options, options);
				gallery_id = $this.attr('data-gallery_id');
				if (gallery_id) options.gallery_id = gallery_id;
				interval = $this.attr('data-interval');
				if (interval) options.interval = interval;
				in_duration = $this.attr('data-in_duration');
				if (in_duration) options.in_duration = in_duration;
				out_duration = $this.attr('data-out_duration');
				if (out_duration) options.out_duration = out_duration;
				show_captions = $this.attr('data-show_captions');
				if (show_captions) options.show_captions = show_captions;
				show_thumbnails = $this.attr('data-show_thumbnails');
				if (show_thumbnails) options.show_thumbnails = show_thumbnails;
				images_url = $this.attr('data-images_url');
				gallery_url = $this.attr('data-gallery_url');
				var data = {};
				data.options = options;
				$this.data('slider', data);
				// Get the gallery details
				$.post(gallery_url, {'gallery_id': options.gallery_id, 'action': 'slider_get_gallery'}, function(gallery){
					data = $this.data('slider');
					data.gallery = gallery;
					data.current_image_index = 0;
					$this.data('slider', data);
					// Get the image list
					$.post(images_url, {'gallery_id': options.gallery_id, 'action': 'slider_get_image_list'}, function(images){
						data = $this.data('slider');
						data.images = images;
						$this.data('slider', data);
						// Load the first image
						$this.empty().append('<img src="/' + data.gallery.path + '/' + data.images[0].filename + '" alt="' + data.images[0].alttext + '"/>');
						$($this.children('img')[0]).css('opacity', 1);
						if (options.show_captions === 'Yes') {
							$this.append('<div class="slider-image-caption"><p class="slider-image-title">' + data.images[0].alttext + '</p><p class="slider-image-description">' + data.images[0].description + '</p></div>');
							$($this.children('div')[0]).css('opacity', 0.7);
						}
						$($this).slider('load_remaining_images');
						setTimeout(function() {
							$($this).slider('slide_image');
						}, data.options.interval);
					}, 'json');
				}, 'json');
			});
		},
		show_data: function() {
			//var $this = $(this);
			//console.log!="undefined"&&console.log($this.data('slider'));
		},
		load_remaining_images: function() {
			var $this = $(this);
			data = $this.data('slider');
			setTimeout(function() {
				for (i=1; i<data.images.length;i++) {
					$this.append('<img src="/' + data.gallery.path + '/' + data.images[i].filename + '" alt="' + data.images[i].alttext + '"/>');
					$($this.children('img')[i]).css('opacity', 0);
					if (data.options.show_captions === 'Yes') {
						$this.append('<div class="slider-image-caption"><p class="slider-image-title">' + data.images[i].alttext + '</p><p class="slider-image-description">' + data.images[i].description + '</p></div>');
						$($this.children('div')[i]).css('opacity', 0);
					}
				}
			}, 500);
		},
		slide_image: function() {
			var $this = $(this);
			data = $this.data('slider');
			next_image_index = data.current_image_index + 1;
			if (next_image_index === data.images.length)
				next_image_index = 0;
			$this.children('img').slice(next_image_index, next_image_index+1).animate({opacity:1}, data.options.in_duration);
			$this.children('div').slice(next_image_index, next_image_index+1).animate({opacity:0.7}, data.options.in_duration);
			$this.children('img').slice(data.current_image_index, data.current_image_index+1).animate({opacity:0}, data.options.out_duration);
			$this.children('div').slice(data.current_image_index, data.current_image_index+1).animate({opacity:0}, data.options.out_duration);
			data.current_image_index = next_image_index;
			setTimeout(function() {
				$($this).slider('slide_image');
			}, data.options.interval);
		}
	};

	$.fn.slider = function(method) {
		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.slider' );
		}    
	};
})(jQuery);

jQuery(document).ready(function($) {
	$('.slider').slider();
//	$('.slider').slider('show_data');
});
