(function($) {				
	$.fn.chaosWebSlider = function(options) {
		return this.each(function()	{
			var $this = $(this);
			$this.options = $.extend({}, $.fn.chaosWebSlider.defaults, options);
			$this.slides = $("li", $this);
			$this.currentId = -1;
			setup($this);
			slide($this, 0);
		});
	};

	$.fn.chaosWebSlider.defaults = { width: 900, height: 600, speed: 6500, transitionSpeed: 1000 };
	
	function setup($this) {
		$this.slides.each(function(){
			$li = $(this);
			var $h2 = $('h2', $li).detach();
			var $span = $('span', $li).detach();
			var $table = $('<table><tr><td valign="center"></td></tr></table>');
			$h2.appendTo($('td', $table));
			$span.appendTo($('td', $table));
			$table.appendTo($li);			
		});
		$this.slides.eq(0).addClass("currentSlide");			
	}
	
	function slide($this, index) { 
		clearTimeout($this.slideTimer);
		
		$this.currentId = index = (index + $this.slides.length) % $this.slides.length;
		
		$(".currentSlide", $this).fadeOut($this.options.transitionSpeed, function() {
			$(this).removeClass("currentSlide");
			
			var $cur = $this.slides.eq($this.currentId);
			$cur.find('h2, img, span').hide();
			$cur.show();
			$("img", $cur).fadeIn($this.options.transitionSpeed * 2);
			$("h2", $cur).fadeIn($this.options.transitionSpeed);
			$("span", $cur).fadeIn($this.options.transitionSpeed * 1.5);
			$cur.addClass('currentSlide');			
			
			$this.slideTimer = setTimeout(function() { slide($this, index + 1); }, $this.options.speed);
		});			
				
				
		return;
		$(".currentSlide", $this).children('img, h2, span')
			.fadeOut($this.options.transitionSpeed, function() {
				$this.slides.removeClass('currentSlide');
				$this.slides.eq($this.currentId).addClass('currentSlide');
			});
	
		$this.slideTimer = setTimeout(function() { slide($this, index + 1); }, $this.options.speed);
	}
	
})(jQuery);
