/*
	jQuery cycle v2.05
	
	Wouter Beeftink
	wouter@footsteps.nl
*/
(function($) {
		  
	$.fn.cycle = function(settings) {
		
		var settings = $.extend({
			
			start : 0,
			startShown : true,
			rotate : true,
			speed : 1000,
			interval : 5000,
			keep : false
			
		}, settings);
		
		function callback(type, object, arguments) {
			
			var callbackFunction = settings[type];
			
			if($.isFunction(callbackFunction))
				callbackFunction.call(object, arguments);
			
		};
		
		this.showIndex = function(index, startShown) {
			
			callback('beforeCycle', this);
			
			if(currentIndex != index) {
			
				this.hide();
				
				if(startShown) {
					
					this.eq(settings.start).show();
					
				} else {
						
					this.stop();
					
					if(currentIndex >= 0) {
						
						elements.eq(currentIndex).css({
							'display' : 'block',
							'z-index' : 1,
							'opacity' : 1
						});
	
					};
					
					elements.eq(index)
						.css({
							'display' : 'block',
							'z-index' : 2,
							'opacity' : 0
						})
						.animate({
							'opacity' : 1
						}, settings.speed);
					
				};
				
				currentIndex = index;
						
				if(timer) {
					this.stopCycle();
					this.startCycle();
				};
				
				callback('onCycle', this);
				
			};
			
		};
		
		this.showPrevious = function() {
			this.showIndex(this.getPreviousIndex());
		};
		
		this.showNext = function() {
			this.showIndex(this.getNextIndex());
		};
		
		this.startCycle = function() {
			
			callback('beforeStart', this);
			
			if(!this.isAnimated()) {
				
				timer = window.setInterval(function() {
					elements.showNext();
				}, settings.interval);
				
				callback('onStart', this);
				
			};
			
			stopped = false;
			
		};
		
		this.pauseCycle = function() {
			
			callback('beforePause', this);
			
			if(this.isAnimated()) {

				window.clearInterval(timer);
				timer = null;
				
				callback('onPause', this);
				
			};
			
			stopped = false;
			
		};
		
		this.stopCycle = function() {
			
			callback('beforeStop', this);
			
			if(this.isAnimated()) {

				window.clearInterval(timer);
				timer = null;
				
				callback('onStop', this);
				
			};
			
			stopped = true;
			
		};
		
		this.isAnimated = function() {
			return timer ? true : false;
		};
		
		this.getCurrentIndex = function() {
			return currentIndex;
		};
		
		this.getPreviousIndex = function() {
			return currentIndex == 0 ? amount - 1 : currentIndex - 1;
		};
		
		this.getNextIndex = function() {
			return currentIndex == amount - 1 ? 0 : currentIndex + 1;
		};
		
		this.getAmount = function() {
			return amount;
		};
		
		var elements = this,
			amount = this.length;
			
		if(amount > 1) {
			
			callback('beforeLoad');
			
			var stopped = settings.rotate,
				currentIndex = null,
				timer = null;
			
			this.css('position', 'absolute');
			this.showIndex(settings.start, settings.startShown);
			
			if(stopped)
				this.startCycle();
				
			if(settings.keep) {
				
				this.hover(function() {
									
					elements.pauseCycle();
					
				}, function() {

					if(!stopped)
						elements.startCycle();
						
				});
				
			};
			
			callback('onLoad');
			
		};
		
		return this;
		
	};
		  
})(jQuery);
