/*
 *  @author   Rafaël <http://twitter.com/rafBM>, <http://github.com/rafBM>
 *  @company  iXmédia <http://www.ixmedia.com>
 */
if (!window.console) {
  (function() {
    var f = function() {}
    window.console = { log: f, warn: f, error: f, trace: f, group: f, groupCollapsed: f, groupEnd: f, time: f, timeEnd: f, profile: f, profileEnd: f, count: f }
  })()
}

$.fn.inGroupsOf = function(countPerGroup) {
  var groups = [], offset = 0, $group
  
  while ( ($group = this.slice(offset, (countPerGroup + offset))).length ) {
    groups.push($group)
    offset += countPerGroup
  }
  return groups
}

CAN_HAZ_TRANSITIONS = (function() {
  var div = document.createElement('div')
  return (
    div.style.WebkitTransition !== undefined ||
    div.style.MozTransition !== undefined ||
    div.style.OTransition !== undefined ||
    div.style.MsTransition !== undefined ||
    div.style.MSTransition !== undefined ||
    div.style.transition !== undefined
  )
})()

$.fn.focusOnce = (function() {
  var onblur = function() {
    $(this).removeProp('tabIndex')
  }
  return function() {
    this.each(function() {
      $(this).prop('tabIndex', 0).focus().one('blur', onblur)
    })
  }
})()

$(function() {
  
  Carousel = function() { this.initialize.apply(this, arguments) }
  Carousel.prototype = {
    initialize: function(ul) {
      var self = this
      
      // jQuery collections
      self.$ul = $(ul)
      self.$li = self.$ul.children('li')
      
      // constants
      self.LENGTH = self.$li.length - 11
      
      self.ITEM_WIDTH_EXPANDED  = self.$ul.attr('data-item-width-expanded')
      self.ITEM_WIDTH_COLLAPSED = self.$ul.attr('data-item-width-collapsed')
      
      self.INITIAL_OFFSET = parseInt(self.$ul.css('margin-left'))
      
      // properties
      self.currentIndex = 0
      
      // event listeners
      self.$ul.delegate('a.previous-teammate[href]', 'click', function() {
        self.slidePrevious()
      })
      self.$ul.delegate('a.next-teammate[href]', 'click', function() {
        self.slideNext()
      })
    },
    
    slideTo: function(index) {
      var self = this
      
      var $previousLi = self.$li.eq(self.currentIndex + 4)
      $previousLi.addClass('collapsed')
      
      self.currentIndex = index
      
      var offset = self.INITIAL_OFFSET - (self.currentIndex * self.ITEM_WIDTH_COLLAPSED)
      self.$ul.css('margin-left', offset)
      
      self.$li.eq(self.currentIndex + 4).removeClass('collapsed')
      setTimeout(function() {
        self.$li.eq(self.currentIndex + 4).find('.description').focusOnce()
      }, CAN_HAZ_TRANSITIONS ? 500 : 0)
    },
    
    slidePrevious: function() {
      this.slideTo(
        this.currentIndex > 0
          ? this.currentIndex - 1
          : this.LENGTH - 1
      )
    },
    
    slideNext: function() {
      this.slideTo(
        this.currentIndex < (this.LENGTH - 1)
          ? this.currentIndex + 1
          : 0
      )
    }
  }
  
  $(document).ready(function() {
    $(document.body).addClass('can-haz-js')
    var carousel = new Carousel('#carousel')
    $('#newsletter label :input').placeHolder()
  })
  
  var GroupLooper = Looper.$extend({
    
    __init__: function(opts) {
      this.groups = opts['groups']
      opts['length'] = this.groups.length;
      this.$super(opts)
      
      this.groups.slice(1).forEach(function(group) {
        group.hide()
      })
    },
    
    goto: function(index) {
      this.$super(index)
      this.groups[this.previousIndices[0]].fadeOut('slow', (function() {
        this.groups[this.currentIndex].fadeIn('slow')
      }).bind(this))
    }
    
  })
  
  var $points = $('#points ul')
  if ($points.length) {
    
    var dummyLooper = new GroupLooper({
      groups: $points.children('li').inGroupsOf(3),
      duration: 7000
    })
    window.dummyLooper = dummyLooper
    
    dummyLooper.start()
  }
  
})

