;(function($) {

	$.fn.continuFade = function(o) {
		return this.each(function() {
			new $cf(this, 0);
		});
	};
	
	var defaults = {
		delay: 5000,
		animtime: 1200,
		mode: 'fade'
	};
		
	$.continuFade = function(e, o) {
	
		this.options	= $.extend({}, defaults, o || {});
	
		this.cf_o		= null;
		this.items		= null;
		this.timeout	= null;
		
		this.cf_o 		= $(e);
		this.items 		= this.cf_o.find('ul:first');
		
		this.init_items();
		this.cf_action(0);
		this.start_timeout();
	
	}
	
	var $cf = $.continuFade;
	
	$cf.fn = $cf.prototype = {		
		continuFade: '0.2.3'
	}
	
	$cf.fn.extend = $cf.extend = $.extend;
	
	$cf.fn.extend({
		init_items: function() {
			switch(this.options.mode) {
				case "fade":
					this.items.find('li').each(function(i) {
						$(this).css({
							position: 'absolute',
							top:      0,
							left:     0,
							display:  'none',
							opacity:  0
						});
					})
					this.items.find('li:first').css({
						opacity: 1
					});
					break;
				default:		
			}		
		},
		
		cf_action: function(index) {
			
			switch(this.options.mode) {
				case "fade":
					var current_index  = (this.items.find('li.selected').length > 0) ?  this.items.find('li').index(this.items.find('li.selected')) : this.items.find('li').length - 1;
					var move_to_index  = index;

					this.items.find('li:eq('+ move_to_index  +')').show().css({ zIndex: index + 1 }).animate({ opacity: 0.999 }, this.options.animtime,  'linear');				
					this.items.find('li:eq('+ current_index  +')').css({ zIndex: index }).animate({ opacity: 0 }, this.options.animtime, 'linear', function() { $(this).hide(); });				

					break;
				default:
			}

			this.items.find('li').removeClass('selected');
			this.items.find('li:eq('+ index +')').addClass('selected');	
			
		},
		
		start_timeout: function() {
			var self = this;
			
			this.timeout = setInterval(function() {
				var current_index = self.items.find('li').index(self.items.find('li.selected'));
				var item_count    = self.items.find('li').length;
				var new_index;

				if (current_index < item_count-1) {
					new_index = current_index+1;
				} else {
					new_index = 0;
				}
				self.cf_action(new_index);

			}, this.options.delay);
						
		}
		
		
	});	
	
	$cf.extend({
		defaults: function(d) {
			return $.extend(defaults, d || {});			
		}
		
	});
	

})(jQuery);
