﻿

function Marquee()
{
    this.ID = document.getElementById(arguments[0]);//目标层名称（需要滚动部分的层名称）
	if(!this.ID)
	{
		alert('您要设置的"' + arguments[0] +'"初始化错误\r\n请检查标签ID设置是否正确!');
		this.ID = -1;
		return;
	}	
	this.Speed=this.Space=this.PageWidth=this.Height=this.PageWidth=this.Time=0;
	this.ObjectName =this.DivName=this.ClickName="";
	if(typeof arguments[1] == "string")this.DivName = arguments[1];//滚动层主名称（NEW时必给）
	if(typeof arguments[2] == "string")this.ClickName = arguments[2];//按钮层名称（可选，为空或名称不对就看不到那4个点）
	
	if(typeof arguments[3] == "number")this.Speed = arguments[3];//间歇时间（每多少秒调用一次）
	if(typeof arguments[4] == "number")this.Space = arguments[4];//位移幅度（步长）一次移动多少像素
	if(typeof arguments[5] == "number")this.Width = arguments[5];//NEW出的窗题宽  （暂时没有用到，宽高外部不可调，预留功能）
	if(typeof arguments[6] == "number")this.Height = arguments[6];//NEW 出的窗题高 （暂时没有用到，宽高外部不可调，预留功能）
	if(typeof arguments[7] == "number")this.PageWidth = arguments[7];//一次翻多少像素（一屏大小）
	if(typeof arguments[8] == "number")this.Time = arguments[7];//自动滚动时等待时间（多久自动翻一次）
	if(typeof arguments[9] == "string")this.ObjectName = arguments[9];	//本类的实例对象名 如：var mq=new Marquee("List1","ISL_Cont","dotList"); 中的mq
	//if(typeof arguments[10] == "number")this.Time = arguments[10];//自动滚动时等待时间（多久自动翻一次）


    //this.ISL_Pages = parseInt(this.ID.scrollWidth / this.PageWidth);//获取页面数量（需要滚动的对象有几屏）
    this.fill = 0; //整体移位
    this.MoveLock = false;
    this.MoveTimeObj=null;
    this.Comp = 0;
    this.AutoPlayObj=null;   
    this.ISL_Page = 1;
    this.GotoLock = false;
    //定义一个层字符串，会把要滚动的目标内容读进来
    var templateLeft='<div class="navpic" id="'+ this.DivName+'"><div style="width:50000px;"><div class="wid" style="float:left;">MSCLASS_TEMP_HTML</div>'
                    + '<div class="wid" style="float:left;">MSCLASS_TEMP_HTML</div></div></div>';
    
    var msobj = this;
	msobj.tempHTML = msobj.ID.innerHTML;	
	msobj.ID.innerHTML = templateLeft.replace(/MSCLASS_TEMP_HTML/g,msobj.ID.innerHTML);//读取目标文本并加载到页面中
	msobj.scrollLeft = this.fill>=0?this.fill:this.ID.scrollWidth - Math.abs(this.fill);//设置滚动条起始位置
    //alert(GetObj(this.DivName));    
}

Marquee.prototype.Start=function()
{    
    this.ISL_Pages = parseInt(this.ID.scrollWidth / this.PageWidth);//获取页面数量（需要滚动的对象有几屏）
    GetObj(this.DivName).onmouseover = function(){clearInterval(this.AutoPlayObj);}//定义层中按钮点击事件
    GetObj(this.DivName).onmouseout = function(){} //定义层中按钮点击事件
    this.AutoPlay();//开始滚动
    this.ISL_PageList();//圆点列表     
}

function GetObj(objName){ return document.getElementById(objName); }//查找并返回对象

Marquee.prototype.begin = function()//向左滚动（默认调用）
{
    this.ISL_GoDown();this.ISL_StopDown();
}

Marquee.prototype.AutoPlay=function(){ //自动滚动
	clearInterval(this.AutoPlayObj);	
	this.AutoPlayObj = setInterval(this.ObjectName+'.begin();',this.Time); //间隔时间
}

Marquee.prototype.ISL_PageList=function(){//生成圆点层内容 如果没有给圆点层的名称则不会出现圆点
	var i,temp="";
	for(i = 1;i<=this.ISL_Pages;i++){
		 temp += "<span class='"+(this.ISL_Page==i?"on":"")+"' onclick='"+this.ObjectName+".ISL_GotoPage(" + i + ")' alt='" + i + "页'></span>";		
	}
	if(document.getElementById(this.ClickName)!=null)
	{
	    GetObj(this.ClickName).innerHTML = temp;
	}
}

Marquee.prototype.ISL_GotoPage=function(num){//圆点层事件，点击对应的点则滚动到对应层
	if(this.MoveLock)return;
	this.MoveLock = true;
	this.GotoLock = true;
	this.Comp = (num - 1) * this.PageWidth - GetObj(this.DivName).scrollLeft;
	this.ISL_Page = num;
	this.ISL_PageList();
	clearInterval(this.AutoPlayObj);
	this.CompScr();
	this.AutoPlay();
}

Marquee.prototype.ISL_GoUp=function(){ //左翻开始（向左滚动）
	if(this.MoveLock) return;
	clearInterval(this.AutoPlayObj);
	this.MoveLock = true;
	this.MoveTimeObj = setInterval(this.ObjectName+".ISL_ScrUp()",this.Speed);
}
Marquee.prototype.ISL_StopUp=function(){ //左翻停止（向左滚动）
	if(this.GotoLock){return};
	clearInterval(this.MoveTimeObj);
	if((GetObj(this.DivName).scrollLeft - this.fill) % this.PageWidth != 0){
		this.Comp = this.fill - (GetObj(this.DivName).scrollLeft % this.PageWidth);
		this.CompScr();
	}else{
		this.MoveLock = false;
	}
	this.AutoPlay();
}

Marquee.prototype.ISL_ScrUp=function(){ //左翻动作
	if(GetObj(this.DivName).scrollLeft <= 0){GetObj(this.DivName).scrollLeft = GetObj(this.DivName).scrollLeft + this.ID.offsetWidth}
	GetObj(this.DivName).scrollLeft -= this.Space ;
}


Marquee.prototype.ISL_GoDown=function(){ //右翻（向右滚动）
	clearInterval(this.MoveTimeObj);
	if(this.MoveLock) return;
	clearInterval(this.AutoPlayObj);
	this.MoveLock = true;
	this.ISL_ScrDown();
	this.MoveTimeObj = setInterval(this.ObjectName+".ISL_ScrDown()",this.Speed);
}

Marquee.prototype.ISL_StopDown=function(){ //右翻停止（向右滚动）
	if(this.GotoLock){return};
	clearInterval(this.MoveTimeObj);
	if(GetObj(this.DivName).scrollLeft % this.PageWidth - (this.fill>=0?this.fill:this.fill+1) != 0 ){
		this.Comp = this.PageWidth - GetObj(this.DivName).scrollLeft % this.PageWidth + this.fill;
		this.CompScr();
	}else{
		this.MoveLock = false;
	}
	this.AutoPlay();
}

Marquee.prototype.ISL_ScrDown=function(){ //右翻动作（向右滚动）
	if(GetObj(this.DivName).scrollLeft >= this.ID.scrollWidth){GetObj(this.DivName).scrollLeft = GetObj(this.DivName).scrollLeft - this.ID.scrollWidth;}
	GetObj(this.DivName).scrollLeft += this.Space;
}

Marquee.prototype.CompScr=function()//滚动（递归调用，Comp==0就跳出 大于0左翻 小于0右翻）
{
	if(this.Comp == 0)//<为0时刚好是一屏距离>
	{	
		this.ISL_Page = Math.round((GetObj(this.DivName).scrollLeft - this.fill) / this.PageWidth) + 1;
		if(this.ISL_Page>this.ISL_Pages){this.ISL_Page = 1};
		this.ISL_PageList();
		this.MoveLock = false;
		this.GotoLock = false;
		return;
	}
	
	var num;
	var TempSpeed = this.Speed,TempSpace = this.Space;
	if(Math.abs(this.Comp)<this.PageWidth/5){
		TempSpace =  Math.round(Math.abs(this.Comp/5));
		if(TempSpace<1){TempSpace=1};
	}
	
	if(this.Comp < 0){ //左翻
		if(this.Comp < -TempSpace){
			this.Comp += TempSpace;
			num = TempSpace;
		}else{
			num = -this.Comp;
			this.Comp = 0;
		}
		GetObj(this.DivName).scrollLeft -= num;
		setTimeout(this.ObjectName + '.CompScr()',TempSpeed);
	}else{ //右翻
		if(this.Comp > TempSpace){
			this.Comp -= TempSpace;
			num = TempSpace;
		}else{
			num = this.Comp;
			this.Comp = 0;
		}
		GetObj(this.DivName).scrollLeft += num;
		setTimeout(this.ObjectName +  '.CompScr()',TempSpeed);
	}
}