jQuery.fn.splash = function(options){
	if (typeof options == 'undefined') options = {};
	jQuery.extend({
		speed: 1000,
		duration: 400
	}, options);
	return jQuery(this).each(function(){
		var element = jQuery(this);
		var slides = element.children();
		var titles = (options.titles)? jQuery(options.titles).children() : false;
		if (titles) titles.hide().eq(0).show();
		slides.addClass('splash-banner-slide');
		slides.hide().eq(0).show();
		var slideWidth = slides.outerWidth(true);
		var slideHeight = slides.outerHeight(true);
		element.width(slideWidth);
		element.height(slideHeight);
		var timer;
		var animating = false;
		var currentIndex = 0;
		var goto = function(index){
			if (index == currentIndex) return;
			if (animating) return;
			animating = true;
			var currentSlide = slides.eq(currentIndex);
			var nextSlide = slides.eq(index);
			var start = (currentIndex < index)? slideWidth : -slideWidth;
			nextSlide.css({left: start}).show();
			currentSlide.animate({left: -start}, {duration: options.duration, complete: function(){
				currentSlide.hide();
			}});
			nextSlide.animate({left: 0}, {duration: options.duration, complete: function(){
				animating = false;
				currentIndex = index;
			}});
			if (options.titles){
				var currentTitle = titles.eq(currentIndex);
				var nextTitle = titles.eq(index);
				currentTitle.fadeOut(options.duration, function(){
					nextTitle.fadeIn(options.duration);
				});
			}
			if(options.controls) jQuery(options.controls).find('a.splash-banner-goto').removeClass('current').eq(index).addClass('current');
		};
		var next = function(event){
			if (event && event.preventDefault) event.preventDefault();
			var n = currentIndex + 1;
			if (n >= slides.length) n = 0;
			goto(n);
		};
		var prev = function(event){
			event.preventDefault();
			var n = currentIndex - 1;
			if (n < 0) n = slides.length - 1;
			goto(n);
		};
		if(options.controls){
			var controlsEl = jQuery(options.controls);
			var controls = jQuery('<ul/>');
			var control = jQuery('<li><a class="splash-banner-prev">Previous</a></li>');
			control.bind('click', prev);
			controls.append(control);
			slides.each(function(index){
				var control = jQuery('<li><a class="splash-banner-goto">' + (index + 1) + '</a></li>');
				control.bind('click', function(event){
					event.preventDefault();
					goto(index);
				});
				controls.append(control);
			});
			control = jQuery('<li><a class="splash-banner-next">Next</a></li>');
			control.bind('click', next);
			controls.append(control);
			controlsEl.append(controls);
			controlsEl.find('a.splash-banner-goto').eq(0).addClass('current');
		}
		var mouseIsOver = false;
		var pauseEls = (options.controls)? element.add(jQuery(options.controls)) : element;
		pauseEls.bind({
			'mouseenter': function(event){
				if (mouseIsOver) return;
				mouseIsOver = true;
				clearInterval(timer);
			},
			'mouseleave': function(event){
				if (!mouseIsOver) return;
				mouseIsOver = false;
				timer = setInterval(next, options.speed);
			}
		});
		timer = setInterval(next, options.speed);
	});
}
