﻿TScrollerDEFAULT = {    	
	'mode': 'c_vertical',
	'speed': 5,
	'width':150,
	'height':100	
}
var TScrollerS = [];
function TScrollers(obj,SCROLL_CFG,ITEMS){
    this.n_id = TScrollerS.length;
	TScrollerS[this.n_id] = this;
	this.objID=obj;
	this.mode=SCROLL_CFG.mode?SCROLL_CFG.mode:TScrollerDEFAULT.mode;
    this.vertical = (this.mode=='c_vertical'||this.mode=='p_vertical');
    this.items = ITEMS;       
    this.w = SCROLL_CFG.width ? SCROLL_CFG.width : TScrollerDEFAULT.width;
    this.h = SCROLL_CFG.height ? SCROLL_CFG.height : TScrollerDEFAULT.height;
    this.n_wheight = this.vertical ? this.h : this.w;
    this.n_step = SCROLL_CFG.speed != 0 ? SCROLL_CFG.speed : 10;
	this.n_id = TScrollerS.length;
	TScrollerS[this.n_id] = this;	     
    this.getElem=function(div) {
	        return document.getElementById ? document.getElementById(div) : document.all[div]
    }  
    this.o_container = this.getElem(this.objID+"_contentDiv"); 
    this.o_container.style.width = this.w + 'px';
    this.o_container.style.height = this.h + 'px';
    if(this.mode!='c_vertical')
    {
        this.n_timeout=this.n_interval = this.n_cur = this.prior = 0;
        this.n_astep = Math.abs(this.n_step);        
        this.n_pos=this.n_wheight;   
        this.init  = TTScrollerInit;
	    this.sleep = TTScrollerSleep;
	    this.stop = TTScrollerStop;
	    this.move = TTScrollerMove;
	    this.roll = TTScrollerRoll;
	    this.move_s=TTScrollerMove_s;
	    this.supp=TTScrollerSupp;
        this.n_num = this.items.length; 
        if(this.vertical)
        {
          this.o_container.innerHTML='<div id="mn'+this.n_id+'" style="overflow:hidden;height:'+this.h+'px;width:'+this.w+'px;border:0px;padding:0px;">'  
                      +'<table cellpadding="0" cellspacing="0" border="0" width="100%">'
                      +'<tr><td id="ItemHead" height="'+this.h+'px"></td></tr>'
                      +'<tr><td class="ItemBody" id="mnc'+this.n_id+'"></td></tr>'
                      +'<tr><td id="ItemFoot" height="'+this.h+'px"></td></tr></table></div>';
        }
        else
        {
          this.o_container.innerHTML='<div id="mn'+this.n_id+'" style="overflow:hidden;height:'+this.h+'px;width:'+this.w+'px;border:0px;padding:0px;">'  
                      +'<table cellpadding="0" cellspacing="0" border="0" width="'+3*this.w+'">'
                      +'<tr><td id="ItemHead" width="'+this.w+'px"></td>'
                      +'<td class="ItemBody" id="mnc'+this.n_id+'" width="'+this.w+'px"></td>'
                      +'<td id="ItemFoot" width="'+this.w+'px"></td></tr></table></div>';       
        }
        this.main = this.getElem('mn'+this.n_id);
        this.o_cont = this.getElem('mnc'+this.n_id);    
	    this.ajust = this.vertical ? 
	    function () { this.main.scrollTop = this.n_pos;} :
	    function () { this.main.scrollLeft = this.n_pos;}  
        this.init();
    }
    else{
        if(this.n_step<10){this.n_step=10;}//最小值设置为10
        this.Marquee=TTScrollerMarquee;
        this.clone_container=this.getElem(this.objID+"_cloneDiv");
        this.clone_container.innerHTML=this.getElem(this.objID).innerHTML;                        
        this.MarqueeStop=TTScrollerMarqueeStop;
        this.MarqueeStart=TTScrollerMarqueeStart;    
        this.MarqueeStart();   
    } 
}
function TTScrollerInit(n_2measure) { 
    if(this.items&&this.items.length==1){        
	    this.o_container.innerHTML = this.items[0].content;
	    return;
	}   
	var b_fl = true, e_frm, k = 0, i;
	if (n_2measure != null)
	{
		this.items[n_2measure].height = this.vertical ? this.o_cont.offsetHeight : this.o_cont.offsetWidth;		
	}
	for (i = this.n_num - 1; i >= 0; i--) 
	    if (!this.items[i].height) {	        
		    if (this.items[i].content)	{		        
			    this.o_cont.innerHTML = this.items[i].content;
			    return setTimeout('TScrollerS[' + this.n_id + '].init(' + i + ')', 100);
		    }		
		b_fl = false;
 	}
	if (!b_fl) return setTimeout('TScrollerS[' + this.n_id + '].init()', 100)
	this.main.style.visibility='visible';
	this.move(0)
}
function TTScrollerMove(p) {   
    if(this.items&&this.items.length==1) return;   
	if (this.prior <= p && this.n_interval == 0) {	
		this.prior = 0;
		this.n_interval = setInterval('TScrollerS[' + this.n_id + '].roll()', 40); 
	}
} 
function TTScrollerStop(p) {
    if(this.items&&this.items.length==1) return;      
	if (this.prior < p) this.prior = p;
	if (this.n_timeout != 0) {	   
		clearTimeout(this.n_timeout);
		this.n_timeout = 0;
	}
	if (this.n_interval != 0) {
		clearInterval(this.n_interval);
		this.n_interval = 0;
	}
}
function TTScrollerSleep(delay) { 
	this.stop(0); 
	this.n_timeout=setTimeout('TScrollerS[' + this.n_id + '].move_s()', delay * 1000); 
}
function TTScrollerMove_s() {
	this.n_pos += this.n_step;
	this.ajust(); 
	this.move(0);
	}
	
function TTScrollerRoll() {	
	var item = this.items[this.n_cur];	
	if (this.n_pos >= this.n_wheight+item.height) {	
		item = this.items[this.n_cur = this.n_cur == 0 ? this.n_num - 1 : this.n_cur - 1];
		this.n_pos = 0;		
		this.supp(this.n_cur);		
	}	
	var delta = this.vertical?this.main.style.scrollTop-this.n_wheight:this.main.style.scrollLeft-this.n_wheight;	
	if (item.pause_a > 0 && this.n_pos >= item.height+this.n_wheight && this.n_pos < this.n_astep + item.height+this.n_wheight) {	   
		this.n_pos = delta+this.n_wheight;
    	this.ajust();
		return this.sleep(item.pause_a);
	}
	if (item.pause_b > 0 && this.n_pos >= this.n_wheight && this.n_pos < this.n_astep+this.n_wheight) {
		this.n_pos = this.n_wheight;
		this.ajust();
		return this.sleep(item.pause_b);
	}
	this.n_pos += this.n_step;		
	this.ajust(); 
}

function TTScrollerSupp(id) {    
	this.o_cont.innerHTML = this.items[id].content;
	this.ajust()
}

function TTScrollerMarquee()
{    
    if(this.getElem(this.objID).scrollHeight<this.h){
        if(this.clone_container.innerHTML!=""){
            this.clone_container.innerHTML="";
        }                      
    }
    else if(this.clone_container.offsetTop-this.o_container.scrollTop<=0)
    {
        this.o_container.scrollTop-=this.getElem(this.objID).offsetHeight;         
    }else{
       this.o_container.scrollTop++;        
    }
}
function TTScrollerMarqueeStart()
{
    this.n_Marquee=setInterval('TScrollerS[' + this.n_id + '].Marquee()', this.n_step);
}
function TTScrollerMarqueeStop()
{
    if (this.n_Marquee != 0) {
		clearInterval(this.n_Marquee);
		this.n_Marquee = 0;
	}
}

