jQuery.fn.extend({
	PicScroll: function(hv, speed, time) {
		this.each(function() { $(this).PicScrollInit(this.id, hv, speed, time); });
	},
	PicScrollInit: function(id, horv, speed, time) {
		var BtnR = $("#" + id + " #" + id + "R");
		var BtnL = $("#" + id + " #" + id + "L");
		this.ConPad = $("#" + id + " #" + id + "CP");
		this.Con = $("#" + id + " #" + id + "C");
		this.hv = horv;
		if (horv == "v") {
			this.Conitemw = this.Con.children(":first").height();
			this.AllWidth = Math.round(this.Con.children().length / Math.round(this.ConPad.width() / this.Conitemw - 0.5) + 0.4) * this.Conitemw;
		} else {
			var aw = 0;
			this.Con.children().each(function() { aw += $(this).width(); });
			this.AllWidth = aw;
			this.Conitemw = this.Con.children(":first").width();
		}
		this.s = null;
		this.Astep = speed ? speed : 50;
		this.Atime = time ? time : 30;
		this.ToRight = function(t) {
			if (t.s == null) {
				var ConPad = t.ConPad;
				//var sc = t.AllWidth - ConPad.width() - t.hide_w;
				//实际移动
				var sl;
				if (t.hv == "v") {
					//应该移动
					var a = Math.round(ConPad.height() / t.Conitemw + 0.4) * t.Conitemw;
					//剩下可以移动的
					var b = t.AllWidth - ConPad.height() - ConPad.scrollTop();
					var c;
					if (b < a) {
						sl = ConPad.scrollTop() + Math.round(b / t.Conitemw - 0.5) * t.Conitemw;
					}
					else {
						sl = ConPad.scrollTop() + a;
					}
					var sleft = ConPad.scrollTop();
					t.s = window.setInterval(function() {
						if (sl > sleft + t.Astep) {
							sleft += t.Astep
							ConPad.scrollTop(sleft);
						} else {
							ConPad.scrollTop(sl);
							clearInterval(t.s);
							t.s = null;
						}
					}, t.Atime);
				} else {
					//应该移动
					var a = Math.round(ConPad.width() / t.Conitemw + 0.4) * t.Conitemw;
					//剩下可以移动的
					var b = t.AllWidth - ConPad.width() - ConPad.scrollLeft();
					var c;
					if (b < a) {
						sl = ConPad.scrollLeft() + Math.round(b / t.Conitemw - 0.5) * t.Conitemw;
					}
					else {
						sl = ConPad.scrollLeft() + a;
					}
					var sleft = ConPad.scrollLeft();
					t.s = window.setInterval(function() {
						var sleft = ConPad.scrollLeft();
						if (sl > sleft + t.Astep) {
							sleft += t.Astep
							ConPad.scrollLeft(sleft);
						} else {
							ConPad.scrollLeft(sl);
							clearInterval(t.s);
							t.s = null;
						}
					}, t.Atime);
				}
			}
		};
		this.ToLeft = function(t) {
			if (t.s == null) {
				var ConPad = t.ConPad;
				var s = t.s;
				if (t.hv == "v") {
					var sl = ConPad.scrollTop() - Math.round(ConPad.height() / t.Conitemw + 0.4) * t.Conitemw;
					if (sl < 0)
						sl = 0;

					var sleft = ConPad.scrollTop();
					t.s = window.setInterval(function() {
						if (sl < sleft - t.Astep) {
							sleft -= t.Astep
							ConPad.scrollTop(sleft);
						} else {
							ConPad.scrollTop(sl);
							clearInterval(t.s);
							t.s = null;
						}
					}, t.Atime);
				} else {
					var sl = ConPad.scrollLeft() - Math.round(ConPad.width() / t.Conitemw + 0.4) * t.Conitemw;
					if (sl < 0)
						sl = 0;

					var sleft = ConPad.scrollLeft();
					t.s = window.setInterval(function() {
						if (sl < sleft - t.Astep) {
							sleft -= t.Astep;
							ConPad.scrollLeft(sleft);
						} else {
							ConPad.scrollLeft(sl);
							clearInterval(t.s);
							t.s = null;
						}
					}, t.Atime);
				}
			}
		};

		if (BtnR.length > 0) {
			BtnR.click(function() { var t = $(this).data("h"); t.ToRight(t); });
			BtnL.click(function() { var t = $(this).data("h"); t.ToLeft(t); });

			BtnR.data("h", this);
			BtnL.data("h", this);
		}
		return this;
	}
});