(function($){
	
		var Slider = function(){

			this.time=4000;
			this.slides=null;
			this.timer=null;
			this.index=0;
			this.disabled=false;
			
			this.init = function(slides,time)
			{
				if(time)this.time=time;
				this.slides=slides.children();
				var that=this;
				this.timer = window.setTimeout(function(){that.next();},this.time)
			}
			
			this.step = function(i)
			{
				if(!i)i=1;
				window.clearTimeout(this.timer);
			
				if(this.index>=this.slides.length)this.index=0;
				if(this.index<0)this.index=this.slides.length-1;
				var nextIndex=this.index+i;
				if(this.index+i>=this.slides.length)nextIndex=0;
				if(this.index+i<0)nextIndex=this.slides.length-1;
				
				var exit = $(this.slides[this.index]);
				var enter = $(this.slides[nextIndex]);
				
				enter.css('z-index',parseInt(exit.css('z-index'))+1);			
				
		//	$('.slides').removeClass('static');
				this.disabled=true;
				enter.addClass('on',1200,'easeOutQuad',function(){that.disabled=false});
				
				exit.delay(400).removeClass('on',600,'easeInQuad');
				

				var that=this;
				$('.spinner').animate({rotate:'+=180deg'},2000,'easeOutQuart');
				
				$('.slide_desc:hidden').html(enter.find('img').attr('alt'));
				$('.slide_desc:hidden').delay(400).fadeIn(400);
				$('.slide_desc:visible').delay(400).fadeOut(300);
				
				
				
				this.index+=i;
				var that=this;		
				this.timer = window.setTimeout(function(){that.next();},this.time)
			}
			
			this.next = function()
			{
				this.step(1);
			}
			
			this.prev = function()
			{
				this.step(-1);
			}
			
			this.nextClick = function()
			{
				if(this.disabled)return;
				var oldTime = this.time;
				this.time = 16000;	// If next clicked, don't auto-next for another 16 seconds
				this.next();
				this.time=oldTime;
			}
			
			this.prevClick = function()
			{
				if(this.disabled)return;
				var oldTime = this.time;
				this.time = 16000;	// If next clicked, don't auto-next for another 16 seconds
				this.prev();
				this.time=oldTime;
			}

		}
	
	$(window).load(function(){
		var s = new Slider();
		s.init($('.slides'),8000);
		$('.spinner').click(function(){s.nextClick()});
	});
	
})(jQuery);
