//this wrap stops the plugin from interfering with other js libraries that use the $ function
(function($){
	$.fn.slideshow = function(options) {
		//set default options
		var	defaults = {  
			items : 'img', //items that will be faded in and out etc
			navigation: false //weather to use navigation
		},
		settings = $.extend({}, defaults, options),
		$this = $(this),
		navholder = $(settings.navigation),
		nextSlide = $this.find(settings.items+':first-child'),
		nextButton,
		pause = false,
		startfading = false,
		stop = false,
		setup = function() {
			$this.find(settings.items).hide();
			var i = 1;
			$this.find(settings.items).each(function() {
				var $thisimage = $(this);
				navholder.append('<a href="#" class="image-item">'+i+'</a>');
				navholder.find('a:last-child').click(function() {
					navholder.find('a').removeClass('current');
					$(this).addClass('current');
					stop = true;
					if (startfading) {
						$this.find(settings.items).fadeOut();
						$thisimage.fadeIn();
					}
					startfading = true;
					return false;
				});
				if (i == 1) $thisimage.fadeIn();
				i++;
			});
			nextButton = navholder.find('a:first');
			navholder.append('<a href="#" id="resume-slideshow-button">Resume&nbsp;Slideshow</a>');
			navholder.find('a.image-item').mouseover(function() {
				pause = true;
				navholder.find('#resume-slideshow-button').fadeIn();
			});
			navholder.find('#resume-slideshow-button').click(function() {
				navholder.find('#resume-slideshow-button').fadeOut();
				pause = false;
				return false;
			}).hide();
			tick();
		},
		tick = function() {
			if (!pause || !stop) {
				nextButton.click();
				if (nextButton.get(0) == navholder.find('a:last').get(0)) {
					nextButton = navholder.find('a:first');
				} else {
					nextButton = nextButton.next();
				}
				
			}
			setTimeout(tick,6000);
		};
		setup();
		//tick();
		//return this;
	}
})(jQuery);
