// Copyright (C) 2008 Orange Concept
var scrollHInterval=null;
var scrollActive=new Array();
var scrollElements=new Array();
function scrollGetElem(div)
{
      return document.getElementById ? document.getElementById(div) : document.all[div];
}

function scrollTicker() 
{
  //var indic=scrollGetElem('indic');
  for (var i = 0; i < scrollElements.length; i++)
    {
      if (scrollActive[i])
        {
          var theScrollDiv=scrollElements[i];
          
          var time = new Date().getTime();
          if (time <= theScrollDiv.t1) 
            {
              var elapsed = time - theScrollDiv.t0;
              var diff=Math.round((elapsed/theScrollDiv.anElemDT)*theScrollDiv.diff);
              if (diff>theScrollDiv.diff)
                diff=theScrollDiv.diff;
              var top=parseInt(theScrollDiv.baseTop)-diff;
              //indic.innerHTML="Move A diff="+diff;
              theScrollDiv.style.top=top+"px";
            }
          else if (time<theScrollDiv.t2)
            {
              var top=parseInt(theScrollDiv.baseTop)-theScrollDiv.diff;
              //indic.innerHTML="Move B diff="+theScrollDiv.diff;
              theScrollDiv.style.top=top+"px";
            }
          else
            {
              scrollStartMoveNext(theScrollDiv);
            }
        }
    }
//      alert(theScrollDiv.style.top);
}
function ScrollOnMouseOver()
{
  scrollActive[this.scrollIndex]=false;
}

function ScrollOnMouseOut()
{
  if (this.nextElement!=null)
    scrollActive[this.scrollIndex]=true;
}

function scrollInit(ScrollID,ElemDT,ElemWait)
{
  var aScrollElem=scrollGetElem(ScrollID);
  if (aScrollElem)
    {
      aScrollElem.scrollIndex=scrollElements.length;
      aScrollElem.onmouseover=ScrollOnMouseOver;
      aScrollElem.onmouseout=ScrollOnMouseOut;

      aScrollElem.anElemDT=ElemDT;
      aScrollElem.anElemWait=ElemWait;
      scrollStartMove(aScrollElem);  

      scrollElements[scrollElements.length]=aScrollElem;
      scrollActive[aScrollElem.scrollIndex]=true;
      if (scrollHInterval==null)
        scrollHInterval=setInterval("scrollTicker()",100);
    }
//alert(n_timeout);
}

function scrollStartMove(theScrollDiv)
{    
  var aTop=parseInt(theScrollDiv.style.top);
  var LUElem=firstChildTagOfType(theScrollDiv,'UL');
  if (LUElem)
    {
      aTop-=parseInt(LUElem.offsetTop);
/*      var LIElem=firstChildTagOfType(LUElem,'LI');
      if (LIElem)
        aTop-=parseInt(LIElem.offsetTop);*/
    }
  theScrollDiv.originalTop=aTop+"px";
  theScrollDiv.currentElement=null;
  scrollStartMoveNext(theScrollDiv);
}
function scrollStartMoveNext(theScrollDiv)
{    
  if (theScrollDiv.currentElement==null)
    {
      theScrollDiv.style.top=theScrollDiv.originalTop;
      theScrollDiv.firstElement=firstChildTagOfType(theScrollDiv,'LI');
      theScrollDiv.currentElement=theScrollDiv.firstElement;
      theScrollDiv.nextElement=theScrollDiv.currentElement;
    }
  else
    {
      if (theScrollDiv.nextElement==null)
        {
          theScrollDiv.style.top=theScrollDiv.originalTop;
          theScrollDiv.currentElement=theScrollDiv.firstElement;
          theScrollDiv.nextElement=theScrollDiv.currentElement;
        }
      else
        {
          theScrollDiv.currentElement=theScrollDiv.nextElement;
          theScrollDiv.nextElement=nextSiblingOfType(theScrollDiv.currentElement,'LI');
        }
    }
  if (theScrollDiv.firstElement)
    {
      theScrollDiv.baseTop=parseInt(theScrollDiv.originalTop)-(parseInt(theScrollDiv.currentElement.offsetTop)-parseInt(theScrollDiv.firstElement.offsetTop));
      var coordStart=theScrollDiv.currentElement.offsetTop;
      var coordStop;
      if (theScrollDiv.nextElement==null)
        coordStop=coordStart+theScrollDiv.currentElement.offsetHeight;
      else
        coordStop=theScrollDiv.nextElement.offsetTop;
      theScrollDiv.diff=coordStop-coordStart;
      theScrollDiv.t0 = new Date().getTime();
      theScrollDiv.t1 = theScrollDiv.t0 + theScrollDiv.anElemDT;
      theScrollDiv.t2 = theScrollDiv.t1+theScrollDiv.anElemWait;
    }
  else
    {
      scrollActive[theScrollDiv.scrollIndex]=false;
    }
}

function firstChildTagOfType(Elem,type)
{
  var anElem=Elem.firstChild;
  while(anElem)
    {
      if (anElem.tagName==type)
        {
          return anElem;
        }
      else
        {
          found=firstChildTagOfType(anElem,type);
          if (found)
            return found;
          else
            anElem=anElem.nextSibling;
        }
    }
  return null;
}

//Returns Elem next sibling type type, null otherwise
function nextSiblingOfType(Elem,type)
{
  var anElem=Elem.nextSibling;
  while(anElem)
    {
      if (anElem.tagName==type)
        {
          return anElem;
        }
      else
        {
          anElem=anElem.nextSibling;
        }
    }
  return null;
}

