var HomeBanner = (function() {
  var _current = 0;
  var _imgs,
      _links,
      _timeout;
      
  var _goto = function(index) {
    if (index == _current) return;
    clearTimeout(_timeout);
    var _last = _current;
    _current = index;
    $('#featureBoxImages')
      .append(_imgs.eq(_current))
      .animate({left: '-'+HomeBanner.width+'px'}, {
        complete: function() {
          _imgs.eq(_last).remove();
          $('#featureBoxImages').css('left', '0px');
          _links.eq(_last).removeClass('selected');
          _links.eq(_current).addClass('selected');
        }
      });
    _timeout = setTimeout(_rotate, HomeBanner.delay);    
  };      
  
  var _next = function() {
    _goto((_current + 1) % _imgs.length);
  };
  var _previous = function() {
    _goto((_current + _imgs.length - 1) % _imgs.length);
  };
  
  var _rotate = function() {
    _next();
  };
  
  return {
    init: function() {
      if (!HomeBanner.delay) { HomeBanner.delay = 13000; }
      if (!HomeBanner.width) { HomeBanner.width = 940; }
      /* Init Links */
      _links = $('#featureBoxLinks a.selector');
      _links.each(function(i) {
        $(this).click(function() {_goto(i); return false; });
      });
      $('#featureBoxLinks a.next').click(function() { _next(); return false; });
      $('#featureBoxLinks a.previous').click(function() { _previous(); return false; });
      /* Init Images */
      _imgs = $('#featureBoxImages .rotate').remove();
      
      /* Init page state and start timer */
      $('#featureBoxImages').append(_imgs.eq(_current));
      _links.eq(_current).addClass('selected');
      _timeout = setTimeout(_rotate, HomeBanner.delay);      
    }
  };
})();

$(HomeBanner.init);