/******************************************************************************
* scroller.js                                                                 *
*                                                                             *
* Allows you to create scrolling displays on a page. Multiple scrollers can   *
* be defined, each with it's own parameters and list of items. Item text can  *
* include basic HTML tags, including links and images.                        *
* Note: requires dhtmllib.js.                                                 *
******************************************************************************/

//*****************************************************************************
// Scroller constructor Object.
//*****************************************************************************
	function Scroller(Width, Height) {
		
		// set Width and Height
		this.width = Width;
		this.height = Height;
		
		// Set Some Default 
		this.x = 0;
		this.y = 0;
		this.border = 0;
		
		// Create Array For Items
		this.items = new Array();
		this.created = false;
		
		// Set Default Speed and Pause Time
		this.speed = 50;
		this.pauseTime = 1000;
		
		// Define Methods
		this.setColors = ScrollerSetColor;		
		this.setSpeed = ScrollerSpeed;
		this.setPause = ScrollerPause;
		this.addItem = ScrollerAddItem;
		this.create = ScrollerCreate ;
		this.show = ScrollerShow;
		this.hide = ScrollerHide;
		this.moveTo = ScrollerMoveTo;
		this.moveBy = ScrollerMoveBy;
		this.getzIndex = ScrollerGetzIndex;
		this.setzIndex = ScrollerSetzIndex;
		this.stop = ScrollerStop;
	    this.start = ScrollerStart;
	}



//*****************************************************************************
// Scroller methods.
//*****************************************************************************

	function ScrollerSetColor(bgColor) {
		// This Will Set the background Colors
		
		if (this.created) {
			alert("Scroller Error: Scroller has already been created.");
			return;
		}
		
		this.bgColor = bgColor;
	}
	
	function ScrollerSpeed(sSpeed) {
		// This Will Set the Scroller Speed
		
		if (this.created) {
			alert("Scroller Error: Scroller has already been created.");
			return;
		}
		
		this.speed  = sSpeed;
	}
	
	function ScrollerPause(sPause) {
		// This Will Set the Pause Time
		
		if (this.created) {
			alert("Scroller Error: Scroller has already been created.");
			return;
		}
		
		this.pauseTime  = sPause;		
	}
	
	function ScrollerAddItem(strItem) {
		// This Will Add an Items to the Itmes Array
		if (this.created) {
			alert("Scroller Error: Scroller has already been created.");
			return;
		}
		
		this.items[this.items.length]  = strItem;
	}
	
			// Create the Scroller
	function ScrollerCreate() {
		
		var intIndex, iIndex, intCharLength, strLayer, strItem;
		var strFirstText, strSecondText ;
		var xPos, yPos;
		
		if (!isMinNS4 && !isMinIE4) {
			return;
		}	
		
		// On first scroller, start interval timer.		
		if (ScrollList.length == 0)
			setInterval('Scrolling()',ScrollInterval);
		
		
		// Create the Scroller only once;
		if (this.created) {
			alert("Scroller Error: Scroller has already been created.");
			return;
		}
		this.created = true;
		
		// Copy first item to the end of the list, this lets us 
		// scroll from the last defined item to the first without jumping.
		this.items[this.items.length] = this.items[0];
		
		// Set Up The HTMl Code
		var StrtTable, EndTable, StrtRow, EndRow, StrtCol, EndCol, StrtColCenter
		var strSeperator = "@@";
		
		StrtTable = '<Table Border = "0" '
				 + 'Cellpadding = "0" CellSpacing = "0" '
				 + 'Width = "' + this.width + '" '
				 + 'Height = "' + this.height + '" >' ;
		EndTable = '</Table>';
		
		StrtRow = '<TR>';
		EndRow = '</TR>';
		
		StrtCol = '<TD Width = "' + this.width + '" Align = "center" Class = "ft7pt_b" >';
		StrtColCenter = '<TD Width = "' + this.width + '" Align = "Center" Class = "ft8pt_red">';
		EndCol = '</TD>';
		
		// Build The Layers
		if (isMinNS4) {
			// If NetScape 
			this.baseLayer = new Layer(this.width);
			this.scrollLayer = new Layer(this.width, this.baseLayer);
			this.scrollLayer.visibility = "inherit";
			this.itemLayers = new Array();
			for (intIndex = 0; intIndex < this.items.length; intIndex++) {
				// Nullify the Original String 
				strLayer = ""; 
				strItem = "";
				strItem = this.items[intIndex];				
				this.itemsLayers[intIndex] = new Layer(this.width,this.scrollLayer);
				this.itemsLayer[intIndex].document.open();
				strLayer = StrtTable;				
					var strItemArray = strItem.split(strSeperator);
					for (intCharLength = 0; intCharLength < strItemArray.length; intCharLength++) {
						strLayer = strLayer + StrtRow;
						if (strItemArray.length == 1) {
							strLayer = strLayer + strColCenter;
						}
						else {
							strLayer = strLayer + StrtCol;
						}
						strLayer = strLayer + strItemArray[intCharLength] + EndCol + EndRow;
					}					
				strLayer = strLayer + EndTable;
				this.itemsLayer[intIndex].document.close();
				this.itemLayers[i].visibility = "inherit";
			}
			
			// Set background colors.
			setBgColor(this.baseLayer, this.bgColor);
			setBgColor(this.scrollLayer, this.bgColor);
		}
		
		if (isMinIE4) {
			// If Internet Explorer
			iIndex = ScrollList.length;
			strLayer = '<div id="scroller' + iIndex + '_baseLayer"'
			         + ' style="position:absolute;'
			         + ' background-color:' + this.bgColor + ';'
			         + ' width:' + this.width + 'px;'
			         + ' height:' + this.height + 'px;'
			         + ' overflow:hidden;'
			         + ' visibility:hidden;">\n'
			         + '<div id="scroller' + iIndex + '_scrollLayer"'
			         + ' style="position:absolute;'
			         + ' background-color: ' + this.bgColor + ';'
			         + ' width:' + this.width + 'px;'
			         + ' height:' + (this.height * this.items.length) + 'px;'
			         + ' visibility:inherit;">\n';			
				for (intIndex = 0; intIndex < this.items.length; intIndex++) {
					// Nullify the Original String 
					strItem = "";
					strItem = this.items[intIndex];
					strFirstText = "";
					strSecondText = "";	
					strLayer += '<div id="scroller' + iIndex + '_itemLayers' + intIndex + '"'
				             +  ' style="position:absolute;'
					         +  ' width:' + this.width + 'px;'
					         +  ' height:' + this.height + 'px;'
							 +  ' visibility:inherit;">\n';					
					strLayer = strLayer + StrtTable;					
					var strItemArray = strItem.split(strSeperator);					
						for (intCharLength = 0; intCharLength < strItemArray.length; intCharLength++) {
							strLayer = strLayer + StrtRow; 
							if (strItemArray.length == 1) { 
								strLayer = strLayer + StrtColCenter;
							}
							else {
								strLayer = strLayer + StrtCol;
							}
							strLayer = strLayer + strItemArray[intCharLength] + EndCol + EndRow;
						}											
					strLayer = strLayer + EndTable;
					strLayer = strLayer +  '</div>\n';
				}			
			
			strLayer = strLayer + '</div>\n</div>\n';	
			
			// Insert HTML code at end of page. For IE4, need to scroll window to
			// end of page, insert and scroll back to correct bug.
			
			if (!isMinIE5) {
				xPos = getPageScrollX();
				yPos = getPageScrollY();				
				window.scrollTo(getPageWidth(),getPageHeight());
			}
			
			document.body.insertAdjacentHTML("beforeEnd", strLayer);

			if (!isMinIE5) {
				window.scrollTo(xPos,yPos);
			}
			
						
			// Get handles to each layer.
			this.baseLayer = getLayer("scroller" + iIndex + "_baseLayer");
			this.scrollLayer = getLayer("scroller" + iIndex + "_scrollLayer");
			this.itemLayers = new Array();
			
			for (intIndex = 0; intIndex < this.items.length; intIndex++) {
				this.itemLayers[intIndex] = getLayer("scroller" + iIndex + "_itemLayers" + intIndex);
			}
		}	
		
		// Position and clip base and scroll layers.		
			moveLayerTo(this.baseLayer, this.x, this.y);			
			clipLayer(this.baseLayer, 0, 0, this.width, this.height);			
			moveLayerTo(this.scrollLayer, this.border, this.border);
			clipLayer(this.scrollLayer, 0, 0,
				this.width - 2 * this.border, this.height - 2 * this.border);

		// Position and clip each item layer.
			xPos = 0;
			yPos = 0;
		
			for (intIndex = 0; intIndex < this.items.length; intIndex++) {
				moveLayerTo(this.itemLayers[intIndex],xPos,yPos);
			    clipLayer(this.itemLayers[intIndex], 0, 0, this.width, this.height);
				yPos += this.height;				
			}	 
			
		// Set up scrolling parameters.
		
			this.stopped = false;
			this.currentY = 0;
			this.stepY = this.speed / ( 1000 / ScrollInterval);
			this.stepY = Math.min(this.height, this.stepY);						
			this.nextY = this.height;			
		    this.maxY = this.height * (this.items.length - 1);
			
			this.paused = true;
			this.counter = 0;
	
		// Add to global list.
			ScrollList[ScrollList.length] = this;

		// Display it.
			showLayer(this.baseLayer);		
	}
	
	function ScrollerShow() {
		// Show The Scoller 
		
		if (this.created) 
			showLayer(this.baseLayer);
		
	}
	
	function ScrollerHide() {
		// Hide Scroller 
		
		if (this.created)
			hideLayer(this.baseLayer);
	}
	
	function ScrollerMoveTo(xPos, yPos) {
		// Move the Scoller to This Position
		
		if (this.created)
			moveLayerTo(this.baseLayer,xPos,yPos);
	}
	
	function ScrollerMoveBy(xDist , yDist) {
		// Move By this Positions dist
		
		if (this.created)
			moveLayerBy(this.baseLayer,xDist,yDist);
	}
	
	function ScrollerGetzIndex() {
		// Get the z Index 
		
		if (this.created) 
			return(getzIndex(this.baseLayer));
		else
			return(0);
	}
				
	function ScrollerSetzIndex(ZIndex) {
		// Set the Z Index 
		
		if (this.created)
			setzIndex(this.baseLayer, ZIndex);
	}		

	function ScrollerStop() {
		// Stop Scrolling 
		
		this.stopped = true;
	}
	
	function ScrollerStart() {
		// Start Scrolling 
		
		this.stopped = false;
	}	
	
//*****************************************************************************
// Code for scrolling.
//*****************************************************************************

// An array is used to hold a pointer to each scroller that is defined. The
// Scrolling() function runs at regular intervals and updates each scroller
// in this list.
	
	var ScrollList = new Array();
	var ScrollInterval = 20;
	
	function Scrolling() {
		// To Scroll
		var intIndex;
		// Update each scroller object in the list.		
		for (intIndex = 0; intIndex < ScrollList.length; intIndex++) {
			
			// If Stopped , Skip 
			if (ScrollList[intIndex].stopped);
			
			// If Paused, update counter
			else if (ScrollList[intIndex].paused) {
				ScrollList[intIndex].counter += ScrollInterval;
				if (ScrollList[intIndex].counter > ScrollList[intIndex].pauseTime) 
					ScrollList[intIndex].paused = false;
			}
			
			// Scroll It
			else { 
				ScrollList[intIndex].currentY += ScrollList[intIndex].stepY;
				
				// Pause it if next has scrolling view
				if (ScrollList[intIndex].currentY >= ScrollList[intIndex].nextY) {
					ScrollList[intIndex].paused = true;
					ScrollList[intIndex].counter = 0;
					ScrollList[intIndex].currentY = ScrollList[intIndex].nextY;
					ScrollList[intIndex].nextY += ScrollList[intIndex].height;
				}
				
				// When we reach the end , start over again
				if (ScrollList[intIndex].currentY >= ScrollList[intIndex].maxY) {
					ScrollList[intIndex].currentY -= ScrollList[intIndex].maxY;
					ScrollList[intIndex].nextY = ScrollList[intIndex].height;
				}
				scrollLayerTo(ScrollList[intIndex].scrollLayer, 
							  0, Math.round(ScrollList[intIndex].currentY),
							  false);
				
			}
		}
	}
	
//*****************************************************************************
// Code to handle a window resize.
//*****************************************************************************

// These variables are used to determine if a resize event is a true one.
// Necessary due to a bug in older NS4 releases.

	var origWidth;
	var origHeight;
	
	if (isMinNS4) { 
		origWidth  = window.innerWidth;
		origHeight = window.innerHeight;
	}
	
	window.onresize = scrollerReload;

	function scrollerReload() {
		// Reload page in case of a browser resize. 
		// First make sure it's a true resize.

		if (isMinNS4 && origWidth == window.innerWidth && origHeight == window.innerHeight)
			return;
		window.location.href = window.location.href;
	}
	