(function($) {
	var slideshow_methods = {
		init:function(options) {
			var settings = {
				self     :this,
				path     :'',
				images   :[],
				captions :false,
				capGap   :0,
				delay    :2000,
				fade     :1000,
				width	 :200, //max
				height   :200, //max must include caption block
				color    :'#FFFFFF',
				top	 :false, //align to the top or center
				random   :true,
				first    :false
			};
			return this.each(function() {
				if (options) { 
					$.extend(settings,options);
				}
				if (settings.random) {
					settings = $(this).slideshow('randomize',settings);
				}
				$(this).data('settings',settings);
				$(this).css({
					position:'relative',
					width:String(settings.width) + 'px',
					height:String(settings.height) + 'px',
					margin:'0',
					padding:'0',
					backgroundColor:settings.color,
					overflow:'hidden'
				});
				$(this).slideshow('load',0);
				$('.incomingSlide img').load(function() {
					$(settings.self).slideshow('swap');
				});
			});
		},
		randomize:function(settings) {
			var a = [];
			var i = 0;
			var j = 0;
			var ni = [];
			var nc = settings.captions?[]:false;
			while (i < settings.images.length) {
				a.push(i);
				i++;
			}
			settings.first?a.shift():false;
			a.sort(function() {
				return (Math.round(Math.random()) - 0.5);
			});
			settings.first?a.unshift(0):false;
			while (j < settings.images.length) {
				ni.push(settings.images[a[j]]);
				if (settings.captions) {
					nc.push(settings.captions[a[j]]);
				}
				j++;
			}
			settings.images = ni;
			settings.captions = nc;
			return settings;
		},
		getNext:function() {
			var settings = $(this).data('settings');
			$(this).slideshow('load',($('.currentSlide').data('index') + 1)%settings.images.length);
			window.setTimeout(function() {
				if ($('.incomingSlide img').data('loaded')) {
					$(settings.self).slideshow('swap');
				} else {
					$('.incomingSlide img').load(function() {
						$(settings.self).slideshow('swap');
					});
				}
			},settings.delay);
		},
		makeSlide:function(imageIndex) {
			var settings = $(this).data('settings');
			var $slide = $('<div class="slideHolder incomingSlide"></div>');
			$slide.css({
				display:'none',
				zIndex:'2000',
				position:'absolute',
				top:0,
				left:0,
				width:String(settings.width) + 'px',
				height:String(settings.height) + 'px',
				background:settings.color
			});
			$slide.data('index',imageIndex);
			var $img = $('<img src="' + settings.path + settings.images[imageIndex] + '" alt="Scandinavia House" />');
			$img.css({
				display:'block',
				position:'absolute',
				left:0,
				top:0
			});
			$img.data('loaded',false);
			$img.load(function() {
				$(this).data('loaded',true);
			});
			$slide.append($img);
			if (settings.captions && settings.captions[imageIndex]) {
				var $caption = $('<div class="caption">' + settings.captions[imageIndex] + '</div>');
				$caption.css({
					display:'block',
					position:'absolute',
					left:0,
					top:0,
					width:String(settings.width) + 'px'
				});
				$slide.append($caption);
			}
			return $slide;
		},
		load:function(imageIndex) {
			$(this).append($(this).slideshow('makeSlide',imageIndex));
		},
		swap:function() {
			var settings = $(this).data('settings');
			var imgTop = settings.top?0:Math.round((settings.height - $('.incomingSlide img').height()) / 2);
			$('.incomingSlide').css('opacity',0).show();
			$('.incomingSlide img').css({
				left:String(Math.round((settings.width - $('.incomingSlide img').width()) / 2)) + 'px',
				top:String(imgTop) + 'px'
			});
			if (settings.captions) {
				$('.incomingSlide .caption').css({
					top:String(settings.capGap + imgTop + $('.incomingSlide img').height()) + 'px'
				});
			}
			$('.incomingSlide').hide().css('opacity',1);
			$('.incomingSlide').fadeIn(settings.fade, function() {
				$('.currentSlide').remove();
				$('.incomingSlide').addClass('currentSlide');
				$('.currentSlide').removeClass('incomingSlide');
				$('.currentSlide').css({zIndex:'1000'});
				$(settings.self).slideshow('getNext');
			});
		}
		
	};
	$.fn.slideshow = function(method) {
		if (slideshow_methods[method]) {
			return slideshow_methods[method].apply(this,Array.prototype.slice.call(arguments,1));
		} else if (typeof method === 'object' || !method) {
			return slideshow_methods.init.apply(this,arguments);
		} else {
			$.error('Method ' +  method + ' does not exist on jQuery.slideshow');
		}
	};
}(jQuery));
