////////////////////////////////////////////
// Copyright 2007. Tom Kaminski
//
// Description: This object can be used to manage multiple scrolling
//				banners, marquees, or new headlines
////////////////////////////////////////////


//////////////////////////////////////////////////
// ScrollTicker Object 
if(gScrollTickerList==null) var gScrollTickerList = new Array();

function ScrollTicker(scrollerDivId,prevButtonId,nextButtonId,pauseButtonId) {

	// Public ///////////////////////////////////
	this.scrollerDivId = scrollerDivId;
	this.prevButtonId = prevButtonId;
	this.nextButtonId = nextButtonId;	
	this.pauseButtonId = pauseButtonId;
	this.numHeadlines = 2;
	
	this.delay=6000;	// ms between headline scrolling
	this.lineHeight = 15;	// height defined in css + 2px padding
	this.nudgeSpace = 3;  //  pixels to move for each interval (must be divisible by line height)
	this.nudgeTime = 45; //  ms for each movement
	
	// Private /////////////////////////////////
	this.scrollOffset = 0;
	this.currentHeadline = 1;	
	this.autoAdvance = false;
	this.moveDirection = 1;
	this.moveHeadlineTimer = null;
	this.newHeadlineTimer = null;
	
	this.id = gScrollTickerList.push(this)-1;	// Let's manage all tickers
	
	prev = document.getElementById(this.prevButtonId);
	if(prev!=null) {
		prev.scrollTicker = this;
		prev.onclick = function() {
			this.scrollTicker.NewHeadline(-1);
		}
	}
	
	next = document.getElementById(this.nextButtonId);
	if(next!=null) {
		next.scrollTicker = this;
		next.onclick = function() {
			this.scrollTicker.NewHeadline(1);
		}
	}
	pause = document.getElementById(this.pauseButtonId);
	if(pause!=null) {
		pause.scrollTicker = this;
		pause.onclick = function() {
			if(this.scrollTicker.autoAdvance) this.scrollTicker.Stop();
			else this.scrollTicker.Start();
		}
	}
	
	
	////////////////////////////////////////////
	this.UpdateButtons = function() {
		prev = document.getElementById(this.prevButtonId);
		next = document.getElementById(this.nextButtonId);
		
		if(prev == null || next == null) return;
		
		if (this.currentHeadline == 1){ // first headline
			prev.style.display = 'none';
			next.style.display = 'inline';
		}
		else if(this.currentHeadline == this.numHeadlines) {  // last headline
			prev.style.display = 'inline';
			next.style.display = 'none';
		}
		else{ // intermediate headline
			prev.style.display = 'inline';
			next.style.display = 'inline';
		}
	}
	
	///////////////////////////////////////////
	this.NewHeadline = function(dir) {
		//alert('NewHeadline:' + this + ":" + this.currentHeadline);
		
		if(dir!=null) this.moveDirection = dir;		
		if(this.numHeadlines<=1) return;	// scrolling is disabled!!!
		
		// Update the direction
		if(this.moveDirection>0 && this.currentHeadline >= this.numHeadlines) this.moveDirection = -1;
		else if(this.moveDirection<0 && this.currentHeadline <= 1) this.moveDirection = 1;	
		
		// Move it!
		this.currentHeadline += this.moveDirection;	
		this.MoveHeadline();
		this.UpdateButtons();
		//alert('NewHeadline:AutoAdvance=' + this.autoAdvance);
		if(this.autoAdvance) this.InvokeAutoScroll();
	}
	
	///////////////////////////////////////////
	this.MoveHeadline = function() {
		var targetOffset = -(this.currentHeadline-1) * this.lineHeight;
		if(this.scrollOffset==targetOffset) return;	// nothing to move!!
						
		if(this.scrollOffset < targetOffset) this.scrollOffset += this.nudgeSpace; 
		else if(this.scrollOffset > targetOffset) this.scrollOffset -= this.nudgeSpace; 
		
		newsDiv = document.getElementById(this.scrollerDivId);
		//alert('scrollOffset=' + this.scrollOffset + "  targetOffset=" + targetOffset);
		newsDiv.style.top = this.scrollOffset + "px";	// apply the new offset
		
		if(this.scrollOffset!=targetOffset) this.moveHeadlineTimer = setTimeout("gScrollTickerList[" + this.id + "].MoveHeadline()",this.nudgeTime); 	// Keep it a movin
	}
	
	///////////////////////////////////////////
	this.InvokeAutoScroll = function() {
		//alert('UpdateAutoScroll:' + this);
		clearTimeout(this.newHeadlineTimer);
		this.newHeadlineTimer = setTimeout("gScrollTickerList[" + this.id + "].NewHeadline()",this.delay); 	
	}
	
	/////////////////////////////////////////////////
	this.Start = function() {
		//alert('Start:' + this);
		this.autoAdvance = true;
		this.UpdateButtons();
		this.InvokeAutoScroll();
	}
	
	///////////////////////////////////
	this.Stop = function() {
		clearTimeout(this.moveHeadlineTimer);
		clearTimeout(this.newHeadlineTimer);
		this.autoAdvance = false;
	}
	
	/////////////////////////////////////////
	this.Reset = function() {
		this.Stop();
		newsDiv = document.getElementById(this.scrollerDivId);
		newsDiv.style.top = '0px';
		this.currentHeadline = 1;
		this.scrollOffset = 0;
		this.UpdateButtons();
	}
}
