(function() {
  var Adjust, Carousel, Core, Sandbox, init;
  var __hasProp = Object.prototype.hasOwnProperty, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
  window.typeOf = function(o) {
    return Object.prototype.toString.call(o).match(/\s[A-Za-z]+/)[0].toLowerCase();
  };
  Core = (function() {
    var createInstance, eventData, moduleData;
    eventData = {};
    moduleData = {};
    createInstance = function(moduleId, Core) {
      var instance, method, name;
      instance = new moduleData[moduleId].creator(new Sandbox(Core));
      if (window.DEBUG_MODE === true) {
        for (name in instance) {
          method = instance[name];
          if (typeof instance === 'function') {
            instance[name] = (function(method, name) {
              try {
                return method.apply(instance, arguments);
              } catch (e) {
                if (console !== void 0) {
                  return console.log("" + name + "(): " + e);
                }
              }
            })(method, name);
          }
        }
      }
      return instance;
    };
    return {
      register: function(moduleId, creator) {
        creator.prototype.pkg = moduleId;
        moduleData[moduleId] = {
          creator: creator,
          instance: null
        };
        return this;
      },
      start: function(moduleId) {
        moduleData[moduleId].instance = createInstance(moduleId, this);
        moduleData[moduleId].instance.init();
        return this;
      },
      stop: function(moduleId) {
        var data;
        data = moduleData[moduleId];
        if (data != null) {
          data.instance.destroy();
          data.instance = null;
        }
        return this;
      },
      startAll: function() {
        var moduleId;
        for (moduleId in moduleData) {
          if (!__hasProp.call(moduleData, moduleId)) continue;
          this.start(moduleId);
        }
        return this;
      },
      stopAll: function() {
        var moduleId;
        for (moduleId in moduleData) {
          if (!__hasProp.call(moduleData, moduleId)) continue;
          this.stop(moduleId);
        }
        return this;
      },
      events: eventData
    };
  })();
  Sandbox = function(Core) {
    var eventData, slice;
    eventData = Core.events;
    slice = Array.prototype.slice;
    return {
      listen: function() {
        var args, ctx, e, ev, evt, found, handler, _len, _ref;
        args = slice.call(arguments);
        e = args.shift();
        handler = args.shift();
        ctx = args[0] || this;
        eventData[e] = eventData[e] || [];
        found = false;
        _ref = eventData[e];
        for (evt = 0, _len = _ref.length; evt < _len; evt++) {
          ev = _ref[evt];
          if (evt.handler === handler && evt.ctx === ctx) {
            found = true;
          }
        }
        if (!found) {
          return eventData[e].push({
            handler: handler,
            ctx: ctx
          });
        }
      },
      notify: function() {
        var args, e, ev, evt, _ref, _results;
        args = Array.prototype.slice.call(arguments);
        e = args.shift();
        if (eventData[e] != null) {
          _ref = eventData[e];
          _results = [];
          for (ev in _ref) {
            evt = _ref[ev];
            _results.push(typeOf(evt.handler === 'function' && typeOf(evt.ctx === 'object')) ? evt.handler.apply(evt.ctx, args) : void 0);
          }
          return _results;
        }
      },
      mute: function() {
        var args, ctx, e, ev, evt, handler, _len, _ref, _results;
        args = slice.call(arguments);
        e = args.shift();
        handler = args.shift();
        ctx = args[0] || this;
        if (eventData[e] != null) {
          _ref = eventData[e];
          _results = [];
          for (evt = 0, _len = _ref.length; evt < _len; evt++) {
            ev = _ref[evt];
            _results.push(evt.ctx === ctx && evt.handler === handler ? eventData[e].splice(ev, 1) : void 0);
          }
          return _results;
        }
      }
    };
  };
  Carousel = function(sandbox) {
    var animating, captions, carousel, currentIndex, init, items, left, lim, min, right, timer, wrapper;
    currentIndex = 0;
    min = 0;
    lim = 0;
    carousel = null;
    wrapper = null;
    captions = null;
    items = null;
    left = null;
    right = null;
    init = false;
    animating = false;
    timer = 0;
    return {
      init: function() {
        carousel = $('#carousel');
        wrapper = carousel.children('#carousel-wrapper');
        items = wrapper.children('li');
        carousel.animate({
          opacity: 1
        });
        captions = $('.captions').children();
        lim = items.length;
        sandbox.listen('resize', this.adjust, this);
        left = $('#carousel-left');
        right = $('#carousel-right');
        left.css('cursor', 'pointer').bind('click', __bind(function(e) {
          return this.left(e);
        }, this));
        right.css('cursor', 'pointer').bind('click', __bind(function(e) {
          return this.right(e);
        }, this));
        $(window).bind('keyup', __bind(function(e) {
          return this.keyPressed(e);
        }, this));
        this.setCurrentIndex(4);
        return this.autoRotate();
      },
      autoRotate: function() {
        return timer = setTimeout(__bind(function() {
          this.setCurrentIndex(currentIndex + 1);
          return this.autoRotate();
        }, this), 4000);
      },
      itemClicked: function(e) {
        var id;
        e.preventDefault();
        id = $(e.currentTarget).index();
        return this.setCurrentIndex(id);
      },
      left: function(e) {
        clearTimeout(timer);
        e.preventDefault();
        return this.setCurrentIndex(currentIndex - 1);
      },
      right: function(e) {
        if ((e != null) && typeOf(e.preventDefault === 'function')) {
          clearTimeout(timer);
          e.preventDefault();
        }
        return this.setCurrentIndex(currentIndex + 1);
      },
      adjust: function(w, h) {
        this.ww = w;
        this.wh = h;
        return this.updateData();
      },
      keyPressed: function(e) {
        if (e != null) {
          clearTimeout(timer);
        }
        switch (e.keyCode) {
          case 37:
            this.left(e);
            break;
          case 39:
            this.right(e);
        }
        return e.preventDefault();
      },
      setCurrentIndex: function(index) {
        if (animating === true) {
          return false;
        }
        currentIndex = index;
        this.controlCurrentIndex();
        return this.updateData();
      },
      controlCurrentIndex: function() {
        if (currentIndex <= 2) {
          return currentIndex = lim - 3;
        } else if (currentIndex >= lim - 3) {
          return currentIndex = 4;
        }
      },
      getOffset: function(currentItem) {
        var dx;
        return dx = currentItem.offset().left - wrapper.offset().left - 340 - $(window).width() / 2 + 914 / 2;
      },
      updateData: function() {
        var currentCaption, currentItem, dx, id;
        currentItem = items.eq(currentIndex);
        id = currentItem.data('id');
        currentCaption = captions.filter("[data-id=\"" + id + "\"]");
        currentCaption.stop().fadeIn().siblings().stop().fadeOut();
        dx = this.getOffset(currentItem);
        currentItem.stop().animate({
          opacity: 1
        });
        currentItem.siblings().stop().animate({
          opacity: .1
        });
        if (init === true) {
          animating = true;
        }
        wrapper.stop()[init === false ? 'css' : 'animate']({
          left: -dx
        }, __bind(function() {
          if (currentIndex === 3) {
            currentIndex = lim - 5;
            currentItem = items.eq(currentIndex);
            dx = this.getOffset(currentItem);
            currentItem.css('opacity', 1);
            currentItem.siblings().css('opacity', .1);
            wrapper.css({
              left: -dx
            });
          }
          if (currentIndex === lim - 4) {
            currentIndex = 4;
            currentItem = items.eq(currentIndex);
            dx = this.getOffset(currentItem);
            currentItem.css('opacity', 1);
            currentItem.siblings().css('opacity', .1);
            wrapper.css({
              left: -dx
            });
          }
          return animating = false;
        }, this));
        return init = true;
      },
      destroy: function() {}
    };
  };
  Core.register('carousel', Carousel);
  Adjust = function(sandbox) {
    return {
      init: function() {
        this.window = $(window);
        return this.window.bind('resize', __bind(function(e) {
          return this.adjust(e);
        }, this));
      },
      adjust: function(e) {
        var h, w;
        w = this.window.width();
        h = this.window.height();
        return sandbox.notify('resize', w, h);
      },
      destroy: function() {
        return this.window.unbind('resize', __bind(function(e) {
          return this.adjust(e);
        }, this));
      }
    };
  };
  Core.register('adjust', Adjust);
  init = function() {
    Core.startAll();
    return $(window).trigger('resize');
  };
  $(function() {
    return $(window).load(__bind(function() {
      return setTimeout(init, 50);
    }, this));
  });
}).call(this);

