﻿/* THE FOLLOWING DEFINES A JAVASCRIPT OBJECT TO REPRESENT A TICKER CONTROL */

function getWidth(control)
{
    var width = 0;
    var count = 0;
    if (control.childNodes.length == 1 && control.childNodes[0].nodeType == 3) 
    {
        width += getControlWidth(control);
    }
    else
    {
        for (count;count < control.childNodes.length;count+=1) 
        {
            width += getWidth(control.childNodes[count]);
        }
    }
    return width;
}

function getControlWidth(control)
{
    var width = 0;
    var count = 0;
    if (window.getComputedStyle)
    {
        if(isNaN(parseInt(window.getComputedStyle(control, null).width)) == false) {width += parseInt(window.getComputedStyle(control, null).width)}
        else{width += control.offsetWidth}
        if(isNaN(parseInt(window.getComputedStyle(control, null).paddingLeft)) == false) {width += parseInt(window.getComputedStyle(control, null).paddingLeft)}
        if(isNaN(parseInt(window.getComputedStyle(control, null).paddingRight)) == false) {width += parseInt(window.getComputedStyle(control, null).paddingRight)}
        if(isNaN(parseInt(window.getComputedStyle(control, null).marginLeft)) == false) {width += parseInt(window.getComputedStyle(control, null).marginLeft)}
        if(isNaN(parseInt(window.getComputedStyle(control, null).marginRight)) == false) {width += parseInt(window.getComputedStyle(control, null).marginRight)}
    } 
    else if (control.currentStyle)
    {
                if(isNaN(parseInt(control.currentStyle.width)) == false) {width += parseInt(control.currentStyle.width)}
        else{width += control.offsetWidth}
        if(isNaN(parseInt(control.currentStyle.paddingLeft)) == false) {width += parseInt(control.currentStyle.paddingLeft)}
        if(isNaN(parseInt(control.currentStyle.paddingRight)) == false) {width += parseInt(control.currentStyle.paddingRight)}
        if(isNaN(parseInt(control.currentStyle.marginLeft)) == false) {width += parseInt(control.currentStyle.marginLeft)}
        if(isNaN(parseInt(control.currentStyle.marginRight)) == false) {width += parseInt(control.currentStyle.marginRight)}
    }
    return width;
}

//Define the object, this will be instantiated by each ticker control at runtime.
function tickerControl(id, speed, ticker, scroller) 
{
  this.scrollerWidth = getWidth(scroller);
  this.tickerWidth = getControlWidth(ticker);
  var intervalId = null;
  this.id = id; //the id of the control
  this.speed = speed; //the speed that we want to scroll across the page
  this.scroller = scroller; //the span object that moves across the screen as the scroller
  this.ticker = ticker; //the div control that contains the scroller  
    
  //function to set up the interval at which the scroll function gets called
  this.startInterval = function()
  {
    if(intervalId == null)
    {
      //set up to scroll at regular intervals - every 25ms
      var script = this.id + '.scroll();';
      intervalId = setInterval(script, 25);
    }
  }
  
  //function to stop the scrolling intervals
  this.stopInterval = function()
  {
    if (intervalId != null)
    {
      //stop the interval we set up earlier so that the control stops scrolling.
      clearInterval(intervalId);
      intervalId = null;
    }
  }
  
  //function to move the scroller across the screen, and re-position it once out of view to start over
  this.scroll = function()
  {
    //If the scroller is a long way out to the right of the ticker we want to bring it back so it appears quicker.
    //This could happen if the width of the window is shrunk and the ticker has a percentage width set.
    if (parseInt(this.scroller.style.left) > this.tickerWidth + 4)
    {
      this.scroller.style.left = this.tickerWidth + 4 + 'px';
    }
    //If its in the right place, move the scroller to the left an amount equal to the speed.
    this.scroller.style.left = parseInt(this.scroller.style.left) - this.speed + 'px';
    //If the scroller has moved out of the ticker on the left, reset it back to the right to start again.
    if (parseInt(this.scroller.style.left) + this.scrollerWidth < 0)
    {
      this.scroller.style.left = this.tickerWidth + 4 + 'px';
    }
  }
}

//add function to start the ticker scrolling
tickerControl.prototype.start = function()
{
  this.scroller.style.left = this.tickerWidth + 4 + 'px';
  this.startInterval();
}

//add function to stop the ticker scrolling
tickerControl.prototype.stop = function(intervalId)
{
  this.stopInterval();
}

/* THE FOLLOWING ARE FUNCTIONS REQUIRED FOR THE TICKER TO BEHAVE CORRECTLY */
//Function to activate stylesheet that allows the scrolling ticker to overflow the page.
function activateStylesheet(sheetref) 
{
    if(document.getElementsByTagName) 
    {
        var ss=document.getElementsByTagName('link');
    } 
    else if (document.styleSheets)
    {
        var ss = document.styleSheets;
    }
    for(var i=0;ss[i];i++ ) 
    {
        if(ss[i].href.toLowerCase().indexOf(sheetref.toLowerCase()) != -1)
        {
            ss[i].disabled = true;
            ss[i].disabled = false;
        }
    }
}

