var IE= document.all ? 1: 0;

function Ticker(){
	this.FADE_DELTA= 3;				// The geometric rate of fade-in change (2-10 is good)
	this.FADE_INTERVAL= 200; 		// Time interval between fading steps (milliseconds)
	this.NEXT_ITEM_INTERVAL= 9000;  // Time interval between when an item is 100% and when it goes blank
	this.CLEAR_DURATION= 400;		// Time interval between items (while blank)	
	this.autoAnimTimer= null;		// The timing object
	this.hIndex= 0;					// Index of the current headline
	this.isPaused= false;
	this.firstRotation= false;		// For things we have to do the first time around


	//Property setters. No getters; adds overhead, no need.
	Ticker.prototype.setHeadlines= function(headlines){
		this.headlines= headlines;
	}

	Ticker.prototype.setNextButtonId= function(nextButtonId){
		this.nextButtonId= nextButtonId;
	}

	Ticker.prototype.setPreviousButtonId= function(previousButtonId){
		this.previousButtonId= previousButtonId;
	}
	
	Ticker.prototype.setContainerId= function(containerId){
		this.containerId= containerId;
	}
	
	Ticker.prototype.getContainer= function(){
		return (document.getElementById(this.containerId));
	}

	//loop does the actual fading
	Ticker.prototype.fadeIn= function (){
		var o= this.getOpacity();
		var delta= (this.getMaxOpacity()- o)/ this.FADE_DELTA;
	
		if(delta > .009){
			this.setOpacity(delta);
		 	this.autoAnimTimer= setTimeout('ticker.fadeIn()', this.FADE_INTERVAL);
		} else{
			if(this.firstRotation){
				this.autoAnimTimer= setTimeout('ticker.clearNewsItem();', 60000);
				this.firstRotation= false;
			} else {
				this.autoAnimTimer= setTimeout('ticker.clearNewsItem();', this.NEXT_ITEM_INTERVAL);
			}
		}
	}

	// clears the current item, makes for a nice pause in between
	Ticker.prototype.clearNewsItem= function(){
		this.resetOpacity();
		this.autoAnimTimer= setTimeout('ticker.changeNewsItem()', this.CLEAR_DURATION);
	}

	// populate the next item
	Ticker.prototype.changeNewsItem= function(){
		document.getElementById('count').innerHTML=
			(this.hIndex+1)+' of '+ (this.headlines.length);
		if(IE){
			this.getContainer().innerHTML= this.getHeadlineHTML();
			this.hIndex++;
			if(this.hIndex> this.headlines.length- 1) this.hIndex= 0;
		} else{
			for(var i= 0; i< this.headlines.length; i++){
				document.getElementById('tickerItem_'+i).style.visibility=  'hidden';
			}			

			document.getElementById('tickerItem_'+this.hIndex).style.visibility=  'visible';
			
			this.hIndex++;
			if(this.hIndex> this.headlines.length- 1) this.hIndex= 0;
		}
		
		if(this.isPaused){
			this.revealCurrent();
			return;
		} else{
			this.fadeIn();
		}
	}

	//	the manual override goes forward/back
	Ticker.prototype.manualControl= function(direction){
		window.clearTimeout(this.autoAnimTimer);
	
		if(direction && (direction== 'pause')){
			if(this.isPaused){
				this.isPaused= false;
			} else{
				this.isPaused= true;
				this.revealCurrent();
				return;
			}
		}
		if(direction && (direction== 'previous')){
			this.hIndex-=2;
			if(this.hIndex< -1){this.hIndex= this.headlines.length-2};
			if(this.hIndex< 0){this.hIndex= this.headlines.length-1};
		}
		this.clearNewsItem();
	}
	
	Ticker.prototype.setOpacity= function(delta){
		if(IE){
			var o= parseFloat(this.getContainer().filters.alpha.opacity);
			
			var newO= o+ (delta*100)
			this.getContainer().filters.alpha.opacity= newO;
		} else{
			var o= parseFloat(this.getContainer().style.opacity);
			this.getContainer().style.opacity= o+ delta;
		}
	}
	
	Ticker.prototype.resetOpacity= function(){
		if(IE){
			this.getContainer().filters.alpha.opacity= 0;
		} else{
			this.getContainer().style.opacity= 0;			
		}
	}

	Ticker.prototype.revealCurrent= function(){
		if(IE){
			this.getContainer().filters.alpha.opacity= 100;
		} else{
			this.getContainer().style.opacity= 1;			
		}
	}	
	
	//	gets the current opacity of the item
	Ticker.prototype.getOpacity= function(){
		var o;
		if(IE){
			o= parseFloat(this.getContainer().filters.alpha.opacity)/ 100;
		} else{
			o= parseFloat(this.getContainer().style.opacity);
		}
		return (o);
	}
	

	//	returns the correct maximum opacity setting
	Ticker.prototype.getMaxOpacity= function(){
		return(1);
	}
	
	//	the headline html
	Ticker.prototype.getHeadlineHTML= function(targetIndex){
		var i= targetIndex ? targetIndex : this.hIndex;
		var h= this.headlines[i];
		
		var s= '<div style="background:url(/images/bg_homepageRhsBot.gif); width:344px;	height:171px;  background-repeat:repeat-x; background-position:top left;">'+
			'<p style="padding:25px 25px 0px 25px;">'+ h[0]+ '</p>'+
			'<em style="font-size:11px; padding:10px 20px 0px 0px;">'+h[1]+'</em></div>';
			
			
		return s;
	}
	
	//creates the content that will be toggled. replaces old 
	Ticker.prototype.initContent= function(){
		//fix for firefox/flash/refresh bug. 
		//right now it's !ie, but it's easier than guessing
		//which mozilla browsers have the flash conflict.
		if(!IE){
			var s='<div style="position:relative; width:350px; height:171px;">';
			
			for(var i=0; i< this.headlines.length; i++){
				s+= '<div id="tickerItem_'+i+'" style="visibility:hidden;position:absolute;top:0px;width:350px;padding:0px;height:171px; text-align=left;">'+
					this.getHeadlineHTML(i)+
					'</div>';
			}s+="</div>";
	
			this.getContainer().innerHTML= s;
		}
		this.changeNewsItem();
	}
}





