(function($){
	var _intervalTimer, $current, $next, $settings, $slideshow;
	
	$.fn.slideshow = function(settings){

		var _slideshow = this;
		
		this.each(function(i){
			var $parent = $(this);

			this.playId   = null;
			this.playFlag = false;
			this.length   = 0;
			this.inited   = new Array();
			this.titles   = new Array();
			
			this.build = function(){
				
				var _self = this;
				$parent.addClass('slideshow-content');
				$parent.wrap('<div class="slideshow-wrapper"></div>');
				$parent = $('.slideshow-wrapper');
				
				this.length = $parent.find('.slideshow-content > *').length;
				$parent.find('.slideshow-content > *').each(function(){
					$(this).addClass('slideshow-slide');
				});
				
				this.events();

				// init slide (replace by ajax etc)
				//this.init(this.options.index);

				// show slide
				$parent.find('.slideshow-slide:eq('+this.options.index+')').show();
				
				return true;
			};

			/**
			 * Init N-slide
			 * @method
			 * @param {Integer} index
			 */
			this.init = function (index) {
				// initialize only ones
				for (var i = 0, loopCnt = this.inited.length; i < loopCnt; i++) {
					if (this.inited[i] === index) {
						return true;
					}
				}

				// index to inited stack
				this.inited.push(index);

				// current slide
				slide = $parent.find('.slideshow-slide:eq('+index+')');

				return false;
			};
			
			this.play = function(){
				var _self = this;
				this.playFlag = true;
				this.playId = setTimeout(function(){ _self.next() }, this.options.speed);
			};
			
			this._play = function () {  
				var _self = this;
				
				// if it last frame
				if (this.options.index == (this.length-1) ) {
					this.stop();
					// should be restart slideshow
					if ( this.options.loop ) {
						this.play();
					}
					return false;
				}
				this.playId = setTimeout(function(){ _self.next(); }, this.options.speed);
				return true;
			};
			
			this.stop = function () {
				this.playFlag = false;
				
				clearTimeout(this.playId);
				this.playId = null;
			};
			
			this.events = function() {
				var _self = this;
				
				$parent.hover(function(){
					if (_self.playId) {
						_self.stop();
					}
				}, function(){
					if (!_self.playId) {
						_self.play();
					}
				});
				
				$parent.find('.slideshow-content > *').each(function(){
					$(this).hover(function(){
						if (_self.playId) {
							_self.stop();
						}
					}, function(){
						if (!_self.playId) {
							_self.play();
						}
					});
				});
			};
			
			this.next = function(){
				if (this.options.index == (this.length-1)) {
					i = 0;
				} else {
					i = this.options.index + 1;
				}            
				this.goToSlide(i);
			};

			/**        
			 * Goto N-slide
			 * @method
			 * @param {Integer} n
			 */
			this.goToSlide = function (n) {
				if (this.options.index == n) return;

				//if (!this.init(n)) return;

				var next = $parent.find('.slideshow-content > *:eq('+n+')');
				var prev = $parent.find('.slideshow-content > *:eq('+this.options.index+')');
				
				// restore next slide after all effects, set z-index = 0 for prev slide
				prev.css({zIndex:0});
				next.css({zIndex:1, top: 0, left: 0, opacity: 1});
				
				this.options.index = n;								
				
				prev.css({zIndex:0, opacity: 1});
				next.css({zIndex:1, opacity: 0});
				
				prevAni = {opacity: 0};
				
				var _self = this;
				
				prev.animate(prevAni,this.options.effecttime);
				
				// play next slide animation, hide prev slide, update label, update counter
				next.show().animate({top: 0, left: 0,opacity: 1}, this.options.effecttime, function () { 
						prev.hide();
						_self._play();
					});
			};
			
			
			this.options = $.extend({}, $.fn.slideshow.options, settings);
			
			this.build();
		});
		
		_slideshow.playSlide = function(){ _slideshow.each(function () { this.play();  }) };
		
		return this;
	};
	
	$.fn.slideshow.options = {
		effecttime: 1000,
		index: 0,
		loop: true,
		play: true,
		speed: 2500
	};
	
})(jQuery);
