(function($) {
	//
	// HTML slideshow for jQuery 1.3.2
	//
	// "Member functions" for the plugin, called in the style of jQuery UI's member functions
	function showSlide(index) {
		var index = index % $(this).data('numberOfSlides');
		return this.each(function() {
			if ($(this).data('slideIndex') != index) {
				$(this).data('slideIndex',index).children('.html-slideshow-slide').fadeOut('slow').eq(index).fadeIn('slow');
				$(this).find('.html-slideshow-control'+(index+1)).addClass('selected').siblings().removeClass('selected');
			}
		});
	}
	function next() {
		return this.each(function() {
			$(this).htmlslides('showSlide',$(this).data('slideIndex')+1);
		});
	}
	function prev() {
		return this.each(function() {
			$(this).htmlslides('showSlide',$(this).data('slideIndex')-1);
		});		
	}
	function timer() {
		var returnValue = ($(this).data('timer') == undefined) ? this : next.apply(this, []);
		var me = this;
		$(this).data('timer',setTimeout(function() { return timer.apply(me, Array.prototype.splice.call(arguments, 1)); },$(this).data('interval')));
		return returnValue;
	}
	function stopTimer() {
		clearTimeout($(this).data('timer'));
	}
	$.fn.htmlslides = function(options) {
		// build main options before element iteration
		var opts = $.extend({}, $.fn.htmlslides.defaults, options);
		// The following line of code sets up functions the user of the plugin can call by re-using the plugin: e.g. $('#slideshow').htmlslideshow('next').htmlslideshow('stopTimer');
		var functions = {'stopTimer':stopTimer, 'showSlide':showSlide, 'next':next, 'prev':prev};
		var $this = $(this);
		$this.addClass('html-slideshow');
		if (functions[options] != undefined) {
			return functions[options].apply(this, Array.prototype.slice.call(arguments, 1));
		}
		$this.children().addClass('html-slideshow-slide');
		$this.data('numberOfSlides',$(this).children('.html-slideshow-slide').size());
		$this.prepend($('<div class="html-slideshow-controls"></div>'));
		$this.children('.html-slideshow-slide').css('position','absolute').hide().each(function(index) {
			// if the slide has a data-href attribute set, go to the specified URL
			if ($(this).attr('data-href') != undefined) {
				if (opts.hrefExclusionSelector != undefined) {
					$this.find(opts.hrefExclusionSelector).bind('mouseenter mouseleave click',function(event) {
						$(this).css('cursor','auto');
						event.stopPropagation();
					});
				}
				$(this).hover(function() {
					$(this).css('cursor','pointer');
				});
				$(this).click(function(event) {
					location.href = $(this).attr('data-href');
				});
			}
			var controlText = $(this).attr('data-control-text') != undefined ? $(this).attr('data-control-text') : (index+1);
			var currentControl = $('<div class="html-slideshow-control html-slideshow-control'+(index+1)+'"><a href="#">'+controlText+'</a></div>');
			if (opts.controlOrientation == 'horizontal') {
				currentControl.css('float','left');
			}
			currentControl.data('index',index).click(function(event) {
				event.preventDefault();
				$this.htmlslides('showSlide',index).htmlslides('stopTimer');
			});
			$this.find('div.html-slideshow-controls').append(currentControl);
		});
		$this.find('div.html-slideshow-controls').each(function() {
			$(this).css('position','absolute');
			$(this).css('z-index','5000');
			if (opts.controlVAlign == 'bottom') {
				$(this).css('margin-top',$this.outerHeight()-$(this).outerHeight());
			}
			if (opts.controlAlign == 'right') {
				$(this).css('margin-left',$this.outerWidth()-$(this).outerWidth());
			}
		});
		$this.data('interval',opts.interval);
		timer.apply(this, [opts.interval]);
		return showSlide.apply(this, [opts.startIndex]);
	};
	$.fn.htmlslides.defaults = {
		interval: 10000,
		startIndex: 0,
		controlAlign: 'right',
		controlVAlign: 'bottom',
		controlOrientation: 'horizontal'
	};
})(jQuery);
