// do nothing yet

var rssScrollers = new Object();

function rssScroller(name, speed)
{
  if (rssScrollers[name]) return;
  this.name = name;
  this.speed = speed?5000:speed;
  this.container = document.getElementById(name);
  if (!this.container) return false;
  
  rssScrollers[this.name] = this;
  
  if (!speed) speed = 2000;
  
  this.ul = this.container.getElementsByTagName('ul')[0];
  this.ul.setAttribute('id', name + '-ul');
  this.ul.style.position = 'absolute';

  this.calculateStops();
  
  // wrap the container in another container so we can do stuff
  var obj = this.container;
  var div = document.createElement('div');
  this.container.parentNode.replaceChild(div, this.container);
  div.setAttribute('id', name + '-container');
  div.appendChild(obj);
  div.appendChild(this.createTimer());
  div.appendChild(this.createPages());

  
  this.ss = new StyleSlider(name + '-ul', 'top');
  
  this.ss.startValue = 0;
  this.ss.endValue = 0;
  this.ss.slideMode = 'easeOut';
  this.ss.steps = 20;
  this.ss.length = 350;

  this.timer_ss = new StyleSlider(name + '-timer', 'width');
  this.timer_ss.styleUnit = '%';
  this.timer_ss.startValue = 0;
  this.timer_ss.startValue = 100;
  this.timer_ss.steps = 100;
  this.timer_ss.length = speed;

  this.container.style.display = 'block';
  
  this.calculateStops();
  this.curStop = 0;
  
  this.jumpTo(0);
}

rssScroller.prototype.calculateStops = function()
{
  this.stops = new Array();
  var lis = this.ul.getElementsByTagName('li');
  for (var i = 0; i < lis.length; i++)
  {
    this.stops[i] = findPos(lis[i], false)[1];
  }
}

rssScroller.prototype.advance = function()
{
  this.curStop = (this.curStop + 1) % this.stops.length;

  var start = this.ss.endValue;
  var end = this.stops[this.curStop] - this.stops[0];
  this.ss.startValue = start;
  this.ss.endValue = -1 * end;
  this.ss.startSlide();
  
  this.resetTimer();
  this.updatePages();

}

rssScroller.prototype.jumpTo = function(stop)
{
  this.curStop = stop;

  var start = this.ss.endValue;
  var end = this.stops[this.curStop] - this.stops[0];
  this.ss.startValue = start;
  this.ss.endValue = -1 * end;
  this.ss.startSlide();

  this.resetInterval();
  this.resetTimer();
  this.updatePages();
}

rssScroller.prototype.resetTimer = function()
{
  this.timer_ss.startSlide();
}

rssScroller.prototype.createTimer = function()
{
  var div = document.createElement('div');
  div.setAttribute('id', this.name + '-timer');
  div.style.width = '0';
  return div;
}

rssScroller.prototype.createPages = function()
{
  var div = document.createElement('div');
  div.setAttribute('id', this.name + '-pages');
  var html = '';
  for (var i = 0; i < this.stops.length; i++)
    html += '<a href="#" id="' + this.name + '-pages-' + i + '" onmousedown="rssScrollers[\'' + this.name + '\'].jumpTo(' + i + '); return false;" onclick="this.blur(); return false;">' + (i + 1) + '</a>';
  div.innerHTML = html;
  return div;
}

rssScroller.prototype.resetInterval = function()
{
  clearInterval(this.interval);
  this.interval = setInterval('rssScrollers[\'' + this.name + '\'].advance()', this.speed);
}

rssScroller.prototype.updatePages = function()
{
  for (var i = 0; i < this.stops.length; i++)
  {
    var anchor = document.getElementById(this.name + '-pages-' + i);
    anchor.className = (i == this.curStop)?'selected':'';
  }
}

function findPos(obj, recurse) {
	var curleft = curtop = 0;
  if (obj.offsetParent) {
    do {
      curleft += obj.offsetLeft;
      curtop += obj.offsetTop;
      
    } 
    while (obj = obj.offsetParent && recurse);
  }
	return [curleft,curtop];
}


