(function(){
	$.fn.jfade = function(o){
		var setting = {
			elems    : 'a',
			speed    : 450,        //渐变时间，默认值为450毫秒，只能用数值来表示
			ease     : 'swing',    //
		  //delay : 900,        //默认为2倍speed
			opacity  :  0,
			before   : function(){},
			after    : function(){}			
		};
		
		o = $.extend(setting, o);
		var o_max = o.opacity==0?1:0;
		return this.each(function(){
			var elem = $(this),
				elems = elem.find(o.elems),
				always = elems.filter('.always'),
				active = elems.filter('.active');			
			elems = elems.not('.always').not('.disabled').not('.active');
			//alert(elems.length);			
			//alert(always.length);	
			
			if(o.opacity!=0) elems.animate({opacity:0}, {duration:1});
			
			active.animate({opacity:o.opacity}, {duration:1});
			
			if(elems.length>0){
				elems.hover(function(){
					o.before;
					$(this).stop().animate({opacity:o.opacity, ease:o.ease}, {duration:o.speed});	
				}, function(){
					$(this).stop().animate({opacity:o_max, ease:o.ease}, {duration:o.speed, complete:o.after})
				});
			}
						
			if(always.length>0){		
				always.each(function(e){
					setInterval(function(){
						$(always[e]).animate({
							opacity:o_max, 
							ease:o.ease
						}, {
							duration: o.speed, complete:function(){
								$(always[e]).animate({
									opacity:o.opacity,
									ease:o.ease
								}, { duration:o.speed}
						);}});	
					}, 2*o.speed);
				});				
			}						  
		});
	};
		
})(jQuery);

