// scrolling gallery plugin
jQuery.fn.fadeGallery = function(_options){
	var _options = jQuery.extend({
		slides: '>img',
		switchTime:5000,
		duration:650
	},_options);

	return this.each(function(){
		// gallery options
		var _this = jQuery(this);
		var _slides = jQuery(_options.slides, _this);
		var _duration = _options.duration;
		var _switchTime = _options.switchTime;
		
		// gallery init
		if(!_slides.length) return;


		var _currentStep = 0;
		var _nextStep = 1;
		var _stepCount = _slides.length;
		var _timer;

		function autoSlide() {
			if(_timer) clearTimeout(_timer);
			_timer = setTimeout(nextSlide,_switchTime+_duration);
		}
		
		function nextSlide() {
			if (_currentStep == (_stepCount - 1)) {
				_nextStep = 0;
			} else {
				_nextStep = _currentStep+1;
			}
			$(_slides.get(_currentStep)).fadeOut(_duration);
			$(_slides.get(_nextStep)).fadeIn(_duration);
			_currentStep = _nextStep;
			autoSlide();
		}
		autoSlide();
	});
}

