// newsTicker Object
// =============================================== usage:
// 1. create HTML
//    - it MUST have two elements: xxxx_link and xxxx_text where xxxx is the parameter when initializing the object:
// 2. Initialize the object:
//    - var ticker = new newsTicker('xxxx');
// 3. Add some content to the object:
//    - ticker.addheadlines("some%20h%quot;adline%201", "link1");
//    - ticker.addheadlines("some headline 2", "link2");
//    - repeat as many times as you want...
//    - make sure the text content is escaped!
// 4. run the ticker
//    - ticker.playitem();
// 5. to control the ticker you have to call below links:
//    - <a href="javascript:ticker.previtem();">prev</a>
//    - <a href="javascript:ticker.pauseitem();">pause</a>
//    - <a href="javascript:ticker.nextitem();">next</a>
// All HTML is up to you... as long as the code is embedded and two IDs are declared.
// You can temporarily switch the thing off by adding ?ticker=off to the query string

function newsTicker(target_div_id)
{
   // define properties
   var headlines      = new Array();
   var chartimeout    = 50;
   var storytimeout   = 2000;
   var targetdivid    = target_div_id;
   var currentitem    = null;
   var charLimit      = 40;

   // check the above config
   if (checkconfig()==false)
   {
       document.getElementById(target_div_id).style.height="0px;";
       document.getElementById(target_div_id).style.display="none";
       return false;
   }


   // If we are sure the config is right we can continue with setting dependending objects
   var targettext     = document.getElementById(targetdivid+"_text");
   var targetlink     = document.getElementById(targetdivid+"_link");

   // flags
   var currentitem    = 0;
   var pause          = false;
   var chartimer      = 0;
   var storytimer     = 0;
   var storedtimer    = 0;

   // define public methods
   this.addheadlines  = addheadlines;
   this.pauseitem     = pauseitem;
   this.playitem      = playitem;
   this.nextitem      = nextitem;
   this.previtem      = previtem;
   this.typeheadline  = typeheadline;
   this.drawcharacter = drawcharacter;
   this.setCharLimit  = setCharLimit;

   // define public properties
   // Not applicable but it would be this.headlines = headlines;

   // define subobjects
   this.targettext = targettext;
   this.targetlink = targetlink;

   function checkconfig()
   {
      var errors = new Array();
      if (document.getElementById(targetdivid+"_link")==null)
      {
          errors[errors.length]="Link <span> within Target DIV ID is not valid";
      }
      if (document.getElementById(targetdivid+"_text")==null)
      {
          errors[errors.length]="Text <span> within Target DIV ID is not valid";
      }

      // any errors? Print them out
      if (errors.length>0)
      {
          alert("There were errors in the configuration: \n\n"+errors.join('\n'));
          return false;
      }
   }

   function addheadlines(headline,url)
   {
      var someheadline            = new Object();
      someheadline.headline       = headline;
      someheadline.url            = url;
      headlines[headlines.length] = someheadline;
   }

   function pauseitem(obj)
   {
       if (pause == false)
       {
           clearTimeout(storytimer);
           pause = true;
       }
       else
       {
           nextitem();
       }
   }

   function playitem()
   {
       if (pause==false)
       {
           typeheadline();
       }
   }

   function typeheadline()
   {
       // populate the obvious bits
       targetlink.href = headlines[currentitem].url;
       drawcharacter(0);
   }

   function drawcharacter(pos)
   {
        // shorten the calls a bit
        var wholetext   = headlines[currentitem].headline;
        var wholelength = headlines[currentitem].headline.length;

        // Unfortunately we need to check for the escape characters here...
        if (wholetext.substr(pos,1)=="\\")
        {
           // this char is an escape char - pick one more character
           pos=pos+2;
        }
        else
        {
           // it is not escape char so pick just one char
           pos=pos+1;
        }

        // If the newstring is longer than, let's say charLimit characters there is a chance it will not fit the ticker div... so we will strip it down
        // to last charLimit characters
        if (pos > charLimit)
        {
            var newstring = wholetext.substr(pos-charLimit,charLimit);
        }
        else
        {
            var newstring = wholetext.substr(0,pos);
        }

        targettext.innerHTML = newstring;
        if (pos > wholelength)
        {
             if (pause==false)
             {
                 storytimer = setTimeout("ticker.nextitem()",storytimeout);
             }
        }
        else
        {
            chartimer = setTimeout("ticker.drawcharacter("+pos+")",chartimeout);
        }
   }

   function nextitem()
   {
       if (currentitem==headlines.length-1)
       {
          currentitem=0;
       }
       else
       {
          currentitem++;
       }
       pause=false;
       clearTimeout(chartimer);
       playitem();
   }

   function previtem()
   {
       if (currentitem==0)
       {
          currentitem=headlines.length;
       }
       else
       {
          currentitem--;
       }
       pause=false;
       clearTimeout(chartimer);
       playitem();
   }

   function setCharLimit(limit)
   {
       charLimit = limit;
   }
}
