/*
 * ticker.js, by angus@netscape.com
 * No formal support offered. 
 */


function swapTicker(ref) {
/*
 This function is called to swap the "clone" tape with the "real" tape.
 It is called whenever the active tape is about to run out.
 See docs for more info on this concept.
*/

 if (ref.activeTicker == ref.tickLayer) {
  ref.activeTicker = ref.tickLayerSub;
  ref.nonActiveTicker = ref.tickLayer;
  return true;
 }
 ref.activeTicker=ref.tickLayer;
 ref.nonActiveTicker=ref.tickLayerSub;
 return true;
}

function slideTick(ref,inc) {
/*
 This function is called using setInterval. It slides the ticker ever so
 slightly in each iteration. It performs checks to see if it should be 
 swapping the tape, and it also performs checks to make sure that it has
 the "backup" tape on the correct side of the "active" tape, in case the
 user switches gears by sliding the tape in the opposite direction.
*/
 ref.tickLayer.moveBy(inc,0);
 ref.tickLayerSub.moveBy(inc,0);

 if ((ref.activeTicker.left < (ref.realWidth*-1)) && (ref.velocity<0)) {
  ref.activeTicker.moveBy(ref.realWidth*2,0);
  swapTicker(ref);
 }
 if ((ref.activeTicker.left > ref.width) && (ref.velocity>0)) {
  ref.activeTicker.moveBy(ref.realWidth*-2,0);
  swapTicker(ref);
 }
 if ((ref.velocity>0)&&(ref.activeTicker.left>0) && 
	(ref.nonActiveTicker.left > (ref.activeTicker.left-ref.realWidth))) {
 		ref.nonActiveTicker.moveTo(ref.activeTicker.left-ref.realWidth,0);
	}
if ((ref.velocity<0)&&(ref.activeTicker.left<(0-ref.realWidth+ref.width)) && 
	(ref.nonActiveTicker.left < (ref.activeTicker.left+ref.realWidth))) {
 		ref.nonActiveTicker.moveTo(ref.activeTicker.left+ref.realWidth,0);
	}
}

function startTicker() {
/*
 this is the Ticker.start() method. It checks to make sure the user hasn't placed
 the ticker into stop mode before starting (because this is called from mouseover
 events also). It then optimizes the interval time difference and the movement per
 iteration to work out so that slideTicker is called every 15 or more seconds (this
 is to avoid really short timeout periods). It then fires off an interval to start
 the motion.
*/

 ref = this;
 if (!ref.velocity) ref=this.parent;
 if (!ref.onLoadFired) {
	if (ref.onLoad) ref.onLoad();
	ref.onLoadFired = true;
	} 
 if (ref.active) ref.stop();
 if (ref.stopped == true) return(false);
 var increment = 1000/Math.abs(ref.velocity);
 var amount = ref.velocity/Math.abs(ref.velocity);
 while (increment < 15) {
  amount *=2;
  increment *=2;
 }
 ref.active = setInterval(slideTick,increment,ref,amount);
 return(true);
}

function stopTicker() {
clearInterval(this.active);
}

function slowVelocity(e) {
/*
 Called whenever the mouse goes over the ticker tape. Stops the ticker,
 reduces velocity to 50%, and restarts the ticker. Only does this if the
 ticker motion is currently active. (Ticker.active is the interval ID)
 Routes off the event if its happening over an anchor.
*/

 if (e.target.constructor == window.Url) return routeEvent(e);
 if (this.parent.active) {
 this.parent.stop();
 this.parent.velocity *= .5;
 this.parent.start();
 return true;
}
}

function speedVelocity(e) {
/*
 Called whenever the mouse goes out of the ticker tape. Stops the ticker,
 increases velocity back up to 100%, and restarts the ticker. Only does this if the
 ticker motion is currently active. (Ticker.active is the interval ID)
 Routes off the event if its happening over an anchor.
*/


 if (e.target.constructor == window.Url) return routeEvent(e);
 if (this.parent.active) {
 this.parent.stop();
 this.parent.velocity *= 2;
 this.parent.start();
 return true;
}
}

function makeSubTicker() {
/*
 Called in the Ticker constructor phase, this creates the initial "clone"
 tape by instantiating a new layer.
*/

 ref = this.parent;
 ref.tickLayerSub = new Layer(ref.realWidth,ref.container);
 if (ref.velocity < 0) ref.tickLayerSub.left=ref.realWidth;
 if (ref.velocity > 0) ref.tickLayerSub.left=ref.realWidth*-1;
 ref.tickLayerSub.top=0;
 ref.tickLayerSub.clip.width = ref.realWidth;
 ref.tickLayerSub.clip.height = ref.height;
 ref.tickLayerSub.visibility="inherit";
 ref.tickLayerSub.src=ref.src;
 ref.tickLayerSub.onMouseOver = slowVelocity;
 ref.tickLayerSub.onMouseOut = speedVelocity;
 ref.tickLayerSub.parent = ref;
 ref.nonActiveTicker = ref.tickLayerSub;
 ref.tickLayerSub.onLoad=ref.start;
}

function makeTicker(ref) {
/*
 Called in the Ticker constructor phase, this creates the initial "active"
 tape by instantiating a new layer.
*/

 ref.tickLayer = new Layer(ref.realWidth,ref.container);
 ref.tickLayer.left=0;
 ref.tickLayer.top=0;
 ref.tickLayer.clip.width = ref.realWidth;
 ref.tickLayer.clip.height = ref.height;
 ref.tickLayer.visibility="inherit";
 ref.tickLayer.src=ref.src;
 ref.activeTicker = ref.tickLayer;
 ref.tickLayer.onLoad=makeSubTicker;
 ref.tickLayer.onMouseOver = slowVelocity;
 ref.tickLayer.onMouseOut = speedVelocity;
 ref.tickLayer.parent = ref;
}

function Ticker(left,top,width,height,src,realwidth,velocity) {
/*
 Object stuff. Creates references to all the necessary properties and
 methods, creates the container layer and then calls the functions to
 create the active tape, the clone tape, which in turn calls the 
 start() method here when everything has loaded sequentially. Sets initial
 values to some vars. 
*/

 this.src = src;
 this.left = eval(left);
 this.top = eval(top);
 this.width = eval(width);
 this.height = eval(height);
 this.realWidth= eval(realwidth);
 this.velocity = eval(velocity);
 this.verticalLock = false;
 this.velocityLock = false;
 this.velocityMinimum = null;
 this.velocityMaximum = null;
 this.onLoadFired = false;
 this.onLoad = null;
 this.start = startTicker;
 this.stop = stopTicker;
 this.stopped = false;
 this.container = new Layer(width)
 this.container.left = eval(left);
 this.container.top = eval(top);
 this.container.clip.width= eval(width);
 this.container.clip.height= eval(height);
 this.container.visibility = "show";
 this.container.parent = this;
 this.container.onLoad = makeTicker(this);
 this.container.document.parent = this.container;
}

