// Drink Votes Module
// ==================
// Clicking the arrow scrolls the results list.  When you reach the bottom,
// the list repeats from the top.
;$(function () {
  var el, list, scrollHeight

  el = $('div.drink-votes')

  // Double the lists initially and store the initial items to append later
  el.find('ol').each(function (i, list) {
    list = $(list)
    list.data('original-items', list.children().clone())
    appendItems(list)
  })

  // Determine the height we should scroll on click
  scrollHeight = el.find('li').height() * 2

  // Wire events
  el.delegate('.arrow', 'click', scrollList)
  
  function scrollList (e) {
    var list, items
    list = $(e.currentTarget).closest('.voting').find('ol')
    items = list.children()
    items.first().animate({marginTop: '-=' + scrollHeight})
    
    // We append a new list of items on every click, which is more than we need,
    // but is logic free
    appendItems(list)
  }

  function appendItems(list) {
    list.data('original-items').clone().appendTo(list)
  }
});


