﻿// Assumes gtHandler.js is included
var gtDelayHere = 1;  // Use as a marker


// Use the Javascript closeure-mechanism to bundle a function and its this-pointer as a new function
function createObjectCallback(func, object)
{
  var me = object;
  function callFuncOnObject()
  {
    func.call(me);
  } // end callFuncOnObject()
  return callFuncOnObject;
} // end callOnLoaded()



function gtTickerItem(img, title, text, href, target, ypos)
{
  this.m_img      = img;
  this.m_title    = title;
  this.m_text     = text;
  this.m_href     = href;
  this.m_target   = target;
  this.m_height   = 0;
  this.m_ypos     = ypos;
  this.m_element  = null;
} // end gtTickerItem()



function createStylePositionString(l, t, w, h)
{
  return "left:" + l + "px; top:" + t + "px; width:" + w + "px; height:" + h + "px;"
} // end createStylePositionString()

  

function gtBeScroller(e, name)
{
  // -------------------------------------------------------------------------
  // Methods
  // -------------------------------------------------------------------------
  e.addTickerItem = function addTickerItem(img, title, text, href, target)
  {
    var ti = new gtTickerItem(img, title, text, href, target, this.m_tickers.length);
    this.m_tickers.push(ti);
  }; // end addTickerItem()

  

  e.render = function render()
  {
    var w = this.m_width  - 2 * this.m_margin_left;
    var h = this.m_height - 2 * this.m_margin_top;
    var inner = "<div id='divMargin" + this.m_name + "' style='position:absolute; overflow:hidden; " +
      createStylePositionString(this.m_margin_left, this.m_margin_top, w, h) +
      "'>";
    inner += "<div id='divScroller" + this.m_name + "' style='position:absolute;" + createStylePositionString(0, 0, w, h) + "'>";
    var a_style = " style='position:absolute; visibility:hidden; left:0; top:0; width:" + (w - 2 * this.m_margin_left) + "px;'";
    for (var i = 0; i < this.m_tickers.length; i++)
    {
      var t = this.m_tickers[i];
      var entry = "<div id='a" + i + this.m_name + "'" + a_style + ">";
      entry += "<img class='gtIcon' src='" + t.m_img + "'/>&nbsp;";
      entry += "<span class='gtTickerTitle'>" + t.m_title + "</span><br/>";
      entry += "<a class='gtTicker' href='" + t.m_href + "'>" + t.m_text;
      entry += "</a></div>";
      inner += entry;
    }
    inner += "</table>"
    this.innerHTML = inner + "</div></div>";
    
    setTimeout(createObjectCallback(this.onLoaded, this), 1000);
  }; // end render()
  
  
  
  e.onLoaded = function onLoaded()
  {
    for (var i = 0; i < this.m_tickers.length; i++)
    {
      var a = gtGetElementByName("a" + i + this.m_name);
      a.style.visibility = "visible";
      gtSetTop(a, this.m_total_height);
      
      this.m_tickers[i].m_element = a;
      this.m_tickers[i].m_height  = a.offsetHeight;
      this.m_delay_pos[this.m_total_height] = gtDelayHere;
      this.m_total_height += this.m_tickers[i].m_height + this.m_line_spacing;
    }

    this.m_div_scroller = gtGetElementByName("divScroller" + this.m_name);
    this.m_cur_top = this.m_height;

    this.onTimerTick();
  }; // end onLoaded()
  
  
  e.onTimerTick = function onTimerTick()
  {
    if (!this.m_cursor_inside)
    {
      this.m_cur_top--;
      if (this.m_cur_top < -this.m_total_height)
        this.m_cur_top = this.m_height;

      gtSetTop(this.m_div_scroller, this.m_cur_top);
    }

    if (this.m_delay_pos[-this.m_cur_top] == gtDelayHere)
      setTimeout(createObjectCallback(this.onTimerTick, this), this.m_long_delay);
    else
      setTimeout(createObjectCallback(this.onTimerTick, this), e.m_short_delay);
  }; // end onTimerTick()


  // -------------------------------------------------------------------------
  // Member variables
  // -------------------------------------------------------------------------
  e.m_name          = name;
  e.m_tickers       = new Array();
  e.m_cursor_inside = false;
  e.m_margin_top    = 4;
  e.m_margin_left   = 2;
  e.m_line_spacing  = 10;
  e.m_width         = gtGetWidth(e);
  e.m_height        = gtGetHeight(e);
  e.m_total_height  = e.m_margin_top; // Start adding heights from the top margin
  e.m_delay_pos     = new Array();
  e.m_cur_top       = 0;
  e.m_long_delay    = 3000;
  e.m_short_delay   = 35;
  e.onmouseover     = function() { e.m_cursor_inside = true; }
  e.onmouseout      = function() { e.m_cursor_inside = false; }
//  e.style.
} // end gtBeScroller()