!function($, undefined) {

/*
 * Rotator functionality
 *
 */
	var r = $('.rotator'),
			 imgs = r.children('img'),
			 rotator = {};

/*
 * Rotator init function
 *
 */
	rotator.init = function(interval) {
		imgs.each(function() {
					var img = $(this);
					img.attr('data-index', img.index());
					if ( img.index() != 0) { 
						img.hide();  
					} 
					else { 
						img.attr('data-active', true); 
					}						
			});		
		setInterval(function() {
			rotator.rotate();
		}, interval);
	};
	
/*
 * Rotate to next image
 *
 */
	rotator.rotate = function() {
			var current = r.find('img[data-active = true]').fadeOut('slow').removeAttr('data-active'),
					 nextIndex = (parseInt(current.attr('data-index')) + 1);
			if (nextIndex == imgs.size()) {
					nextIndex = 0;
			}
			var next = r.find('img[data-index = ' + nextIndex + ']');
			next.fadeIn('slow').attr('data-active', true);
	};
  

	/* Call init function to kick it off */
	rotator.init(10000);
	
}(jQuery);
