/**
 * Homepage scripts.
 *
 * Requires Prototype 1.6.
 */

function initCarousel() {
  var SCROLL_INTERVAL = 0.02, SCROLL_STEP = 6;
  
  var base = $('brandsCarousel'), list = base && base.down('ul'), scrollDir, scrollTimer,
    scrollLeft = parseInt(list.getStyle('left'), 10), origScrollLeft = scrollLeft,
    containerWidth = base.down('.container').getWidth();
  if (!list) return;
  // Not enough content? Don't display/init a useless carousel!
  var listWidth = list.select('li').length * (120 + 20) - 20;
  list.setStyle('width: ' + listWidth + 'px');
  if (listWidth < containerWidth) {
    base.setStyle('padding: 0').select('.nav').invoke('hide');
    return;
  }
  var minLeft = containerWidth - listWidth, adjustTimer;

  function adjustListWidth() {
    adjustTimer && clearTimeout(adjustTimer);
    if (list.select('img').invoke('isLoaded').all()) {
      listWidth = list.select('li').inject(0, function(acc, item) { return acc + item.getWidth(); });
      list.setStyle('width: ' + listWidth + 'px');
      minLeft = containerWidth - listWidth;
      return;
    }
    adjustTimer = setTimeout(adjustListWidth, 500);
  }
  adjustListWidth();

  function scrollBrands() {
    if (scrollDir == -1) {
      scrollLeft = [scrollLeft + SCROLL_STEP, origScrollLeft].min();
      list.setStyle('left: ' + scrollLeft + 'px');
      if (scrollLeft == origScrollLeft) stopScrolling();
    } else if (scrollDir == 1) {
      scrollLeft = [scrollLeft - SCROLL_STEP, minLeft].max();
      list.setStyle('left: ' + scrollLeft + 'px');
      if (scrollLeft == minLeft) stopScrolling();
    } else
      stopScrolling();
  }
  
  function startScrolling(e) {
    var trigger = e.findElement('.nav');
    if (!trigger) return;
    scrollDir = trigger.hasClassName('previous') ? -1 : 1;
    scrollTimer && clearInterval(scrollTimer);
    scrollTimer = setInterval(scrollBrands, SCROLL_INTERVAL * 1000);
  }
  
  function stopScrolling(e) {
    var trigger = e && e.findElement('.nav');
    if (e && !trigger) return;
    scrollTimer && clearInterval(scrollTimer);
    scrollTimer = null;
  }
  
  base.observe('mouseover', startScrolling).observe('mouseout', stopScrolling);
  base.select('.nav').invoke('observe', 'click', function(e) {
    e.stop();
    this.blur();
  });
}

function initRatingSwitcher() {
  var base = $('ratings'), list = $('ratingList'), form = $('ratingForm');
  if (!base || !list || !form) return;
  base.select('.toggle').invoke('observe', 'click', function(e) {
    e.stop();
    list.toggle();
    form.toggle();
  });
}

// Just in case it's not there already…
Element.addMethods('IMG', {
  // Based on code by John-David Dalton and others: see http://pastie.org/pastes/185452
  isLoaded: (function() {
      var img = new Image();
      if ('naturalWidth' in img)
        return function(element) { return 0 !== element.naturalWidth; };
      if ('complete' in img)
        return function(element) { return !!element.complete; }
      return function(element) { return element.readyState == 'complete'; };
    })()
});

document.observe('dom:loaded', function() {
  initCarousel();
  initRatingSwitcher();
});
