var eCount = 1;
var direction = 1; // FORWARD!;
function moveCarousel(element, text, qty, maxLen)
{
  if (eCount == 1 && direction == 1)
  {
    // We're at the beginning and we're going forward, keep going forward
    direction = 1;
    // alert("Initial start!  At first element, going foward!");
  }
  else if (eCount == 1 && direction == 0)
  {
    // We are going backwards, and we are back at the first element, start going forward again
    direction = 1;
    // alert("Was going backward, at first element, now going foward!");
  }
  else if (eCount < qty && direction == 1)
  {
    // We're less than the number of elements keep going forward
    direction = 1
    // alert("Not at last element yet.  Keep going forward!");
  }
  else if (eCount == qty && direction == 1)
  {
    // We're at the last element and we were going forward, go backward now
    direction = 0
    // alert("At last element.  Now start going backward!");
  }
  else if (eCount > 1 && direction == 0)
  {
    // We're still going backwards and we're not at the first element yet, keep going backwards
    direction = 0;
    // alert("Not at first element yet.  Keep going backward!");
  }
  // alert("on element " + eCount + " total elements:" + qty + " Direction (1 forward, 0 backward):" + direction);
  maxLen =  ( maxLen ) ? maxLen : 1 ;
  var carousel = document.getElementById(element);
  var imgs = carousel.getElementsByTagName('div');
  var total = imgs.length;

  var width = $(imgs[0]).readAttribute('width');

  var x = ($(element).getStyle('left') == null) ? '0px' : $(element).getStyle('left');
  x = String(x.substring(0, (x.length - 2)));

  if (direction == 1) 
  {
    var image = (x == 0) ? 2 : ((Math.abs(x) / width) + 2);
    eCount++;
  } 
  else if (direction == 0) 
  {
    var image = (Math.abs(x) / width);
    eCount--;
  } 
  else 
  {
    return false;
  }
  if (image == 0)
  {
    return false;
  }
  else if (!$(imgs[image - 1])) 
  {
    return false;
  }
  if (direction == 1) 
  {
    if (x > -(width * (total - maxLen))) 
    {
       new Effect.MoveBy(element, 0, 0 , { x: -width, y: 0, duration: 0.3,  transition: Effect.Transitions.sinoidal});
      // $(text).update($(imgs[image - 1]).readAttribute('title'));
       return true;
    }
  } 
  else if (direction == 0) 
  {
    if (x < 0) 
    {
       new Effect.MoveBy(element, 0, 0 , { x: width, y: 0, duration: 0.3,  transition: Effect.Transitions.sinoidal});
      // $(text).update($(imgs[image - 1]).readAttribute('title'));
       return true;
    }
  } 
  else 
  {
    return false;
  }
  return false;
}

function carouselTimer(qty, duration)
{
  setInterval("moveCarousel('carousel','carousel-text'," + qty + ");", duration);
}