function messages () {
	this.msgQueue = new Array();//存放訊息的陣列
	this.msgInterval = 1;//訊息輪播的間隔
	this.msgCurrent = 0;//目前播放的訊息
	this.msgTarget = "";//顯示訊息的物件id
	this.intervalId = null;//用來存放interval或是timer的id，以便做出暫停的效果
	this.pauseState = 0;//存放播放的狀態，用來判斷是在播放或是暫停
}

messages.prototype.addMsg = function (msg) {//新增訊息到訊息陣列
	this.msgQueue.push(msg);
}

messages.prototype.getMsgInterval = function () {//取得訊息播放的間隔
	return this.msgInterval;
}

messages.prototype.setMsgInterval = function (interval) {//設定訊息播放的間隔
	this.msgInterval = interval;
}

messages.prototype.setMsgTarget = function (id) {//設定訊息播放的目標id
	this.msgTarget = id;
}

messages.prototype.getMsgTarget = function () {//取得訊息播放的目標id
	return this.msgTarget;
}

function dynamicMsg () {
	this.container = document.createElement("div");//訊息將在這個div中顯示
	this.roller1 = document.createElement("div");//用兩個div頭接尾做出不間斷播放的效果
	this.roller2 = document.createElement("div");
	this.currentPos = 0;//以下用來存放訊息移動的位置
	this.rollerTop = 0;
	this.rollerLeft = 0;
	this.rollerRight = 0;
	this.rollerBottom = 0;
}
dynamicMsg.prototype = new messages;//繼承messages物件

dynamicMsg.prototype.display = function (width, height, border, delay, color) {
	try{
		if (this.msgQueue.length == 0) throw "No message in queue!!!";
		if (this.msgTarget == "") throw "You must assign an object id for displaying messages!!!";
		var obj = document.getElementById(this.msgTarget);
		if (obj == null) throw "Object with the assigned id:"+this.msgTarget+" cannot be found!!!";
		var callobj = this;//把this指標存到變數，等一下才有辦法讓網頁的事件來使用
		obj.innerHTML = "";
		obj.appendChild(this.container);
		var img = document.createElement("img");//用一個透明的gif把container撐到指定的大小
		img.src = "images/blank.gif";
		img.height = height;
		img.width = width;
		img.border = "0";
		obj.appendChild(img);
		this.container.style.position = "absolute";//position必須設為absolute，clip才會發生作用
		this.container.style.width = width+"px";
		this.container.style.height = height+"px";
		this.container.style.border = "solid "+border+"px";
		this.container.style.background = color;
		this.container.style.overflow = "hidden";
		this.container.style.clip = "rect(0,"+width+","+height+",0)";
		this.container.innerHTML = "";
		this.container.onmousemove = function(){callobj.pause(1);};
		this.container.onmouseout = function(){callobj.pause(0);};
		var tmp = "";
		for (i=0;i<this.msgQueue.length;i++) {//把訊息陣列中的訊息取出，一則訊息一行來顯示
			tmp += ""+this.msgQueue[i]+"";
			if (i != this.msgQueue.length-1) {
				tmp += "<br>";
			}
		}
		this.roller1.style.top = "0px";
		this.roller1.innerHTML = "";
		this.roller1.innerHTML = tmp;
		this.roller1.style.position = "relative";
		this.container.appendChild(this.roller1);
		this.rollerTop = this.roller1.offsetTop;
		this.rollerLeft = this.roller1.offsetLeft;
		this.rollerRight = this.rollerLeft + this.roller1.offsetWidth;
		this.rollerBottom = this.rollerTop + this.roller1.offsetHeight;
		this.currentPos = 0;
		this.container.appendChild(this.roller2);
		this.roller2.innerHTML = "";
		this.roller2.innerHTML = tmp;
		this.roller2.style.position = "relative";
		this.roller2.style.top = "0px";
		this.startOffset = this.roller1.offsetTop;
		this.msgInterval = delay;
		this.intervalId = window.setInterval(function(){callobj.rotate();},this.msgInterval*100);
	}
	catch (e) {
		alert(e);//顯示例外的訊息
	}
}

dynamicMsg.prototype.rotate = function () {//做出向上捲動的效果
	this.roller1.style.top = this.currentPos + "px";
	this.roller2.style.top = this.currentPos + "px";
	this.currentPos -= 2;
	try {
//		if ((this.currentPos + this.roller1.offsetHeight) <= 0) {
		if (this.roller2.offsetTop <= this.startOffset) {
			this.currentPos = 0;
		}
	}catch(e){alert(e);}
}

dynamicMsg.prototype.pause = function (status) {//做出暫停/繼續播放的效果
	if (status == 1) {
		this.pauseState = status;
		clearInterval(this.intervalId);
		return;
	}
	if (status == 0) {
		this.pauseState = status;
		var callobj = this;
		this.intervalId = setInterval(function(){callobj.rotate();},this.msgInterval*100);
		return;
	}
}
