/************************************************************
 ** Clears a field
 ** By: 	Joshua Sowin (fireandknowledge.org)
 ** HTML: <input type="text" value="Search" name="search"
 **			id="search" size="25" 
 ** 		onFocus="clearInput('search', 'Search')" 
 ** 		onBlur="clearInput('search', 'Search')" />
 ***********************************************************/
function clearInput(field_id, term_to_clear) {
	
	// Clear input if it matches default value
	if (document.getElementById(field_id).value == term_to_clear ) {
		document.getElementById(field_id).value = '';
	}
	
	// If the value is blank, then put back term
	else if (document.getElementById(field_id).value == '' ) {
		document.getElementById(field_id).value = term_to_clear;
	}
} // end clearInput()

(function($){
	$.fn.extend({
		Scroll:function(opt,callback){
			//initialize data
			if(!opt) var opt={};
			var _btnUp = $("#"+opt.up);//锟斤拷锟较帮拷钮
			var _btnDown = $("#"+opt.down);//锟斤拷锟铰帮拷钮
			var _btnLeft = $("#"+opt.left);//锟斤拷锟斤拷钮
			var _btnRight = $("#"+opt.right);//锟斤拷锟揭帮拷钮

			var timerID;
			var _this=this.eq(0).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; //锟斤拷锟斤拷锟斤拷时锟斤拷锟斤拷锟斤拷锟斤拷锟诫）

			var lineW = _this.find("li:first").width();  //锟斤拷取item锟斤拷锟�
			var column = opt.line?parseInt(opt.line, 10): parseInt(this.width()/lineW, 10); //每锟轿癸拷锟斤拷锟斤拷item锟斤拷锟斤拷默锟斤拷为一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�

			if(line==0) line=1;
			var upHeight=0-line*lineH;
			if (column==0) column=1;
			var leftWidth=0-column*lineW;

			// 锟斤拷锟较癸拷锟斤拷锟斤拷锟斤拷
			var scrollUp=function(){
				_btnUp.unbind("click",scrollUp); //取锟斤拷锟斤拷锟较帮拷钮锟侥猴拷锟斤拷锟�
				_this.animate(
					{marginTop:upHeight},
					speed,
					function(){
						for(i=1;i<=line;i++){
							_this.find("li:first").appendTo(_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").show().prependTo(_this);
				}
				_this.css({marginTop:upHeight});
				_this.animate(
					{marginTop:0},
					speed,
					function(){
						_btnDown.bind("click",scrollDown);
					}
				);
			}

			// 锟斤拷锟斤拷页

			var scrollLeft=function() {
				_btnLeft.unbind("click", scrollLeft);//取锟斤拷锟斤拷锟斤拷疃�
				_this.animate(
					{marginLeft:leftWidth},
					speed,
					function(){
						for(i=1;i<=column;i++) {
							_this.find("li:first").appendTo(_this);
						}
						_this.css({marginLeft:0});
						_btnLeft.bind("click", scrollLeft); //锟筋定锟斤拷钮锟侥碉拷锟斤拷录锟�
					}
				);
			}
			// 锟斤拷锟揭凤拷页

			var scrollRight=function() {
				_btnRight.unbind("click", scrollRight);//取锟斤拷疃拷锟斤拷锟�
				for(i=1;i<=column;i++) {
					_this.find("li:last").show().prependTo(_this);
				}
				_this.css({marginLeft:leftWidth});
				_this.animate(
					{marginLeft:0},
					speed,
					function() {
						_btnRight.bind("click", scrollRight);
					}
				);
			}
		   //锟皆讹拷锟斤拷锟斤拷
			var autoPlay = function(){
				if(timer)timerID = window.setInterval(scrollUp,timer);
			};
			var autoStop = function(){
				if(timer)window.clearInterval(timerID);
			};

			$("body").load(autoPlay);
			 //锟斤拷锟斤拷录锟斤拷锟�
			_this.hover(autoStop,autoPlay).mouseout();
			_btnUp.css("cursor","pointer").click( scrollUp ).hover(autoStop,autoPlay);//锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷录锟斤拷锟�
			_btnDown.css("cursor","pointer").click( scrollDown ).hover(autoStop,autoPlay);

			_btnLeft.css("cursor","pointer").click( scrollLeft).hover(autoStop,autoPlay);
			_btnRight.css("cursor","pointer").click( scrollRight).hover(autoStop,autoPlay);
		}
	})
	})(jQuery);

