
(function($) {
	
	$.fn.fadeContent = function(options) {
		var settings = {
			'interval'   : 4000,
			'speed'      : 2000,
			'fader_item' : '.item'
		};
		
		return this.each(function() {
			if (options) {
				$.extend(settings, options);
			}
			
			var inst     = this;
			this.curr_id = '';
			this.fading  = false;
			
			this.fade = function() {
				
				// Fetch current visible item
				if (inst.curr_id == '') {
					inst.curr_id = $(this).find(settings.fader_item + ':visible').attr('id');
				}
				
				// Fetch next item
				var next_div = $('#' + inst.curr_id + ' ~ ' + settings.fader_item + ':first');
				
				// If next item doesn't exist, fetch the first item
				if (next_div.length == 0) {
					next_div = $(this).find(settings.fader_item + ':first');
				}
			
				// If next item and current item are the same, don't fade
				if (inst.curr_id != next_div.attr('id')) {
					if (inst.fading == 0) {
						inst.fading = 1;
			
						// Switch the z-index
						$('#' + inst.curr_id).css('z-index', '10');
						next_div.css('z-index', '11');
						
						// And fade in the next item
						next_div.fadeIn(settings.speed, function() {
							
							// Done fading? Hide the previous item
							$('#' + inst.curr_id).hide();
							inst.curr_id = next_div.attr('id');
							inst.fading = 0;
						});
					}
				}
			}
			
			var fadeInterval = setInterval(function() { 
				inst.fade();
			}, settings.interval);
		});
	}
})(jQuery);

