﻿function getObjectsByTagAndClass(tag, cls)
{
   var array = document.getElementsByTagName(tag);
   var output = new Array();
   var matches = 0;
   for(var i = 0; i < array.length; i ++ )
   {
      var parts = array[i].className.split(' ');
      for(var j = 0; j < parts.length; j ++ )
      {
         if(parts[j] == cls)
         {
            output[matches ++ ] = array[i];

         }
      }
   }
   return output;
}

var sPanel = new Object();
sPanel = function (tag, cls)
{
    // Properties
    this.SCROLL_HEIGHT = 19;
    this.TRANSITION_SPEED = 0.3;
    this.INTERVAL = 10;  
    // Varables
    var index = 0;
    var pos = 0;
    var timer = null;
    var previous = null;
    var panels = new Array();
    panels = getObjectsByTagAndClass(tag, cls);
    // Functions
    this.renderPanel = function()
    {
        if (panels.length > 1)
        {
            for (var i = 0; i <= panels.length - 1; i++)
            {
                if (panels[i])
                {
                    new Effect.MoveBy( panels[i], pos, 0,{duration : this.TRANSITION_SPEED});
                }
            }
            
            var panelInstance = this;
            previous = index;
            timer = setInterval(function(){panelInstance.cyclePanel()}, (this.INTERVAL * 1000));
        }
    }
    
    this.cancelAutoRun = function()
    {
        if(timer != "undefined")
        {
            clearInterval(timer);
        }
    }
    
    this.cyclePanel = function()
    {
        this.cancelAutoRun();
        pos = -this.SCROLL_HEIGHT;
        index = (index < panels.length-1) ? index + 1 : 0;
        
        if(previous != null)
        {
            new Effect.MoveBy( panels[previous], ((panels.length) * this.SCROLL_HEIGHT), 0,{duration : 0});
        }
        
        this.renderPanel();
    }
    
    this.load = function()
    {
        pos = pos - this.SCROLL_HEIGHT;
        this.renderPanel();
        
    }
}


















