﻿(function($){
	$.fn.extend({
		Scroll:function(opt){
			//参数初始化
			if(!opt)	var opt={};
			var _btnUp=$("#"+opt.up);	//向上按钮
			var _btnDown=$("#"+opt.down);	//向下按钮
			var timerID;
			var _this=this.find("ul:first");
			var lineH=_this.find("li:first").height();	//获取行高
			var line=opt.line?parseInt(opt.line,10):parseInt(this.height()/lineH,10);
			var speed=opt.speed?parseInt(opt.speed,10):500; //滚动速度，数值越大，速度越慢
			var timer=opt.timer?parseInt(opt.timer,10):3000;  //滚动的时间间隔
			if(line==0)	line=1;
			//var upHeight=0-line*lineH;
			var upHeight=0-lineH;
			//滚动函数
			var scrollUp=function(){
				_btnUp.unbind("click",scrollUp);
				_this.animate({marginTop:upHeight
				},speed,function(){
							for(i=1;i<=line;i++)
							{
								_this.find("li:first").appandTo(_this);
							}
							_this.css({marginTop:0});
							_btnUp.bind("click",scrollUp);	//绑定向上按钮的点击事件
						});
			}
			//向下翻页函数
			var scrollDown=function(){
				_btnDown.unbind("click",scrollDown);
				for(i=1;i<=line;i++){
					_this.find("li:last").prependTo(_this);
				}
				_this.css({marginTop:upHeight});
				_this.animate({
					marginTop:0
				},speed);
			}
			//自动滚动
			var autoPlay=function(){
				if(timer)	timerID=window.setInterval(scrollDown,timer);
			};
			var autoStop=function(){
				if(timer)	window.clearInterval(timerID);
			};
			//鼠标事件绑定
			autoPlay();
			_this.hover(autoStop,autoPlay);
			_btnUp.css("cursor","pointer").click(scrollUp).hover(autoStop,autoPlay);	//向上向下鼠标事件绑定
			_btnDown.css("cursor","pointer").click(scrollDown).hover(autoStop,autoPlay);
	}
})
})(jQuery);
