YAHOO.namespace('mzag.scrollpane');
YAHOO.namespace('mzag.scrollpaneManager');

YAHOO.mzag.scrollpaneManager=function()
{
  this.scrollpanes=new Array();
}
YAHOO.mzag.scrollpaneManager.prototype.add=function(scrollpane_)
{
  this.scrollpanes[scrollpane_.containerElementId]=scrollpane_;
}
YAHOO.mzag.scrollpaneManager.prototype.find=function(containerId_)
{
  return this.scrollpanes[containerId_];
}
YAHOO.mzag.scrollpane = function(containerElementId_, panelElementId_, step_, intervalTime_, delay_, stoppage_, direction_, showonstart_, stoponhover_, autosize_)
{

  this.DIRECTION_UP=1;
  this.DIRECTION_DOWN=2;
  this.DIRECTION_LEFT=3;
  this.DIRECTION_RIGHT=4;
  
  this.containerElementId=containerElementId_;
  this.panelElementId=panelElementId_;
  this.speed=step_;
  this.intervalTime=intervalTime_;
  this.delay=delay_;
  this.stoppage=stoppage_;
  this.direction=direction_;
  this.showonstart=showonstart_;
  this.stoponhover=stoponhover_;
  this.autosize=autosize_;
  this.isStopped=false;
};
YAHOO.mzag.scrollpane.prototype.pxFormat=function(string_){
    return parseInt(string_)+'px';
  }
YAHOO.mzag.scrollpane.prototype.init = function() {
    var Event = YAHOO.util.Event,
        Dom   = YAHOO.util.Dom,
        Region= YAHOO.util.Region;
  this.containerWidth=parseInt(Dom.getStyle(this.containerElementId, 'width'));
  this.containerHeight=parseInt(Dom.getStyle(this.containerElementId, 'height'));
  this.innerWidth=parseInt(Dom.getStyle(this.panelElementId, 'width'));
  if(isNaN(this.innerWidth))
  {
    region=Dom.getRegion(this.panelElementId);
    this.innerWidth=parseInt(region.right-region.left);
  }  
  this.innerHeight=parseInt(Dom.getStyle(this.panelElementId, 'height'));
  if(isNaN(this.Height))
  {
    region=Dom.getRegion(this.panelElementId);
    this.innerHeight=parseInt(region.bottom-region.top);
  }

  var panel=$(this.panelElementId);

  if(this.direction == this.DIRECTION_UP || this.direction == this.DIRECTION_DOWN)
  {
    if(this.direction == this.DIRECTION_UP)
    {
      this.setY(this.showonstart?0:this.containerHeight);
    }
    else
      this.setY(this.showonstart?(-1*this.innerHeight+this.containerHeight):(-1*this.innerHeight));
      
    if(this.autosize && this.containerWidth > this.innerWidth)
      this.setContainerWidth(this.innerWidth);
  }
  else
  {
    if(this.direction == this.DIRECTION_LEFT)
      this.setX(this.showonstart?0:this.containerWidth);
    else
      this.setX(this.showonstart?(-1*this.innerWidth+this.containerWidth):(-1*this.innerWidth));
      
    if(this.autosize && this.containerHeight > this.innerHeight)
      this.setContainerHeight(this.innerHeight);
  }
  
  var ths=this;
  
  if(!this.isStopped)
    setTimeout(function() {ths.animate();}, this.delay);
  
  if(this.stoponhover)
  {
    Event.addListener(this.containerElementId, 'mouseover', function()
    {
      ths.stopAnimation();
    });
    Event.addListener(this.containerElementId, 'mouseout', function(e)
    {
      if(!YAHOO.util.Dom.isAncestor(this, e.relatedTarget) && this!=e.relatedTarget)
        ths.startAnimation();
    });
  }
};

YAHOO.mzag.scrollpane.prototype.setX=function(x_)
{
  YAHOO.util.Dom.setStyle(this.panelElementId, 'left', this.pxFormat(x_));
}
YAHOO.mzag.scrollpane.prototype.getX=function()
{
  return parseInt(YAHOO.util.Dom.getStyle(this.panelElementId, 'left'));
}
YAHOO.mzag.scrollpane.prototype.setY=function(y_)
{
  YAHOO.util.Dom.setStyle(this.panelElementId, 'top', this.pxFormat(y_));
}
YAHOO.mzag.scrollpane.prototype.getY=function()
{
  return parseInt(YAHOO.util.Dom.getStyle(this.panelElementId, 'top'));
}
YAHOO.mzag.scrollpane.prototype.setContainerWidth=function(width_)
{
  YAHOO.util.Dom.setStyle(this.containerElementId, 'width', this.pxFormat(width_));
}
YAHOO.mzag.scrollpane.prototype.setContainerHeight=function(height_)
{
  YAHOO.util.Dom.setStyle(this.containerElementId, 'height', this.pxFormat(height_));
}
YAHOO.mzag.scrollpane.prototype.startAnimation = function()
{
  if(!this.isStopped)
    return;
  var ths=this;
  this.isStopped=false;
  this.isPaused=false;
  setTimeout(function(){ths.animate();}, ths.intervalTime);
} 
YAHOO.mzag.scrollpane.prototype.stopAnimation = function()
{
  this.isStopped=true;
  this.isPaused=false;
} 
YAHOO.mzag.scrollpane.prototype.pauseAnimation = function()
{
  if(!this.isStopped)
  {
    this.stopAnimation();
    this.isPaused=true;
  }
  else if(this.isPaused)
  {
    this.startAnimation();
   }
} 
YAHOO.mzag.scrollpane.prototype.checkDelay = function()
{
  if(this.stoppage > 0)
  {
    this.stopAnimation();
    var ths=this;
    setTimeout(function() {ths.startAnimation();}, this.stoppage);
  }
} 
YAHOO.mzag.scrollpane.prototype.animate = function() { 
  var Event = YAHOO.util.Event,
      Dom   = YAHOO.util.Dom,
      Region= YAHOO.util.Region;
        
  if(this.direction == this.DIRECTION_UP || this.direction == this.DIRECTION_DOWN)
  {
    var top=this.getY();
    
    if(this.direction == this.DIRECTION_UP)
    {
      top-=this.speed;
      if(top < -this.innerHeight)
      {
        top=this.containerHeight;
        this.checkDelay();
      }
    }
    else
    {
      top+=this.speed;
      if(top > this.containerHeight)
      {
        top=-this.innerHeight;
        this.checkDelay();
      }
    }
    
    this.setY(top);
    
  }
  else
  {
    var left=this.getX();
  
    if(this.direction == this.DIRECTION_LEFT)
    {
      left-=this.speed;
      if(left < -this.innerWidth)
      {
        left=this.containerWidth;
        this.checkDelay();
      }
    }
    else
    {
      left+=this.speed;
      if(left > this.containerWidth)
      {
        left=-this.innerWidth;
        this.checkDelay();
      }
    }
      
      
    this.setX(left);
  }
  
  if(!this.isStopped)
  {
    var ths=this;
    setTimeout(function(){ths.animate();}, ths.intervalTime);
  }
}
if(typeof(YAHOO.mzag.scrollpane.manager)=='undefined')
  YAHOO.mzag.scrollpane.manager=new YAHOO.mzag.scrollpaneManager();
