// Copyright 2009 Google Inc.  All Rights Reserved.

/**
 * @fileoverview Supporting code for home page features scoller.
 * @author Michael Rigoli
 */

/**
 * Changes the state of the arrow icons depending what slides are showing.
 * @param {number} cp Represents current slide position.
 * @param {number} sw Represents slide width.
 * @param {number} nos Represents number of slides.
  */
function changeScrollerArrow(cp, sw, nos) {
  var arrowLoc = '/intl/en_ALL/analytics/images/icons/';
  var arrowLeft = document.getElementById('ga-scroller-arrow-left');
  var arrowRight = document.getElementById('ga-scroller-arrow-right');

  if (cp == 0) {
    arrowLeft.src = arrowLoc + 'ga-arrow-left-off.gif';
    arrowRight.src = arrowLoc + 'ga-arrow-right.gif';
  } else if (cp == (nos - 2) * sw) {
    arrowLeft.src = arrowLoc + 'ga-arrow-left.gif';
    arrowRight.src = arrowLoc + 'ga-arrow-right-off.gif';
  } else {
    arrowLeft.src = arrowLoc + 'ga-arrow-left.gif';
    arrowRight.src = arrowLoc + 'ga-arrow-right.gif';
  }
}

/**
 * Moves a sliding div back and forth to display different feature segments.
 * @param {string} direction Supplied in function call to set direction of
 * movement.
  */
function moveItem(direction) {
  var increment = 10;
  var slideWidth = 280;
  var numberOfSlides = 8;
  var slideStyle = document.getElementById('ga-slide-holder').style;
  var currentPosition = Number(slideStyle.left.replace('px', ''));

  direction == 'right' ? increment = increment * -1 : increment = increment;
  direction == 'right' ? slideWidth = slideWidth * -1 :
      slideWidth = slideWidth + increment;

  var stopSlidePosition = currentPosition % slideWidth;

  if (direction == 'right' &&
    currentPosition > slideWidth * (numberOfSlides - 2) ||
    direction == 'left' &&
    currentPosition != 0) {

    slideStyle.left = currentPosition + increment + 'px';
    animate = setTimeout('moveItem(\'' + direction + '\')', 15);

    if (stopSlidePosition == 0 && currentPosition != 0) {
      changeScrollerArrow(currentPosition, slideWidth, numberOfSlides);
      clearTimeout(animate);
      var theDirection = direction;
    }
  } else {
    changeScrollerArrow(currentPosition, slideWidth, numberOfSlides);
  }
}
