
var xmlDoc_m,parser_m;

try //Internet Explorer
  {
  xmlDoc_m = new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc_m.async="false";  
  }
 catch(e)
  {  
  try //Firefox, Mozilla, Opera, etc.
    {    
    parser_m = new DOMParser();    
    }
  catch(e) {alert(e.message)}
  }

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.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";
	if ((this.currentPos + this.roller1.offsetHeight) == 0) {
		this.currentPos = 0;
	}
}

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;
	}
}

function FillMomentNews()
 {  
  var strValue = "http://rss.chinatimes.com/rss/latestnews-u.rss";
  var url = "getRssContent.asp";
  var pars=	"rssurl=" + escape(strValue);
	var myAjax = new Ajax.Request(
                    url,
                    {method: 'post', parameters: pars , asynchronous: true , onComplete: MomentNewsOK}
                    ); 
 }
 
function MomentNewsOK(data)
 {  
 var resultText = data.responseText;  
 var xRows;
 var a = new dynamicMsg();
 if(resultText.indexOf("<msg>")!= -1)
 {     
  try //Internet Explorer
  {
  xmlDoc_m.loadXML(resultText);
  }
catch(e)
  {  
  try //Firefox, Mozilla, Opera, etc.
    {    
    xmlDoc_m = parser_m.parseFromString(resultText,"text/xml");
    }
  catch(e) {
  alert(e.message);
   }
  }  
  xRows = xmlDoc_m.getElementsByTagName("msg"); 
    
  arrayRSSfeed = new Array();
  for(var iC=0;iC<xRows.length;iC++){
  intMessage = arrayRSSfeed.length;
  if (isIE1){   
   sTitle = xRows[iC].getElementsByTagName("title")[0].text;
   sLink = xRows[iC].getElementsByTagName("link")[0].text;
   sDesc = trim(xRows[iC].getElementsByTagName("description")[0].text);
  }else{
     blah=xRows[iC].childNodes;
     for(var j = 0; j < blah.length; j++){
      if (blah[j].nodeType != 3 && blah[j].nodeType != 8) {       
       if(blah[j].nodeName=="title")       
         sTitle =  blah[j].firstChild.nodeValue;         
       else if(blah[j].nodeName=="link")         
         sLink = blah[j].firstChild.nodeValue;
       else if(blah[j].nodeName=="description")
         sDesc = trim(blah[j].firstChild.nodeValue);                 
      }
     }
   }
  arrayRSSfeed[intMessage] = new Array(sTitle,sLink,sDesc);  
  }
 for(var i=0;i<arrayRSSfeed.length;i++){
 a.addMsg("<a href='javascript:showBreakNews(" + i.toString() + ")' class='marquee'><img src='images/title-li-1.gif' border='0' align='absmiddle' height='13' />" + arrayRSSfeed[i][0] + "</a>");
 } 
 a.setMsgTarget("panel");
 a.display(174,100,0,1.5,"#FFFFF0");
 a.container.style.fontSize = "13px";  
 } 
}

function showBreakNews(arrIndex)
 {
 var form_code,diffY1;
 var pane = $("refNews");
  if(pane){
   form_code  = '<form name="refForm">\n';
   form_code += '<div id="divNewsHolder">\n';
   form_code += '<div id="divNewsTitle">\n';
   form_code += '&nbsp;引用新聞</div>\n';
   form_code += '<div id="divAd">\n';
   form_code += '<div id="divNews">&nbsp;<p>\n';   
   form_code += '<div id="divTitle"></div><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color=black>引文：</font>';
   form_code += '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<textarea name="refNewsDesc" cols="60" rows="7" wrap="VIRTUAL"></textarea>\n';
   form_code += '<p><div id="divRefIt" style="text-align:center;"></div>\n';
   form_code += '</div>\n';
   form_code += '</div>\n';
   form_code += '<div id="divControl"><input type="button" name="refClose" id="refClose" value="關閉" OnClick="closeRef(\'refNews\');" /></div></div></form>\n';
   pane.className= "opac0";
   pane.innerHTML = form_code;
   var news_code = '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="black" size="2">' + arrayRSSfeed[arrIndex][0] + '</font>';
   news_code += '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="' + arrayRSSfeed[arrIndex][1] + '" target="_blank"><font color="black" size="2">' + arrayRSSfeed[arrIndex][1]+ '</font></a>';
   $("divTitle").innerHTML = news_code;
   document.refForm.refNewsDesc.value = arrayRSSfeed[arrIndex][2];
   $("divRefIt").innerHTML = '<input type="button" name="btnRefIt" id="btnRefIt" value="引用本則新聞" OnClick="RefIt(' + arrIndex.toString() + ');document.forums.Content.focus();" />';   
   pane.style.left = (window.screen.width-710)/2;
    if (isIE1) {// mean ie4,ie5,ie5.5 or above		
		diffY1 = window.document.body.scrollTop;   
    }
    if (isNS1){		
		diffY1 = self.pageYOffset;
		}	
    pane.style.top = diffY1 + 40; 
    pane.style.display = "";
    SetClass("refNews",0);
  }
 }

function load_marquee(){  
 FillMomentNews(); 
}
