// title and copyright notice go here

// API
// These are the functions you will call to use the NewsScroller from your web page

var ie = document.all;
if (document.getElementById){ ie=""; }

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_showHideLayers() { //v6.0
  var i,p,v,s,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];s=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v;
	obj.display=(s=='show')?'':'none';
	 }
}

// NewsScroller_Initialize
//	Parameters:
//		elmDivForNews: object reference to a DIV element that will be used to display the news
//	Return:
//		returns a NewsScroller object, with supporting properties and methods
function NewsScroller ()
{
	if (window.NewsScrollerInstance != null)
		{
		window.alert("Only one NewsScroller control is allowed on a page.");
		return;
		}
	window.NewsScrollerInstance = this;
	
	this.LoadXML = NewsScroller_LoadXML;
	this.AddNewsItem = NewsScroller_AddNewsItem;
	this.Clear = NewsScroller_Clear;
	this.StartScrolling = NewsScroller_StartScrolling;
	this.StopScrolling = NewsScroller_StopScrolling;
	this.RestartScrolling = NewsScroller_RestartScrolling;
	this.Render = NewsScroller_Render;
	
	// public properties
	this.className = "";
	this.itemNormalClassName = "";

	this.scrollRate = 20;
	this.scrollStep = 1;
	this.scrollPause = 2000;
	
	// internal use
	this.RenderChildren = NewsScroller_RenderChildren;

	this.renderedControl = "";
	this.parentControl = "";
	this.itemsText = new Array();
	this.itemsTitle = new Array();
	this.itemsLink = new Array();
	this.itemsTarget = new Array();
	this.itemCount = 0;
	
	this.scrollEnabled = false;
}

function NewsScroller_LoadXML (sXmlID)
{
	this.Clear();
	var elmXml = document.getElementById(sXmlID);
	var xDoc = elmXml.XMLDocument;
	var xItems = xDoc.selectNodes("/Items/Item");
	for (var iNode = 0; iNode < xItems.length; iNode++)
		{
		var xItem = xItems[iNode];
		var xText = xItem.selectSingleNode("Text");
		var xLink = xItem.selectSingleNode("Link");
		this.AddNewsItem(xText.text, xLink.text);
		}
	var elmControl = document.getElementById(this.renderedControl);
	this.RenderChildren(this.renderedControl);
}

function NewsScroller_AddNewsItem (strTitle, strText, strLink, strTarget)
{
	this.itemsText[this.itemCount] = strText;
	this.itemsTitle[this.itemCount] = strTitle;
	this.itemsLink[this.itemCount] = strLink;
	this.itemsTarget[this.itemCount] = strTarget;
	this.itemCount++;
}

function NewsScroller_Clear ()
{
	this.StopScrolling ();
//	var elmControl = document.getElementById(this.renderedControl);
	var elmControl = document.getElementById(this.parentControl);
	while (elmControl.childNodes.length > 0)
		elmControl.removeChild(elmControl.childNodes[0]);
	this.itemsText = new Array();
	this.itemsTitle = new Array();	
	this.itemsLink = new Array();
	this.itemsTarget = new Array();
	this.itemCount = 0;
}

function NewsScroller_StartScrolling ()
{
	if (this.itemCount > 0)
		{
		var sControlName = this.renderedControl;
		if (sControlName == null)
			{
			window.alert ("You must render a control before you can start scrolling it.");
			return;
			}
		var elmControl = document.getElementById(sControlName);
		var elmItem0 = document.getElementById("floatingNews0");
		if (this.itemCount > 1)
			{
			var elmItem1 = document.getElementById(sControlName + "1");
			this.cySeparator = (elmItem1.offsetTop - elmItem0.offsetTop) - elmItem0.offsetHeight;
			}
		else
			{
			this.cySeparator = 0;
			}
		
		if (this.ScrollTimerID == null)
			this.ScrollTimerID = window.setInterval(NewsScroller_ScrollNews, this.scrollRate);
		}
}

function NewsScroller_StopScrolling ()
{
	window.clearInterval(this.ScrollTimerID);
	this.ScrollTimerID = null;
}
function NewsScroller_RestartScrolling ()
{
		if (this.ScrollTimerID == null)
			this.ScrollTimerID = window.setInterval(NewsScroller_ScrollNews, this.scrollRate);
}

function NewsScroller_Render(sControlID,sControlParent)
{
	if (sControlID.length == 0)
		{
		window.alert("You must provide a unique ID for the rendered control.");
		return;
		}
	var elmControl = document.getElementById(sControlID);
	if (elmControl != null)
		{
		window.alert("A NewsScroller with this ID has already been rendered. Please use a unique ID.");
		return;
		}
	this.renderedControl = sControlID;
	this.parentControl = sControlParent;
	
/*	// <DIV>
	document.write("<div");
	document.write(" id=\"" + sControlID + "\"");
	document.write(" class=\"" + this.className + "\"");
	document.write(" style=\"position: relative; width: 100%; height: 100%; visibility: hidden\"");
	document.write(">");
	
	// </DIV>
	document.write("</div>");
*/
	elpControl = document.getElementById(sControlParent);
	elmDiv = document.createElement("div");
	elmDiv.id = sControlID;
	elmDiv.style.position = "relative";
	elmDiv.style.left = "0px";
	elmDiv.style.width = "100%";
	elmDiv.style.height = "100%";
	elmDiv.className = this.className;
	elmDiv.style.overflow = "hidden";
	elmDiv.style.visibility = "hidden";
	elpControl.appendChild(elmDiv);

	this.RenderChildren(sControlID);
 
	var dtNow = new Date();
	var dtResume = new Date(dtNow.getFullYear(), dtNow.getMonth(), dtNow.getDate(), dtNow.getHours(), dtNow.getMinutes(), dtNow.getSeconds(), this.scrollPause);
	elmControl = document.getElementById(sControlID);
	elmControl.ResumeDateTime = dtResume;
}

// Supporting functions
function NewsScroller_RenderChildren()
{
	var sControlID = this.renderedControl;
	elmControl = document.getElementById(sControlID);
	
	// <DIV>
	elmDiv = document.createElement("div");
	elmDiv.style.position = "absolute";
	elmDiv.style.left = "0px";
	elmDiv.style.width = "100%";
	elmDiv.style.height = "100%";
	elmDiv.style.overflow = "hidden";
	elmControl.appendChild(elmDiv);
	
	for (var nItem = 0; nItem < this.itemCount; nItem++)
		{
		var sItemName = sControlID + nItem.toString();
		
		var elmP = document.createElement("p");
		elmP.id = sItemName;
		elmP.style.position = "relative";
		elmP.style.top = "0px";
		elmP.style.width = "100%";
		elmP.className = "news";
		elmDiv.appendChild(elmP);
		
		if (this.itemsTitle[nItem]!='') {
			var title= document.createElement("span");
			title.className="newsTitle";
			var title_n = document.createTextNode(this.itemsTitle[nItem]);
			title.appendChild(title_n);
			elmP.appendChild(title);
			var br=document.createElement("br");
			elmP.appendChild(br);
		}
				
		if (this.itemsText[nItem].toUpperCase().indexOf("<IMG")==0 || this.itemsText[nItem].indexOf("<#>")==0) {
			bHTML=true;
		} else {
			bHTML=false;
		}
		var mess= document.createElement("span");
		mess.className="news";
		elmP.appendChild(mess);

		if (this.itemsLink[nItem]=="#") {
			if (bHTML) {
				if (this.itemsText[nItem].indexOf("<#>")==0) {
					mess.innerHTML=this.itemsText[nItem].substr(3);
				} else {
					mess.innerHTML=this.itemsText[nItem];
				}
			} else {
				var tn = document.createTextNode(this.itemsText[nItem]);
				mess.appendChild(tn);
			}
		} else {
			var elmA = document.createElement("a");
			elmA.href = this.itemsLink[nItem];
			elmA.target = this.itemsTarget[nItem];
				//elmA.setAttribute("target",this.itemsTarget[nItem]);
			elmA.className="news";
				//elmA.setAttribute("className","news");
			mess.appendChild(elmA);

			if (bHTML) {
				elmA.innerHTML=this.itemsText[nItem];
			} else {
				var tn = document.createTextNode(this.itemsText[nItem]);
				elmA.appendChild(tn);
			}
		}
		}
}

function NewsScroller_ScrollNews()
{
	var ns = window.NewsScrollerInstance;
	var sControlName = ns.renderedControl;
	if (sControlName.length == 0)
		{
		window.alert ("Do not call ScrollNews directly. Use the Start method of the NewsScroller object.");
		return;
		}
	var elmControl = document.getElementById(sControlName);
	if (new Date() >= elmControl.ResumeDateTime)
		{
		// see whether any item is about to reach the top,
		var cyOffset = 0;
		for (var nItem = 0; nItem < ns.itemCount; nItem++)
			{
			var sItemName = sControlName + nItem.toString();
			var elmItem = document.getElementById(sItemName);
			if ((elmItem.offsetTop > 0) && ((elmItem.offsetTop - ns.scrollStep) <= 0))
				{
				// the top of this item has reached the top of the control,
				// so pause scrolling for the whole control
				var dtNow = new Date();
				var dtResume = new Date(dtNow.getFullYear(), dtNow.getMonth(), dtNow.getDate(), dtNow.getHours(), dtNow.getMinutes(), dtNow.getSeconds(), ns.scrollPause);
				elmControl.ResumeDateTime = dtResume;
				cyOffset = elmItem.offsetTop;
				}
			}
		if (cyOffset > 0)
			{
			// control is just now paused, so scroll all items up 
			// in order to put the top item flush with the top of the control
			for (var nItem = 0; nItem < ns.itemCount; nItem++)
				{
				var sItemName = sControlName + nItem.toString();
				var elmItem = document.getElementById(sItemName);
				if (ie) {
				   elmItem.style.pixelTop = (elmItem.style.pixelTop - cyOffset) + "px";
				} else {
				   elmItem.style.top = parseInt(elmItem.style.top) - cyOffset + "px";
				}
				
				}
			}
		else
			{
			// the control is not paused, so scroll every item up by one step
			for (var nItem = 0; nItem < ns.itemCount; nItem++)
				{
				var sItemName = sControlName + nItem.toString();
				var elmItem = document.getElementById(sItemName);
				
				if (ie) {
				   elmItem.style.pixelTop = (elmItem.style.pixelTop - ns.scrollStep) + "px";
				} else {
				   elmItem.style.top = parseInt(elmItem.style.top) - ns.scrollStep + "px";
				}
				
				
				}
			}
		// see whether any items have scrolled off the top
		for (var nItem = 0; nItem < ns.itemCount; nItem++)
			{
			var sItemName = sControlName + nItem.toString();
			var elmItem = document.getElementById(sItemName);
			if ((elmItem.offsetTop + elmItem.offsetHeight) <= 0)
				{
				// the bottom of this item has scrolled beyond the top of the control,
				// so move it to the bottom of the control
				if (ns.itemCount > 1)
					{
					var sBottomItemName;
					if (nItem == 0)
						sBottomItemName = sControlName + (ns.itemCount - 1).toString();
					else
						sBottomItemName = sControlName + (nItem - 1).toString();
					var elmBottomItem = document.getElementById(sBottomItemName);
					if (ie) {
					   elmItem.style.pixelTop = (elmBottomItem.offsetTop + elmBottomItem.offsetHeight + ns.cySeparator) - (elmItem.offsetTop - elmItem.style.pixelTop) + "px";
					   //alert(elmItem.style.pixelTop)					   
					} else {
					   elmItem.style.top = (elmBottomItem.offsetTop + elmBottomItem.offsetHeight + ns.cySeparator) - (elmItem.offsetTop - parseInt(elmItem.style.top)) + "px";
					   //alert(elmItem.style.top);					
					}
					}
				else
					{
					if (ie) {
					   elmItem.style.pixelTop = elmItem.parentElement.offsetHeight + "px";
					   
					} else {
					   elmItem.style.top = elmItem.parentNode.offsetHeight + "px";
					}
					}
				}
			}
		}
}
