// JavaScript Document
/////////////////////////////////////////
//程序名：CMarquee 文本移动类
//版  本：v2.0
//作  者：黄健
//时  间: 2008-08-05
//    QQ: 573704282
//Email : freewind22@163.com
// //////////////////////////////////////
var nDelay = 5000, cm = [], lines = [], n = 0, timerID = 0;

function $(id){
	return document.getElementById(id);
}

var Class = {
	create : function(){
		return function(){
			this.initialize.apply(this, arguments);
		}
	}
};

Object.extend = function(dest, sour){
	for(var property in sour){
		dest[property] = sour[property];
	}
	return dest;
};

var CMarquee = Class.create();
CMarquee.prototype = {
	initialize : function(output, lines){
		this.lines = lines || []; //文本行
		this.output = $(output); //输出对象
		this.marquee = null; //滚动对象
		this.speed = 5000; //移动间隔,毫秒
		this.lineHeight = 21; //行高
		this.timerID = 0;
		this.isStop = false; //是否停止
		this.currentLine = 1; //当前显示行
		this.moveBuffer = true; //移动缓冲
		this.moveSize = 7;
		this.autoPlay = false; //自动播放
		
		this.initEvent();
	},
	initEvent : function(){
		var This = this;
		this.output.onmouseover = function(){
			This.isStop = true;
			if(!This.autoPlay){
				pause();
			}
		};
		this.output.onmouseout = function(){
			This.isStop = false;
			if(This.autoPlay){
				//This.start();
			}else{
				start();
			}
		};
	},
	add : function(line){
		this.lines.push(line);
	},
	show : function(){
		this.marquee = document.createElement("div");
		this.marquee.className = "divMarquee2";
		this.marquee.style.top = "0px";
		this.output.innerHTML = "";
		
		for(var i=0; i<this.lines.length; i++){
			var line = document.createElement("div");
			//line.appendChild(document.createTextNode(this.lines[i]));
			line.innerHTML = this.lines[i];
			this.marquee.appendChild(line);
		}
		//最后一行添加第一个
		var line = document.createElement("div");
		//line.appendChild(document.createTextNode(this.lines[0]));
		line.innerHTML = this.lines[0];
		this.marquee.appendChild(line);
		
		this.output.appendChild(this.marquee);

		if(arguments.length > 0){
			this.autoPlay = arguments[0];
		}
		if(this.autoPlay){
			this.start();
		}
	},
	start : function(){
		var This = this;
		this.timerID = setTimeout(function(){This.move();}, This.speed);
	},
	move : function(){
		if(this.isStop || this.lines.length <= 1){
			return;
		}
		
		if(this.moveBuffer){
			//移动缓冲
			var This = this;
			var moveSize = this.moveSize;
			var num = this.lineHeight / moveSize;
			var tmpTimerID = 0;
				
			function move2(){
				num--;
				This.marquee.style.top = parseInt(This.marquee.style.top) - moveSize + "px";
				if(num == 0){
					//
					clearInterval(tmpTimerID);
					This.currentLine++;
					//回到第一行
					if(This.currentLine > This.lines.length){
						This.marquee.style.top = "0px";
						This.currentLine = 1;
					}
					if(This.autoPlay){
						This.start();
					}
				}
			}
			tmpTimerID = setInterval(move2, 30);
		}else{
			this.currentLine++;
			if(this.currentLine > this.lines.length){
				this.marquee.style.top = "0px";
				this.currentLine = 1;
			}else{
				this.marquee.style.top = parseInt(this.marquee.style.top) - this.lineHeight + "px";
			}
			if(this.autoPlay){
				this.start();
			}
		}
	}
};


/***************************************/
function initMarquee(){
	lines = [];
	for(var i = 0; i < 15; i++){
		lines[i] = $("divKeyword"+(i+1)).innerHTML.split("|");
	}
	n = lines.length;
	for(var i=0; i < n; i++){
		cm[i] = new CMarquee("divKeyword" + (i+1), lines[i]);
		cm[i].show();
		//
		delete lines[i];
	}
	
	start();
}
function initMarquee2(){
	lines[n++] = $("divKeyword1").innerHTML.split("|");
	for(var i=0; i < n; i++){
		cm[i] = new CMarquee("divKeyword" + (i+1), lines[i]);
		cm[i].show();
		//
		delete lines[i];
	}
	
	start();
}
function initMarqueeN(){
	if($("scrollNews")){
		var lines = $("scrollNews").innerHTML.replace(/^\||\|$/g, "").split("||");
		var mar = new CMarquee("scrollNews", lines);
		mar.lineHeight = 21;
		mar.moveSize = 7;
		mar.speed = 5000;
		mar.autoPlay = true;
		mar.show();
	}
}
function initMarquee3(){
	if($("topImgs")){
		window.mar = new CMarquee("topImgs", $("topImgs").innerHTML.split("|"));
		mar.lineHeight = 30;
		mar.moveSize = 6;
		mar.speed = 5000;
		mar.autoPlay = true;
		mar.show();
	}
}
function initMarquee4(){
	if($("topImgs")){
		var isShow = true;
		if(!window.mar){
			isShow = false;
			window.mar = new CMarquee("topImgs", $("topImgs").innerHTML.split("|"));
		}
		mar.lineHeight = 88;
		mar.moveSize = 11;
		mar.speed = 5000;
		mar.autoPlay = true;
		if(!isShow){
			mar.show();
		}
	}
}
function start(){
	timerID = setInterval("move()", nDelay);
}
function move(){
	for(var i=0; i < n; i++){
		cm[i].move();
	}
}
function pause(){
	if(timerID > 0){
		clearInterval(timerID);
		timerID = 0;
	}
}
