(function(window,document,undefined){
	var Slideshow = function(args){
		var that = this;
		
		that.settings = {
			container: '#slideshow',
			slide: '.slide',
			nav: '#nav',
			duration: 5000,
			prevAndNext: true,
			prevText: 'Previous',
			nextText: 'Next',
			separator: '',
			autoPlay: true
		};
		for( var i in args ) {
			that.settings[i] = args[i];
		}
		
		that.init = function(args){
			$(that.settings.container + ' ' + that.settings.slide)
				.css({
					opacity: 0,
					position: 'absolute',
					top: 0,
					left: 0,
					'z-index': 8
				});
			
			if( $(that.settings.container + ' ' + that.settings.slide + '.active').length == 0 )
				$(that.settings.container + ' ' + that.settings.slide + ':first')
					.addClass('active');
			
			$(that.settings.container + ' ' + that.settings.slide + '.active')
				.css({
					'z-index': 10,
					opacity: 1
				});
				
			that.createNav();
			that.play();
			
			$(that.settings.container + ' ' + that.settings.nav + ' .item').live('click',function(e){
				e.preventDefault();
				that.navigate( $(this).index(that.settings.container + ' ' + that.settings.nav + ' .item') );
			});
			$(that.settings.container + ' ' + that.settings.nav + ' .prev').live('click',function(e){
				e.preventDefault();
				that.navigate(-1);
			});
			$(that.settings.container + ' ' + that.settings.nav + ' .next').live('click',function(e){
				e.preventDefault();
				that.navigate();
			});
			$(that.settings.container).hover(function(){
					that.pause();
				},
				function(){
					that.play();
				});
			
		}
		
		that.createNav = function(){
			var s = that.settings,
				n = $(s.container + ' ' + s.slide).length,
				nav = '';
			
			if( n > 1 ) {
				nav = '<div id="' + s.nav.substring(1) + '">';
				if( s.prevAndNext === true )
					nav += '<a href="#" class="prev">' + s.prevText + '</a>';
				for( var i = 0; i < n; i++ ) {
					nav += (i == 0 ? '' : s.separator) + '<a href="#" class="item">' + i + '</a>';
				}
				if( s.prevAndNext === true )
					nav += '<a href="#" class="next">' + s.nextText + '</a>';
				nav += '</div>';
			}
			
			var $nav = $(nav);
			$(s.container).append($nav);
			$(s.container + ' ' + s.nav + ' ' + '.item')
				.eq($(s.container + ' ' + s.slide + '.active')
				.index(s.container + ' ' + s.slide))
				.addClass('active');
			
			$nav.css({
					position: 'absolute',
					'z-index': 11
				});
		};
		that.navigate = function(i){
			that.pause()
			that.swapSlide(i);
			that.play(); 
		};
		that.swapSlide = function( slideTo ){
			var $active = $(that.settings.container + ' ' + that.settings.slide + '.active');
			
			var $next =  $active.next(that.settings.slide).length ? 
				$active.next(that.settings.slide) : 
				$(that.settings.container + ' ' + that.settings.slide + ':first');
			
			// Navigation by buttons
			if( slideTo !== undefined && slideTo >= 0 )
				$next = $(that.settings.container + ' ' + that.settings.slide).eq(slideTo);
			// Prev-button
			else if( slideTo !== undefined && slideTo < 0 )
				$next = $active.prev(that.settings.slide).length ? 
					$active.prev(that.settings.slide) : 
					$(that.settings.container + ' ' + that.settings.slide + ':last');
			
			$active
				.removeClass('active')
				.css({
					'z-index': 9
				});

			$next.css({opacity: 0.0})
				.addClass('active')
				.css({
					'z-index': 10,
					opacity: 0
				})
				.animate({opacity: 1.0}, 1000, function() {
					$(that.settings.container + ' ' + that.settings.nav + ' ' + '.item.active')
						.removeClass('active');
					$(that.settings.container + ' ' + that.settings.nav + ' ' + '.item').eq($(this).index(that.settings.container + ' ' + that.settings.slide))
						.addClass('active');
					$active.css({
						'z-index': 8
					});
				});
			
			// Support the jQuery.resize eventhandler
			$(that.settings.container).resize();
		};
		that.pause = function() {
			clearInterval(that.playSlideshow);
			that.playSlideshow = undefined;
		};
		that.play = function(){
			if( that.playSlideshow === undefined && that.settings.autoPlay == true ) {
				that.playSlideshow = setInterval( function(){
						that.swapSlide();
					}, that.settings.duration );
			}
		}
		
		that.init(args);
	}
	window.Slideshow = Slideshow;
})(window,document);
