
	(function($) {

		var images       = new Array();
		var step_timeout = false;

		var container, options;

		$.fn.cycle = function(the_container, the_options) {
			container = the_container;
			options   = $.extend({}, $.fn.cycle.defaults, the_options);

			image_links = $(this);

			image_links.each(function() {
				image_link = $(this);
				
				images.push(image_link.attr('href'));

				image_link.click(function() {
					$.fn.cycle.step(image_links.index(this));
					return false;
				});
			});

			setTimeout(function() { $.fn.cycle.step(1); }, options.wait);
		}



		$.fn.cycle.step = function(swap_to) {

			clearTimeout(step_timeout);

			$('#slideshow_links a').removeClass('current');

			$('#slideshow_links li:nth-child(' + (swap_to + 1) + ') a').addClass('current');

			$.fn.cycle.swap(swap_to);

			next_swap = swap_to + 1 < images.length ? swap_to + 1 : 0;

			step_timeout = setTimeout(function() { $.fn.cycle.step(next_swap); }, options.wait);

		}



		$.fn.cycle.swap = function(swap_to) {

			old_image = $(container + ' img');

			new_src   = images[swap_to];

			if (old_image.attr('src') == new_src) return;

			$(container).addClass('loading').css( { 'height' : old_image.attr('height') } );

			old_image.fadeOut(options.fadeOutDuration, function() {

				old_image.remove();

				var img = new Image();

				$(img).load(function() {

					new_img = $(this);

					new_img.hide();

					$(container).animate({
						'height' : $(this).attr('height')
					}, options.resizeDuration, 'swing', function() {
						$(this).append(new_img);

						$(new_img).fadeIn(options.fadeInDuration, function() {
							$(this).removeClass('loading');
						});
					});

				}).attr('src', new_src);

			});

		}



		$.fn.cycle.defaults = {
			wait            : 5000,
			fadeOutDuration : 250,
			fadeInDuration  : 500,
			resizeDuration  : 250
		};

	})(jQuery);
