// JavaScript Document
var nDelay = 4000;
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 = 4000; 
		this.lineHeight = 21; 
		this.timerID = 0;
		this.isStop = false; 
		this.currentLine = 1; 
		this.moveBuffer = true; 
		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 = "divMarquee";
		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 = 3;
			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[n++] = $("divKeyword1").innerHTML.split("|");
	lines[n++] = $("divKeyword2").innerHTML.split("|");
	lines[n++] = $("divKeyword3").innerHTML.split("|");
	lines[n++] = $("divKeyword4").innerHTML.split("|");
	lines[n++] = $("divKeyword5").innerHTML.split("|");
	lines[n++] = $("divKeyword6").innerHTML.split("|");
	lines[n++] = $("divKeyword7").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 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;
	}
}
