(function($){
	$.fn.LoadImage=function(removeDOM){ 
		var ok=0;
		var loadLength = $(this).length;
		
		return this.each(function(){ 
			var src=$(this).attr("src");
			var img=new Image();
			$(img).load(function(){ 
				ok++;
				if(ok==loadLength){
					removeDOM.remove(); 
					return $("#imgad").ADSlide({
						speed : "normal",
						show : 3,
						timer : 2500,
						leftspeed : 300,
						space: 80
					});
				}
			});
			img.src=src; 
		
		}); 
	}



	$.fn.ADSlide=function(settings){
		settings = jQuery.extend({
        	speed : "normal",
			show : 4,
			timer : 2000,
			leftspeed : 300,
			space:60
    	}, settings);
		return this.each(function() {
			$.fn.ADSlide.scllor( $(this), settings );
    	});
	};
	
	$.fn.ADSlide.scllor=function($this,settings){
		var index=0;
		$("<div id='flow'></div>").appendTo($this);
		$("<div id='up'></div>").appendTo($this);
		$("<div id='down'></div>").appendTo($this);
		var li=$(".flash_item li");
		var up=$("#up");
		var down=$("#down");
		var showImg=$(".flash_img li");
		$(".flash_img").css({"visibility":"visible"});
		li.css("visibility","visible");
		li.slice(settings.show).css("display","none");
		up.fadeTo(1,0);
		down.fadeTo(1,0);
		$("#flow").fadeTo(1,1);
		liLength=li.length;
		
		var intervalTime= setInterval(function(){
			ShowAD(index);
			index++;
			if(index >= liLength){
				index=0;
			}
		}, settings.timer);
		
		li.hover(function(){
			if(intervalTime){
				clearInterval(intervalTime);
			}
			index=li.index(this);
			intervalTime=setTimeout(function(){
				$(".flash_img li").stop();
				ShowAD(index);
			}, 200);

		},function(){
			clearInterval(intervalTime);
			intervalTime=setInterval(function(){
				ShowAD(index);
				index++;
				if (index >= liLength) {
					index = 0;
				}
			}, settings.timer);
		});
		showImg.hover(function(){
			if(intervalTime){
				clearInterval(intervalTime);
			}
		},function(){
			clearInterval(intervalTime);
			intervalTime=setInterval(function(){
				ShowAD(index);
				index++;
				if (index >= liLength) {
					index = 0;
				}
			}, settings.timer);
		});
		
		up.mouseover(function(){
		if(index-index%settings.show-settings.show>=0){
				$(this).fadeTo(700,0.5);
				if(intervalTime){
					clearInterval(intervalTime);
				}
			}
		}).click(function(){
			if(intervalTime){
				clearInterval(intervalTime);
			}
			if(index-index%settings.show-settings.show>=0){
				var from = index-index%settings.show-settings.show;
				var to = index-index%settings.show-1;
				index = from;
				ShowAD(index);
			}
		}).mouseout(function(){
			$(this).fadeTo(700,0);				  
			clearInterval(intervalTime);
			intervalTime=setInterval(function(){
				ShowAD(index);
				index++;
				if (index >= liLength) {
					index = 0;
				}
			}, settings.timer);
		});

		down.mouseover(function(){
		if(index-index%settings.show+settings.show<liLength){
		
				$(this).fadeTo(700,0.5);	
				if(intervalTime){
					clearInterval(intervalTime);
				}
			}
		}).click(function(){
			if(intervalTime){
				clearInterval(intervalTime);
			}
			if(index-index%settings.show+settings.show<liLength){
				var from = index-index%settings.show+settings.show;
				var to = index-index%settings.show+settings.show*2-1;
				index = from;
				ShowAD(index);
			}
		}).mouseout(function(){
			$(this).fadeTo(700,0);				  
			clearInterval(intervalTime);
			intervalTime=setInterval(function(){
				ShowAD(index);
				index++;
				if (index >= liLength) {
					index = 0;
				}
			}, settings.timer);
		});

		var ShowAD=function(i){
			
			showImg.eq(i).animate({opacity:1},settings.speed).css({"z-index": "1"}).siblings().animate({opacity: 0},settings.speed).css({"z-index":"0"});
			var iLocation = i%settings.show;
			if(i%settings.show==0){
				var from = i;
				var to = i+settings.show;
				li.css("display","none");
				//for(m=from;m<=to;m++){
				//	li.eq(m).css("display","block");
				//}
				li.slice(from,to).css("display","block");
			}else{
				var from = i-i%settings.show;
				var to = from + settings.show;
				li.css("display","none");
				//for(m=from;m<=to;m++){
				//	li.eq(m).css("display","block");
				//}
				li.slice(from,to).css("display","block");

			}
			return $("#flow").animate({top:iLocation*settings.space+"px"});
		};
	};
	
	
	

	
})(jQuery);


