
function _prerequisits()
{
  if ( typeof document.getElementById == "undefined" ) return false;
  if ( typeof document.createElement == "undefined" ) return false;
  if ( typeof window.setTimeout == "undefined" ) return false;
  if ( typeof max == "undefined" )
  {
    max = function ( min )
    {
      for ( var i = 1; i < arguments.length; ++i )
      {
        if ( arguments[i] > min ) min = arguments[i];
      }
      return min;
    };
  }
  
  return true;
}

if ( _prerequisits() )
{

  var Marquee = function Marquee() {
    var marquee = this.marquee = document.getElementById("marquee");
    if (!marquee) return;
    if (typeof marquee.appendChild == "undefined") return;
    this.marquee_width = this.slider_width = this.width_of(marquee);

    var inner = this.slider = [
        document.createElement("div"), document.createElement("div")
      ];
    inner[0].id = "marquee-slider-0";
    inner[1].id = "marquee-slider-1";
    marquee.innerHTML = "";
    marquee.appendChild(inner[0]);
    marquee.appendChild(inner[1]);

    var that = this;

    this.scroll_left = function() { return that._scroll_left(); };

    this.http = libhttp.request();
    this.http.onreadystatechange = function() {
      if (that.http.readyState == 4) that.load();
    };
    var url = "../rss/newsscroller.ashx?n1=" + (new Date()).getHours();

    this.http.open("GET", url, true);
    this.http.send(null);
  };

  Marquee.prototype = {

    /**
    interval is the (inverse) frame rate, 40 ms = 25 frames per second.
    */
    INTERVAL: 30 /* ms */,
    STEP: 1 /* pixel */,

    width_of: function(i) {
      if (i.clientWidth != "undefined") return i.clientWidth;
      if (i.offsetWidth != "undefined") return i.offsetWidth;
    },
    load: function() {
      this.slider[0].innerHTML = this.http.responseText;
      this.slider[1].innerHTML = this.http.responseText;
      this.left_slider = 0;
      this.slider[0].style.left = (this.slider_width) + "px";
      this.slider[1].style.left = (this.slider_width * 2) + "px";
      this.slider_width = 0;

      window.setTimeout(this.scroll_left, this.INTERVAL);
    },

    _date_to_ms: function(d) {
      return (
          d.getMilliseconds()
          + 1000.0 * d.getSeconds()
          + 60.0 * 1000.0 * d.getMinutes()
          + 60.0 * 60.0 * 1000.0 * d.getHours()
        );
    },

    _scroll_left: function() {
      if (!this.slider_width) {
        this.slider_width = max(this.marquee_width, this.width_of(this.slider[0]));

        var off = (this._date_to_ms(new Date()) / this.INTERVAL) % this.marquee_width
        this.slider_offset = off; //- this.marquee_width;
      }
      var left = this.left_slider;
      var right = (left + 1) % 2;
      this.slider_offset = this.slider_offset + this.STEP;
      if (this.slider_offset >= this.slider_width) {
        this.left_slider = left = right;
        right = (left + 1) % 2;
        this.slider_offset = 0;
      }
      //libutil.log( left, right, this.slider_offset );
      this.slider[left].style.left = (-this.slider_offset) + "px";
      this.slider[right].style.left = (this.slider_width - this.slider_offset) + "px";
      window.setTimeout(this.scroll_left, this.INTERVAL);
    },

    _end_of_prototype: "ie/jscript trailing comma bug"
  };
  
  add_onload( function() 
  { 
    //libutil.log.enabled = true;
    /* global */Marquee = new Marquee(); 
  });
}  





