/**
 * 改写基于jQuery
 * @param tableName 表格id
 * @param spaceoftime 滚动的间隔时间
 * @param speed 滚动的速度
 * @param period 停顿的间隔距离
 * @param times 停顿的周期（以滚动的时间间隔为单位）
 * @usage: var ss = new hscroll(string tableName[,int spaceoftime][,int speed][,int period][,times]);
 *     ss.tart();
 *     ss.pause();
 *
 * 	var hscrollad = new hscroll('#hscrollAd',60,1);
 * 	hscrollad.start();
 * 	$('#hscrollAd').hover(
 * 		function(){
 * 			hscrollad.pause();
 * 		},
 * 		function(){
 * 			hscrollad.start();
 * 		}
 * 	);
 */
function hscroll (tableName,spaceoftime,speed,period,times) 
{
	this.spaceoftime = spaceoftime? spaceoftime: 100;
    this.speed = speed? speed: 1;
    this.period = period;
    this.times = times?times:5;
    this.count = 0;
    this.serial = hscroll.instances.length;
    hscroll.instances[this.serial] = this;
    this.interval = null;
    this.m = 0;
    this.table = $(tableName);
	this.tableParent = this.table.parent(); 
    this.originalWidth = this.table.width();

    var tr;
    this.tableParent.css('overflow','hidden');
    tr = this.table.find('tr');
    while(this.originalWidth!=0 && this.table.width()<=this.tableParent.width()){
        tds = tr.children();
        tds.each(function(){
           tr.append($(this).clone());
        });
    }
    tds = tr.children();
    tds.each(function(){
       tr.append($(this).clone());
    });
}

hscroll.instances = new Array();

hscroll.prototype.scroll = function () {
	var scrollLeft = this.tableParent.scrollLeft();
    if (this.period && scrollLeft%this.period==0) {
        if (this.count == this.times) { 
            this.count = 0;
        } else {
            this.count++;
            return true;
        }
    }
    if (scrollLeft>=this.originalWidth) {
        this.tableParent[0].scrollLeft = scrollLeft-this.originalWidth+this.speed;
    } else {
        this.tableParent[0].scrollLeft = scrollLeft+this.speed;
    }
}

hscroll.prototype.start = function () {
    if (this.originalWidth!=0){
        this.interval = setInterval("hscroll.instances["+this.serial+"].scroll()",this.spaceoftime);
	}
}

hscroll.prototype.pause = function () {
	if (this.interval)
    	clearInterval(this.interval);
}
