﻿/*
文件名 ScrollDiv.js
用途   無縫滾動指定內容
版本   2.0.2
兼容   IE6.0 Firefox2.0
作者   songguowei
最後囊改 2006/11/03
*/
//公共變量
var PInstanceCreatedNums=0;  //創建的實例數量
var PInstanceMaxCreateNums=100;  //考慮到效率問題，頁面實例限制為100,可以根據實際硬件配瞞進行囊改
//創建[Marquee]對像
function Marquee()
{
	var mStoptime=0;
	var offsetcount=0;
	var thisObj=this;
	var speed=0;			//移動速度
	var parentdiv ="";		//指定滾動父級包含層
	var maindiv = "";		//指定滾動層
	var copydiv = "";		//空層，必要
	var speed = 0;			//滾動速度 單位是毫秒 1000＝1秒
	var direction = "";		//滾動的方向 "left":向左 "right":向右 "up":向上 "down":向下 
	var pauseDistance = 0;	//暫停距離，每隔多少距離暫停滾動
	var pauseTime = 0;		//暫停時間 單位：毫秒
	var startStatus =0;		//初始狀態，默認為0，可不設瞞；0:顯示滾動內容；1:初始狀態為空白；
	var parentdivWidth=0;	//顯示寬度
	var parentdivHeight=0;  //顯示高度
	PInstanceCreatedNums++;
	//方法 start() 作用:初始化
	thisObj.start=function()
	{
		try
		{
			if(PInstanceCreatedNums>=PInstanceMaxCreateNums)
			{
				alert("創建實例超過最大限制！");
				return false;
			}
			with(thisObj)
			{
				parentdiv=document.getElementById(parentDiv);
				maindiv=document.getElementById(mainDiv);
				divCopy();
				if(parentdiv.style)
				{
					parentdiv.style.overflow='hidden';
					parentdiv.style.width=parentdivWidth;
					parentdiv.style.height=parentdivHeight;
				}
				//鼠標移動事件的調用方法
				parentdiv.onmouseover=Pause;
				parentdiv.onmouseout=Begin;
				//鼠標移動事件的調用方法
				switch(direction)
				{
				case "up":
					maindiv.style.display='block';
					copydiv.style.display='block';
					parentdiv.scrollTop=0;
					break;
				case "down":
					maindiv.style.display='block';
					copydiv.style.display='block';
					parentdiv.scrollTop=maindiv.offsetHeight*2-parentdivHeight;
					break;
				case "left":
					parentdiv.style.whiteSpace='nowrap';
					maindiv.style.display='inline';
					copydiv.style.display='inline';
					parentdiv.scrollLeft=0;
					break;
				case "right":
					parentdiv.style.whiteSpace='nowrap';
					maindiv.style.display='inline';
					copydiv.style.display='inline';
					parentdiv.scrollLeft=maindiv.offsetWidth*2-parentdivWidth;
					break;				
				}
				offsetcount=pauseDistance;
				Begin();
			}
		}
		catch(e)
		{
			alert('發生錯誤！錯誤內容:['+e.message+']');
		}
	}
	//方法 divCopy() 作用:複製層內容
	thisObj.divCopy=function()
	{
		with(thisObj)
		{
			//動態創建用於複製的 copydiv
			copydiv=document.createElement("div");
			copydiv.id='copy'+maindiv.id;
			parentdiv.appendChild(copydiv);			
			copydiv.innerHTML=maindiv.innerHTML;
		}
	}
	//方法 doPause() 作用:滾動間隙暫停函數
	thisObj.doPause=function()
	{
		mStoptime+=1;
		if(mStoptime==thisObj.pauseTime)
		{
			mStoptime=0;
			offsetcount=0;
			return true;
		}
		return false;
	}
	//方法 iMarquee() 作用:無縫滾動控制
	thisObj.iMarquee=function()
	{
		with(thisObj)
		{	
			switch(direction)
			{
			case "up":
				if(offsetcount>=pauseDistance)
				{
					if(parentdiv.scrollTop>=copydiv.offsetTop) 
					{
						if(doPause())
						{
							parentdiv.scrollTop-=maindiv.offsetHeight; 
						}
					}
					else
					{
						doPause();
					}
				}
				else
				{
					parentdiv.scrollTop++;
					offsetcount++;
				}			
				break;
			case "down":
				if(offsetcount>=pauseDistance) 
				{
					if(parentdiv.scrollTop<=maindiv.offsetHeight-parentdivHeight) 
					{
						if(doPause())
						{
							parentdiv.scrollTop=maindiv.offsetHeight*2-parentdivHeight;
						}
					}
					else
					{
						doPause();
					}
				}
				else
				{
					parentdiv.scrollTop--;
					offsetcount++;
				}			
				break;
			case "left":
				if(offsetcount>=pauseDistance)
				{
						
					if(parentdiv.scrollLeft>=copydiv.offsetWidth)
					{
						if(doPause())
						{
							parentdiv.scrollLeft-=maindiv.offsetWidth;
						}
					 }
					 else
					 {
						doPause();
					 }
				 }
				else
				{
					parentdiv.scrollLeft++;
					offsetcount++;
				}
				break;
			case "right":
				if(offsetcount>=pauseDistance)
				{
					if(parentdiv.scrollLeft<=0)
					{
						if(doPause())
						{
							parentdiv.scrollLeft+=maindiv.offsetWidth;
						}					
					 }
					 else
					 {
						doPause();
					 }
				 }
				else
				{
					parentdiv.scrollLeft--;
					offsetcount++;
				}			
				break;
			}
		}
	}
	thisObj.Begin=function() //方法:Begin() 作用:開始滾動
	{
		thisObj.timer=setInterval(thisObj.iMarquee,thisObj.speed);
	}
	thisObj.Pause=function() //方法 Pause() 作用:暫停滾動
	{
		clearInterval(thisObj.timer);
	}
}