var currentScrollSectionIndexes = new Array();

function findParentOfClass(anElement, aClassName)
{
  var result = null;
  if (anElement && anElement.className &&
      (anElement.className == aClassName))
    result = anElement;
  else if (anElement && anElement.parentNode &&
           (anElement.parentNode != anElement))
    result = findParentOfClass(anElement.parentNode, aClassName);
  return result;
}
//end findParentOfClass()

function findDescendantOfClass(anElement, aClassName)
{
  var result = null;
  if (anElement && anElement.className &&
      (anElement.className == aClassName))
    result = anElement;
  else if (anElement && anElement.hasChildNodes())
  {
    var children = anElement.childNodes;
    for (var i = 0; !result && (i<children.length); i++) 
    {
      var child = children[i];
      if (child.className == aClassName)
        result = child;
      else
        result = findDescendantOfClass(child, aClassName);
    }
  }
  return result;
}
//end findDescendantOfClass()

function findElementPosition(element)
{
  var elementPositionX = 0;
  var elementPositionY = 0;
  do
  {
    elementPositionX += (element.offsetLeft ? element.offsetLeft : 0);
    elementPositionY += (element.offsetTop  ? element.offsetTop  : 0);
  }while (element = element.offsetParent)

  return Array(elementPositionX, elementPositionY);
}
//end findElementPosition()

function getIndexInParent(currentElement, tagNameOfParent)
{
  var indexInParent = -1;
  if (currentElement.parentNode && (currentElement.parentNode != currentElement) && currentElement.parentNode.tagName)
  {
    var theParent = currentElement.parentNode;
    if (theParent.tagName.toLowerCase() != tagNameOfParent.toLowerCase())
      indexInParent = getIndexInParent(theParent, tagNameOfParent);
    else
    {
      var brothers = theParent.childNodes;
	  var nbNotMatchingTagNameElements = 0;
      for(var i = 0 ; (indexInParent < 0) && (i < brothers.length) ; ++i)
	  {
	    var brother = brothers[i];
		if (brother.tagName != currentElement.tagName)
		  ++nbNotMatchingTagNameElements;
		else
          indexInParent = (brothers[i] == currentElement) ? i : -1;
	  }
	  indexInParent -= nbNotMatchingTagNameElements;
    }
  }
  return indexInParent;
}
//end getIndexInParent()

function findChildrenOfClass(currentElement, aClassName)
{
  var result = new Array();
  if (currentElement.hasChildNodes())
  {
    var children = currentElement.childNodes;
    for(var i = 0 ; i<children.length ; ++i)
    {
      var child = children[i];
      if (child.className && (child.className == aClassName))
        result.push(child);
	}
  }
  return result;
}
//end findChildrenOfClass()

function scrollToMySection(currentElement)
{
  var scrollContainer = findParentOfClass(currentElement, 'scroll-container');
  scrollToSection(scrollContainer, getIndexInParent(currentElement, 'ul'));
}
//end scrollToMySection()

//scroll the page to the position of element "link"
function scrollToSection(scrollContainer, newSectionIndex)
{
  var indexOfCurrentSection = currentScrollSectionIndexes[scrollContainer.id] ?
                              currentScrollSectionIndexes[scrollContainer.id] : 0;
  if (newSectionIndex != indexOfCurrentSection)
  {
    var movies = scrollContainer.getElementsByTagName('OBJECT');
	for(var i = 0 ; i < movies.length ; ++i)
	{
	  var movie = movies[i];
	  var movieParent = movie.parentNode;
	  movie.getElementsByTagName('EMBED')[0].Stop();
	  movieParent.removeChild(movie);
	  var scripts = movieParent.parentNode.getElementsByTagName('SCRIPT');
	  var script = scripts[0].innerHTML.split("\n").join(" ");
	  var regexp = new RegExp("^.*QT_WritePoster_XHTML\\((.*)\\).*$","m");
	  var args   = script.replace(regexp, "$1").split(',');
	  args.push('', 'controller', 'false', 'autoplay', 'false', 'bgcolor',
	            'black','scale','aspect');
      regexp = new RegExp("^[ ]*'(.*)'[ ]*$");
	  for(var j = 0 ; j<args.length ; ++j)
	  {
	    if (args[j].match(regexp))
          args[j] = args[j].replace(regexp,"$1");
	  }
      QT_ReplaceWithPoster(args[0], args[1], movieParent, args[2], args[3], args[4],
	                       args[5], args[6], args[7], args[8], args[9],
						   args[10], args[11], args[12], args[13]);
	}//end for each movie
	
  
    currentScrollSectionIndexes[scrollContainer.id] = newSectionIndex;

    // Change the section highlight.
	var scrollToolbar   = findDescendantOfClass(scrollContainer, 'scroll-toolbar');
    var toolbarElements = scrollToolbar.getElementsByTagName('li');
    for(var i = 0 ; i<toolbarElements.length ; ++i)
    {
      var toolbarElement = toolbarElements[i];
      toolbarElement.className = (i == newSectionIndex) ? "active" : "inactive";
    }
      
    // Get the element we want to scroll, get the position of the element to scroll to
	var scrollContent   = findDescendantOfClass(scrollContainer, 'scroll-content');
	var sections = findChildrenOfClass(scrollContent, 'scroll-section');
	var newSection =sections[newSectionIndex];
    position = findElementPosition(newSection);

    // Get the position of the offset div -- the div at the far left.
    // This is the amount we compensate for when scrolling
    var offset = findDescendantOfClass(scrollContent, 'scroll-section');
    if (offset != "")
    {
      offsetPos = findElementPosition(offset);
      position[0] = position[0] - offsetPos[0];
    }

	var scrollScroller   = findDescendantOfClass(scrollContainer, 'scroll-scroller');
    scrollStart(scrollScroller, scrollScroller.scrollLeft, position[0], "horiz");
  }//end if (currentSection != link)
}

// Scroll the page using the arrows
function scrollArrow(currentElement, direction)
{
  var scrollContainer = findParentOfClass(currentElement, 'scroll-container');
  var indexOfCurrentSection = currentScrollSectionIndexes[scrollContainer.id] ?
                              currentScrollSectionIndexes[scrollContainer.id] : 0;
  var scrollToolbar   = findDescendantOfClass(scrollContainer, 'scroll-toolbar');
  var toolBarElements = scrollToolbar.getElementsByTagName('li');
  var indexOfNextSection = (direction == 'left') ?
    ((indexOfCurrentSection+toolBarElements.length-1)%toolBarElements.length) :
    ((indexOfCurrentSection+1)%toolBarElements.length);
  scrollToSection(scrollContainer, indexOfNextSection);
}
//end scrollArrow()

//
// Animated Scroll Functions
// Scrolls are synchronous -- only one at a time.
//

var scrollanim = {time:0, begin:0, change:0.0, duration:0.0, element:null, timer:null};

function scrollTimeTransformation(time, begin, change, duration)
{
  return -change/2 * (Math.cos(Math.PI*time/duration) - 1) + begin;
}
//end scrollTimeTransformation()

function scrollStart(elem, start, end, direction)
{
  //console.log("scrollStart from "+start+" to "+end+" in direction "+direction);

  if (scrollanim.timer != null) {
    clearInterval(scrollanim.timer);
    scrollanim.timer = null;
  }
  scrollanim.time = 0;
  scrollanim.begin = start;
  scrollanim.change = end - start;
  scrollanim.duration = 25;
  scrollanim.element = elem;
  
  if (direction == "horiz") {
    scrollanim.timer = setInterval("scrollHorizAnim();", 15);
  }
  else {
    scrollanim.timer = setInterval("scrollVertAnim();", 15);
  }
}

function scrollVertAnim()
{
  if (scrollanim.time > scrollanim.duration) {
    clearInterval(scrollanim.timer);
    scrollanim.timer = null;
  }
  else {
    move = scrollTimeTransformation(scrollanim.time, scrollanim.begin, scrollanim.change, scrollanim.duration);
    scrollanim.element.scrollTop = move; 
    scrollanim.time++;
  }
}

function scrollHorizAnim()
{
  if (scrollanim.time > scrollanim.duration) {
    clearInterval(scrollanim.timer);
    scrollanim.timer = null;
  }
  else {
    move = scrollTimeTransformation(scrollanim.time, scrollanim.begin, scrollanim.change, scrollanim.duration);
    scrollanim.element.scrollLeft = move;
    scrollanim.time++;
  }
}

