var TICKER = {

	maxCount : 10, // default maximum to display
	delay : 4000, // delay between steps, in milliseconds
	maxsteps : 5, // number of steps to take to change from start color to endcolor
	stepdelay : 40, // time in miliseconds of a single step
	startcolor : [255,255,255], // start color (red, green, blue)
	endcolor : [51,51,51], // end color (red, green, blue)
	fadelinks : 1,  //should links inside scroller content also fade like text? 0 for no, 1 for yes.
	playPauseId : "playPause",

	index : 0,
	fadecounter : null,
	doRun : 1,
	t : null,
	content : null,
	container : null,
	pauseImg : null,
	playImg : null,
	
	init : function(storiesId, wrapperId, maxCount) {
	
		// precache images
		TICKER.pauseImg = new Image();
		TICKER.pauseImg.src = "http://media.buffalonews.com/static/images/frontpage/ticker-pause.gif";
		TICKER.playImg = new Image();
		TICKER.playImg.src = "http://media.buffalonews.com/static/images/frontpage/ticker-play.gif";
		
		TICKER.content = document.getElementById(storiesId).getElementsByTagName("LI");
		TICKER.container = document.getElementById(wrapperId);
		if (maxCount > 0) {
			TICKER.maxCount = maxCount;
		}
		if (this.index >= TICKER.content.length) {
			this.index = 0;
		}
		TICKER.run();
	},
	
	run : function() {
		TICKER.setContent();
		if (TICKER.fadelinks == 1) {
			TICKER.linkcolorchange(1);
		}
		TICKER.colorfade(1, 15);
		if (TICKER.index >= TICKER.maxCount - 1) {
			TICKER.index = 0;
		} else {
			if (TICKER.index < TICKER.content.length-1) {
				TICKER.index++;
			} else {
				TICKER.index = 0;
			}
		}
		document.getElementById(TICKER.playPauseId).src = "http://media.buffalonews.com/static/images/frontpage/ticker-pause.gif";
	},
		
	stop : function() {
		window.clearTimeout(TICKER.t);
		document.getElementById(TICKER.playPauseId).src = "http://media.buffalonews.com/static/images/frontpage/ticker-play.gif";
		TICKER.doRun = 0;			
	},
		
	playPause : function() {
		if (TICKER.doRun == "0") {
			TICKER.run();
			TICKER.doRun = 1;
		} else {
			TICKER.stop();		
		}
	},
		
	setContent : function() {
		TICKER.container.innerHTML=TICKER.content[TICKER.index].innerHTML;	
		TICKER.container.style.color = "rgb("+TICKER.endcolor[0]+", "+TICKER.endcolor[1]+", "+TICKER.endcolor[2]+")";
		var obj = TICKER.container.getElementsByTagName("A");
		if (obj.length > 0) {
			for (i=0;i<obj.length;i++) {
				obj[i].style.color = "rgb("+TICKER.endcolor[0]+", "+TICKER.endcolor[1]+", "+TICKER.endcolor[2]+")";
			}
  		}				
	},
		
	linkcolorchange : function(step) {
		var obj = TICKER.container.getElementsByTagName("A");
		if (obj.length > 0) {
			for (i=0;i<obj.length;i++) {
				obj[i].style.color = this.getstepcolor(step);
			}
  		}
	},
	
	colorfade : function(step) {
		if(step <= TICKER.maxsteps) {	
			TICKER.container.style.color = TICKER.getstepcolor(step);
			if (TICKER.fadelinks) {
				TICKER.linkcolorchange(step);
			}
			step++;
			TICKER.fadecounter = setTimeout("TICKER.colorfade("+step+")",TICKER.stepdelay);
		} else {
			TICKER.container.style.color = "rgb("+TICKER.endcolor[0]+", "+TICKER.endcolor[1]+", "+TICKER.endcolor[2]+")";
			if (TICKER.doRun == 1) {
				TICKER.t = setTimeout("TICKER.run()", TICKER.delay);
			} else {
				window.clearTimeout(TICKER.t);		
			}
		}  
	},

	getstepcolor : function(step) {
		var diff;
		var newcolor=new Array(3);
		for(var i=0;i<3;i++) {
			diff = (TICKER.startcolor[i] - TICKER.endcolor[i]);
			if(diff > 0) {
				newcolor[i] = TICKER.startcolor[i]-(Math.round((diff/TICKER.maxsteps))*step);
			} else {
				newcolor[i] = TICKER.startcolor[i]+(Math.round((Math.abs(diff)/TICKER.maxsteps))*step);
			}
		}
		return ("rgb(" + newcolor[0] + ", " + newcolor[1] + ", " + newcolor[2] + ")");
	},

	increment : function() {
		if (TICKER.index >= TICKER.maxCount - 1) {
			TICKER.index = 0;
		} else {
			if (TICKER.index < TICKER.content.length-1) {
				TICKER.index++;
			} else {
				TICKER.index = 0;
			}
		}
		TICKER.setContent();
		TICKER.stop();
	},

	decrement : function(event) {
		if (TICKER.index <= 0) {
			TICKER.index = TICKER.maxCount - 1;
		} else {
			TICKER.index--;
		}	
		TICKER.setContent();
		TICKER.stop();
	}

};