/** 图片滚动 */
(function($){
	$.fn.yutJQRoll = function(options){
			var settings = {
					time: 3000,
					step: 3,
					num : 4,
					auto: 0
				};
			if(options) {
				$.extend(settings, options);
			}
		
		var time = settings.time,
			step = settings.step,
			num = settings.num,
			auto = settings.auto,
			stopPlay = false,  //停止播放
			hasPrev = true,
			hasNext = true;
			
		var prev = $(this).children(".prev"),
			next = $(this).children(".next"),
			box = $(this).children(".wrapper"),
			item = $(this).children(".wrapper").children("ul"),
			itemChild = $(this).children(".wrapper").children("ul").children("li"),
			itemChildWidth = itemChild.width()+8 + 0 + 0;
			
		setItemW();
		var fItemLeft = item.eq(0).offset().left;
		cloneItem();
		
			
		function setItemW(){
			var itemW = itemChildWidth * itemChild.length; 
			item.width(itemW);
			}
			
		function cloneItem(){
			item.clone().appendTo(box);
			}
			
		function onPrevClick(){
			item.animate({ 'left':'+='+left },time);
			hasPrev = false;
			setTimeout(function(){hasPrev = true},time+100);
			}
		function onNextClick(){
			item.animate({ 'left':'-='+left },time);
			hasNext = false;
			setTimeout(function(){hasNext = true},time+100);
			}
			
		function prevOne(){  //第一次点击
			item = box.children("ul");
			var pLeft = - item.width() + 'px';
			item.eq(1).prependTo(box);
			item = box.children("ul");
			item.eq(0).css({ 'left':pLeft });	
			left = itemChildWidth * step;
			}
			
		function nextOne(){//第一次点击
			item = box.children("ul");
			var pLeft = item.width() + 'px';
			item = box.children("ul");
			item.eq(1).css({ 'left':pLeft });
			left = itemChildWidth * step;
			}
		
		function firstToSecond(){  //当第二个ui到达第一个的初始位置时，第一与第二个交换
			item = box.children("ul");
			var newPosition = (box.children("ul").eq(1).offset().left + item.width() - fItemLeft)+'px';
			box.children("ul").eq(0).appendTo(box).css({ 'left':newPosition });
			}
			
		function secondToFirst(){  
			item = box.children("ul");
			var newPosition = (box.children("ul").eq(0).offset().left - item.width() - fItemLeft)+'px';
			box.children("ul").eq(1).prependTo(box).css({ 'left':newPosition });
			}
			
		function scrollPrev(){
			if(box.children("ul").eq(0).offset().left==fItemLeft && hasPrev){ //初始状态
				prevOne();
				onPrevClick();
				}else if(box.children("ul").eq(1).offset().left<=fItemLeft && hasPrev){
					onPrevClick();
					}else if(box.children("ul").eq(1).offset().left == (fItemLeft+num*itemChildWidth) && hasPrev){
						secondToFirst();
						onPrevClick();
						}else if(box.children("ul").eq(1).offset().left>fItemLeft && box.children("ul").eq(1).offset().left<(fItemLeft+num*itemChildWidth)){
							var shenxiags = (fItemLeft-box.children("ul").eq(0).offset().left)/itemChildWidth;
							var jiaohuan = box.children("ul").eq(1).offset().left+step*itemChildWidth - fItemLeft;
							if(shenxiags>=step){
								if(jiaohuan < num*itemChildWidth && hasPrev){
									onPrevClick();
									}else if(jiaohuan >= num*itemChildWidth && hasPrev){
										onPrevClick();
										setTimeout(function(){
											var newPosition = (box.children("ul").eq(0).offset().left - item.width() - fItemLeft) + 'px';
											box.children("ul").eq(1).prependTo(box).css({ 'left':newPosition });
											},(time+100));
										}
								}else if(shenxiags<step){
									if(jiaohuan >= num*itemChildWidth && hasPrev){
										var newPosition = (box.children("ul").eq(0).offset().left - item.width() - fItemLeft) + 'px';
										box.children("ul").eq(0).clone().prependTo(box).css({ 'left':newPosition });
										item = box.children("ul");
										onPrevClick();
										setTimeout(function(){
											box.children("ul").eq(2).remove();
											},time);
										}else if(jiaohuan < num*itemChildWidth && hasPrev){
											alert("此情况为非法操作，请联系程序员！");
											}
									}
								}
							}	
			
		function scrollNext(){
			if(box.children("ul").eq(0).offset().left==fItemLeft && hasNext){ //初始状态
				nextOne();
				onNextClick();
				}else if(box.children("ul").eq(1).offset().left>=(fItemLeft+num*itemChildWidth) && hasNext){
					onNextClick();
					}else if(box.children("ul").eq(1).offset().left==fItemLeft && hasNext){
						firstToSecond();
						onNextClick();
						}else if(box.children("ul").eq(1).offset().left>fItemLeft && box.children("ul").eq(0).offset().left<(fItemLeft+num*itemChildWidth)){
							var boxLastLeft = ((fItemLeft+num*itemChildWidth)-box.children("ul").eq(1).offset().left)/itemChildWidth;
							var shenxiags = itemChild.length - boxLastLeft;
							var jiaohuan = fItemLeft-box.children("ul").eq(0).offset().left+step*itemChildWidth;
							if(shenxiags>=step){
								if(jiaohuan<item.width() && hasNext){
									onNextClick();
									}else if(jiaohuan>=item.width() && hasNext){
										onNextClick();
										setTimeout(function(){
											var newPosition = (box.children("ul").eq(1).offset().left + item.width() - fItemLeft) + 'px';
											box.children("ul").eq(0).appendTo(box).css({ 'left':newPosition });
											},(time+100));
										}
									}else if(shenxiags<step){
										if(jiaohuan >= item.width() && hasNext){
											var newPosition = (box.children("ul").eq(1).offset().left + item.width() - fItemLeft) + 'px';
											box.children("ul").eq(0).clone().appendTo(box).css({ 'left':newPosition });
											item = box.children("ul");
											onNextClick();
											setTimeout(function(){
												box.children("ul").eq(0).remove();
												},time);
											}else if(jiaohuan < item.width() && hasNext){
												alert("此情况为非法操作，请联系程序员！");
												}
										}
								}
							}
		
		function autoPlay(){
			setInterval(function(){
				if(!stopPlay){
					scrollNext();
					}
				},auto);
			}
			
		prev.click(function(){ 
			scrollPrev();
			});
		next.click(function(){
			scrollNext();		
			});
			
		prev.mouseover(function(){
			stopPlay = true;
			});
		prev.mouseout(function(){
			stopPlay = false;
			});
		next.mouseover(function(){
			stopPlay = true;
			});
		next.mouseout(function(){
			stopPlay = false;
			});
			
		box.mouseover(function(){
			stopPlay = true;
			});
		box.mouseout(function(){
			stopPlay = false;
			});
		
		if(auto != null){	//自动滚动
			autoPlay();
			}
	}
	
})(jQuery);


		
