if (typeof jQuery !== 'undefined') {
	(function ($) {
		$.fn.extend({
			slideShow: function (options) {
				var settings = $.extend({}, $.fn.slideShow.defaults, options),
					i = 0;
			
				return this.each(
					function () {
						var $$, $div, $images, $arrows, $pager,
							id, selector, o, options, total = 0,
							parts = [], pi = 0,
							arrows = ['Previous', 'Next'];
						
						$$			= $(this);
						$images		= $$.clone().addClass('cycle-images');
						o			= $.metadata ? $.extend({}, settings, $$.metadata()) : settings;
						id			= 'slideshow-' + (i++ + 1);
						$div		= $('<div />').addClass('slideshow').attr('id', id);
						selector	= '#' + id + ' ';
						options		= {};
						total		= $images.children().length;

						$div.append($images);
						
						// different effect
						if ($$.is('.slide')) {
							options.fx = 'scrollHorz';
						}

						// don't move by default
						if ($$.is('.stopped')) {
							options.timeout = 0;
						}

						// create the arrows
						if ($$.is('.arrows') && total > 1) {
							options.next = selector + '.next';
							options.prev = selector + '.previous';

							$arrows = $('<ul />').addClass('cycle-arrows');

							$.each(arrows, function (i, val) {
								parts[pi++] = '<li class="' + val.toLowerCase() + '">';
								parts[pi++] = '<a href="#' + val.toLowerCase() + '">';
								parts[pi++] = '<span>' + val + '</span>';
								parts[pi++] = '</a>';
								parts[pi++] = '</li>';
							});

							$arrows.append(parts.join('')).appendTo($div);
							$div.addClass('has-cycle-arrows');
							
							$arrows.find('a').bind('click.cycle', function (event) {
								$(this).blur();
							});
						}

						// create the clickable pagination
						if ($$.is('.pagination') && total > 1) {
							options.pager = selector + '.cycle-pagination';

							$pager = $('<ul />').addClass('cycle-pagination');
							$pager.appendTo($div);
							$div.addClass('has-cycle-pagination');

							$pager.find('a').bind('click.cycle', function (event) {
								$(this).blur();
							});

							if ($$.is('.arrows') && list.length > 1) {
								$div.addClass('has-cycle-arrows-and-pagination');
							}
						}

						$$.replaceWith($div);
						$images.cycle($.extend({}, o, options));
					}
				);
			}
		});
		
		// defaults
		$.fn.slideShow.defaults = {
			images:			null,
			fx:				'fade',
			timeout:		4000,
			speed:			1250,
			prevNextClick:	function (isNext, zeroBasedSlideIndex, slideElement) {
				$(this.next).add(this.prev).children('a').blur();
			},
			pagerClick:		function (zeroBasedSlideIndex, slideElement) {
				$(this.pager).children('a').blur();
			}
		};
		
	}(jQuery));
}
