﻿function IntervalEvent (srcID)
{
   return Intervals.GetObj(srcID).Step();
}

function CIntervals ()
{
   this.Objects = new Array();
   this.Add = function(srcObj, stepDelay)
   {
      var hInterval = -1;
      for (hInterval = 0; hInterval < this.Objects.length; hInterval++)
      {
         if (this.Objects[hInterval] == null)
         {
            this.Objects[hInterval] = {'Obj':srcObj, 'intvID' : this.Objects[hInterval].intvID = setInterval ("IntervalEvent(" + hInterval + ")", stepDelay)};
            return hInterval;
         }
      }
      this.Objects.push({'Obj':srcObj, 'intvID':0});
      this.Objects[hInterval].intvID = setInterval ("IntervalEvent(" + hInterval + ")", stepDelay);
      return (hInterval);
   };
   
   this.Remove = function(id)
   {
      clearInterval(this.Objects[id].intvID);
      if (id == this.Objects.length - 1)
         this.Objects.splice (id, 1)
      else
         this.Objects[id] = null;
   }
   
   this.GetObj = function (id)
   {
      return this.Objects[id].Obj;
   }
   
}
var Intervals = new CIntervals();


function CScrollMenu ()
{
   this.linear = function(stepNo, stepCount){return stepNo * (1.0/stepCount)};
   this.sinusIn = function(stepNo, stepCount){return Math.sin (1.570795 * (stepNo/stepCount))};
   this.sinusOut = function(stepNo, stepCount){return Math.cos (1.570795 * (stepNo/stepCount)) * (-1) + 1};
   this.sinusInOut = function(stepNo, stepCount){return (Math.cos (3.1459 * (stepNo/stepCount)) * (-1) + 1) / 2};

   this.AddEventElement = function (elementObj, eventStr, eventHandler)
   {
      if(elementObj.addEventListener)
      {
         if (eventStr.substr (0,2) == "on")
            eventStr = eventStr.substr (2).toLowerCase();
         elementObj.addEventListener(eventStr, eventHandler, false)
      }
      else
      {
         elementObj.attachEvent (eventStr, eventHandler);
      }
   }

   this.Init = CScrollMenu_Init;
   this.Start = CScrollMenu_Start;
   this.Stop = CScrollMenu_Stop;
   this.Step = CScrollMenu_Step;
   this.CalcStep = CScrollMenu_CalcStep;
   this.Transitions = this.sinusInOut;
   this.ScrollCallback = null;
   
   //private
   this.hInterv = -1;
   this.StepNo = 0;
   this.ScrollElement = null;
   this.scTime = 0;
   this.maxScroll = 0;
   
   //public
   this.Duration = 800;
   this.StepDelay = 20;
   this.ScrollElementID = "";
   this.CurrDirection = 1;
   this.ScrollMode = "y";
   this.HideAfterScroll = false;
   this.MaxScrollOffset = 0;
   this.SetPositionOnInit = false;
   this.DisableScrollOut = false;
}

function CScrollMenu_Init()
{
   if (this.hInterv != -1)
      Intervals.Remove(this.hInterv);
   if (!document.getElementById(this.ScrollElementID))
      return;

   this.ScrollElement = document.getElementById(this.ScrollElementID);
   if (this.ScrollMode == "y")
   {
      this.maxScroll = this.ScrollElement.offsetHeight * 1 + this.MaxScrollOffset;
      if (this.SetPositionOnInit)
         this.ScrollElement.style.top = ((this.maxScroll * -1) + "px");
   }
   else
   {
      this.maxScroll = this.ScrollElement.offsetWidth * 1 + this.MaxScrollOffset;
      if (this.SetPositionOnInit)
         this.ScrollElement.style.left = ((this.maxScroll * -1) + "px");
   }
}

function CScrollMenu_Start(forceScrollOut)
{
   if (!forceScrollOut)
      forceScrollOut = false;
   if ((this.CurrDirection < 0 || this.hInterv != -1) && this.DisableScrollOut && forceScrollOut == false)
      return;
   if (this.hInterv != -1)
   {
      var timer = new Date();
      this.scTime = timer.getTime() - (this.Duration - (timer.getTime() - this.scTime));
      this.CurrDirection *= -1;
      return;
   }

   var timer = new Date();
   this.scTime = timer.getTime();
   
   this.ScrollElement.style.visibility = 'visible';
   this.hInterv = Intervals.Add(this, this.StepDelay);
}

function CScrollMenu_Stop()
{
   Intervals.Remove(this.hInterv);
   
   if (this.CurrDirection < 0)
   {
      if (this.ScrollMode == "y")
         this.ScrollElement.style.marginTop = 0;//Rundungsfehler vermeiden
      else
         this.ScrollElement.style.marginLeft = 0;//Rundungsfehler vermeiden

      if (this.HideAfterScroll)
         this.ScrollElement.style.visibility = "hidden";
   }
   
   this.hInterv = -1;
   this.CurrDirection *= -1;
}

function CScrollMenu_Step()
{
   var timer = new Date();
   var scrollPos = 0;
   
   if (this.CurrDirection > 0)
      scrollPos = (this.Transitions(timer.getTime() - this.scTime, this.Duration) * this.maxScroll);
   else
      scrollPos = (this.Transitions(this.Duration - (timer.getTime() - this.scTime), this.Duration) * this.maxScroll);

   if (this.ScrollMode == "y")
      this.ScrollElement.style.marginTop = scrollPos + "px";
   else
      this.ScrollElement.style.marginLeft = scrollPos + "px";
   
   this.StepNo++;

   if (timer.getTime() >= (this.scTime + this.Duration) )
      this.Stop();
   if (this.ScrollCallback != null)
      this.ScrollCallback (scrollPos);
}

function CScrollMenu_CalcStep()
{

}


