
function Scroller(items) {
  this.speed = 35;
  this._slowMode = false;
  document.ivScroller = this;

  this.SupportFixed = function() {
    // ie le 6 doesnt
    var div = document.createElement('DIV');
    div.style.cssText = 'position:fixed; top:0px;';
    document.body.appendChild(div);
    var result = div.offsetTop==0;
    div.parentNode.removeChild(div);
    return result;
  }

  this.MakePadding = function() {
    if (!this.scrollDiv) return;
    var div = document.createElement('DIV');
    height = this.scrollDiv.parentNode.offsetHeight;
    //alert(height);
    div.style.cssText = 'height:'+height+'px;';
    document.body.appendChild(div);
  }

  this.MouseOver = function() {
    this.closeDiv.style.visibility = '';
    this._slowMode = true;
  }
  this.MouseOut = function() {
    this.closeDiv.style.visibility = 'hidden';
    this._slowMode = false;
  }

  this.Close = function() {
    //this._cntr.style.display = 'none';
    var div = this._cntr;
    div.parentNode.removeChild(div);
    ivSetCookie('iv_scroller', '1', 0);
  }

  this.Create = function(items) {
    var cntr = document.createElement('DIV');
    this._cntr = cntr;
    
    cntr.id = 'iv_scroller';
    cntr.onmouseover = function() {document.ivScroller.MouseOver()};
    cntr.onmouseout  = function() {document.ivScroller.MouseOut()};

    var closeDiv = document.createElement('div'); // bo ten sam padding???
    this.closeDiv = closeDiv;
    closeDiv.className = 'close';
    closeDiv.style.cssText = 'z-index:100; cursor:pointer; visibility:hidden;';
    closeDiv.style.position = this.SupportFixed() ? 'fixed' : 'absolute';
    closeDiv.innerHTML = 'zamknij [x]';
    closeDiv.onclick = function() {document.ivScroller.Close()};
    cntr.appendChild(closeDiv);

    // TODO: isiele6
    if (this.SupportFixed()) {
      cntr.style.cssText = 'z-index:99; position:fixed; bottom:0px; left:0px; overflow:hidden; width: 100%';
    } else {
      // IE LE 6
      cntr.style.cssText = 'z-index:99; position:absolute; bottom:0px; left:0px; overflow:hidden; width: 100%; padding-bottom: 1px;';
      window.attachEvent('onscroll',
        function() {
          cntr.style.bottom = "0px";
          cntr.style.bottom = "-1px";
        }
      );
      window.attachEvent('onresize',
        function() {
          cntr.style.bottom = "0px";
          cntr.style.bottom = "-1px";
        }
      );
    }

    var scrollDiv = document.createElement('DIV');
    this.scrollDiv = scrollDiv;
    scrollDiv.style.cssText = 'position:relative; width:29999px';
    cntr.appendChild(scrollDiv);

    // block
    var block = document.createElement('div');
    block.style.cssText = 'float:left';
    scrollDiv.appendChild(block);

    html = '';
    for (var i=0; i<items.length; i++) {
      var item = items[i];
      //html += '<a onfocus="this.blur()" href="'+unescape(item.link)+'">'+unescape(item.title)+'<'+'/a>';
      html += '<span class="a" onclick="location.href=\''+unescape(item.link)+'\'">'+unescape(item.title)+'<'+'/span>';
      html += '<span>&#149;<'+'/span>';
      //html += '<a href="'+item.link+'">'+item.title+'<'+'/a>&nbsp; &#149; &nbsp;';
    }
    block.innerHTML = html;

    document.body.appendChild(cntr);
    cntr.style.visibility = 'hidden';

    scrollDiv._left = cntr.offsetWidth;
    scrollDiv.style.left = scrollDiv._left+'px';

    var count = Math.ceil( screen.availWidth / block.offsetWidth );
    //var count = Math.ceil(document.body.clientWidth/wx);
    while (count--) {
      block.parentNode.appendChild( block.cloneNode(true) );
    }

    cntr.style.visibility = '';
  }

  this.Scroll = function() {
    var scroll = this.scrollDiv;
    var block = scroll.firstChild;
    scroll._left -= this._slowMode ? 1 : 2;
    if (block.offsetWidth < -scroll._left) {
      var parent = block.parentNode;
      parent.removeChild(block);
      parent.appendChild(block);
      scroll._left += block.offsetWidth;
    }
    scroll.style.left = scroll._left+'px';
    setTimeout( function(){document.ivScroller.Scroll()}, this.speed );
  }

  this.Run = function(items) {
    setTimeout(
      function() {
        document.ivScroller.MakePadding();
        document.ivScroller.Scroll();
      }, 100);
    //this.MakePadding(); this.Scroll();
  }
  
  this.Create(items);
};

function ivScroller(items, speed) {
  if (!ivGetCookie('iv_scroller') && items.length) {
    var obj = new Scroller(items);
    if (speed) obj.speed = speed;
    //obj.MakePadding();
    obj.Run();
  }
}
