function initFadeGallery(){
	$('.rotator').each(function(){
		// options
		var _fadeDur = 1000; // fade animation duration
		var _autoFade = 8000; // 8 sec autoslideshow
		
		// vars
		var _holder = $(this);
		var _prevLink = _holder.find('.prev-link');
		var _nextLink = _holder.find('.next-link');
		var _item = _holder.find('div.block');
		var _hovered = false;
		var _animated = false;
		
		// reset
		var _nextEl = _item.index(_item.filter('.active').eq(0));
		if(_nextEl==-1) _nextEl=0;
		var _currEl = 0;
		_item.eq(_nextEl).removeClass('active').css('z-index',2);
		
		// fade function
		function fadeAnimation(){
			_item.eq(_currEl).css('z-index',3);
			_item.eq(_nextEl).css('z-index',2);
			_item.eq(_currEl).animate({
				opacity:0
			},function(){
				_item.eq(_currEl).css({
					opacity:1,
					zIndex:1
				});
				_animated = false;
			});
		}
		
		// click 'next' button
		_nextLink.click(function(){
			if(!_animated) {
				_animated = true;
				_currEl = _nextEl;
				if(_nextEl<_item.length-1) _nextEl++
				else _nextEl=0;
				fadeAnimation();
			}
			return false;
		});
		
		// click 'prev' button
		_prevLink.click(function(){
			if(!_animated) {
				_animated = true;
				_currEl = _nextEl;
				if(_nextEl>0) _nextEl--
				else _nextEl=_item.length-1;
				fadeAnimation();
			}
			return false;
		});
		
		// auto slideshow
		setInterval(function(){
			if(!_hovered && !_animated) {
				_animated = true;
				_currEl = _nextEl;
				if(_nextEl<_item.length-1) _nextEl++
				else _nextEl=0;
				fadeAnimation();
			}
		},_autoFade);
		
		// pause on hover
		_holder.hover(function(){
			_hovered = true;
		},function(){
			_hovered = false;
		});
	});
}
$(function(){
	initFadeGallery();
});