(function() {
  var Box, Rainbow, Ring, TRANSITION_END_EVENTS, buildTransform, expandVendorPrefix, initBox, initRing, initTwitterWidget, toRainbow;

  TRANSITION_END_EVENTS = ["webkitTransitionEnd", "mozTransitionEnd", "oTransitionEnd", "msTransitionEnd", "transitionend"];

  expandVendorPrefix = function(property, value) {
    var dict, prefix, _i, _len, _ref;
    dict = {};
    _ref = ["-webkit-", "-moz-", "-o-", "-ms-", ""];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      prefix = _ref[_i];
      dict["" + prefix + property] = value;
    }
    return dict;
  };

  buildTransform = function(transform) {
    return {
      "-webkit-transform": transform,
      "-moz-transform": transform,
      "-o-transform": transform,
      "-ms-transform": transform,
      "transform": transform
    };
  };

  Rainbow = (function() {

    Rainbow.prototype.colors = ["#FF0000", "#FF7F00", "#FFFF00", "#00FF00", "#00FFFF", "#0000FF", "#EE82EE"];

    Rainbow.prototype.defaultOptions = {
      selector: ".rainbow"
    };

    function Rainbow(options) {
      var delayed, name, _i, _len,
        _this = this;
      options = $.extend({}, this.defaultOptions, options);
      this.source = $(options.selector);
      for (_i = 0, _len = TRANSITION_END_EVENTS.length; _i < _len; _i++) {
        name = TRANSITION_END_EVENTS[_i];
        this.source.on(name, function(e) {
          return _this.changeColor(e.target);
        });
      }
      delayed = function() {
        return _this.source.each(function(i, elem) {
          return _this.changeColor(elem, i - 1);
        });
      };
      setTimeout(delayed, 1);
    }

    Rainbow.prototype.changeColor = function(elem, colorIndex) {
      if (colorIndex == null) colorIndex = $(elem).data("colorIndex");
      colorIndex = (colorIndex + 1) % 7;
      $(elem).css({
        color: this.colors[colorIndex]
      });
      return $(elem).data("colorIndex", colorIndex);
    };

    return Rainbow;

  })();

  Box = (function() {

    Box.prototype.defaultOptions = {
      selector: ".box",
      onRotateEnd: function() {}
    };

    function Box(options) {
      var name, _i, _len,
        _this = this;
      options = $.extend({}, this.defaultOptions, options);
      this.source = $(options.selector);
      this.angleX = 0;
      this.angleY = 0;
      this.angleZ = 0;
      this.animate = false;
      for (_i = 0, _len = TRANSITION_END_EVENTS.length; _i < _len; _i++) {
        name = TRANSITION_END_EVENTS[_i];
        this.source.on(name, function(e) {
          return _this.rotateEnd(e);
        });
      }
    }

    Box.prototype.rotateX = function(angle) {
      this.angleX += angle;
      this.update();
      return this.angleX;
    };

    Box.prototype.rotateY = function(angle) {
      this.angleY += angle;
      this.update();
      return this.angleY;
    };

    Box.prototype.rotateZ = function(angle) {
      this.angleZ += angle;
      this.update();
      return this.angleZ;
    };

    Box.prototype.update = function() {
      var transform;
      transform = "rotateX(" + this.angleX + "deg) rotateY(" + this.angleY + "deg) rotateZ(" + this.angleZ + "deg)";
      this.animate = true;
      return this.source.css(buildTransform(transform));
    };

    Box.prototype.rotateEnd = function(e) {
      this.animate = false;
      if (this.onRotateEnd) return this.onRotateEnd(e);
    };

    return Box;

  })();

  Ring = (function() {

    Ring.prototype.defaultOptions = {
      selector: ".ring",
      repeat: 0,
      radius: 200,
      itemElement: "<span>",
      itemClass: ""
    };

    function Ring(options) {
      var _this = this;
      options = $.extend({}, this.defaultOptions, options);
      this.source = $(options.selector);
      this.source.each(function(i, elem) {
        return _this.createRing($(elem), options);
      });
    }

    Ring.prototype.createRing = function(elem, options) {
      var char, i, interval, n, text, _ref, _results;
      text = elem.text();
      elem.empty();
      interval = 360 / (text.length * (options.repeat + 1));
      n = 0;
      _results = [];
      for (i = 0, _ref = options.repeat; 0 <= _ref ? i <= _ref : i >= _ref; 0 <= _ref ? i++ : i--) {
        _results.push((function() {
          var _i, _len, _results2;
          _results2 = [];
          for (_i = 0, _len = text.length; _i < _len; _i++) {
            char = text[_i];
            elem.append(this.createElement(n, char, interval, options));
            _results2.push(++n);
          }
          return _results2;
        }).call(this));
      }
      return _results;
    };

    Ring.prototype.createElement = function(n, char, interval, options) {
      var elem, transform;
      elem = $(options.itemElement).text(char).addClass(options.itemClass);
      transform = "rotateY(" + (interval * n) + "deg) translateZ(" + options.radius + "px)";
      elem.css(buildTransform(transform));
      return elem;
    };

    return Ring;

  })();

  initBox = function() {
    var box, pos;
    box = new Box;
    $(document).on("keydown", function(e) {
      if (box.animate) return;
      switch (e.keyCode) {
        case 37:
          return box.rotateY(-90);
        case 38:
          return box.rotateX(+90);
        case 39:
          return box.rotateY(+90);
        case 40:
          return box.rotateX(-90);
      }
    });
    pos = {
      x: 0,
      y: 0
    };
    $(document).on("touchstart", function(e) {
      var t;
      e.preventDefault();
      t = e.originalEvent.touches[0];
      return pos = {
        x: t.screenX,
        y: t.screenY
      };
    });
    return $(document).on("touchmove", function(e) {
      var delta, t;
      e.preventDefault();
      t = e.originalEvent.touches[0];
      delta = {
        x: t.screenX - pos.x,
        y: t.screenY - pos.y
      };
      pos = {
        x: t.screenX,
        y: t.screenY
      };
      box.rotateX(-delta.y / 2);
      return box.rotateY(delta.x / 2);
    });
  };

  initRing = function() {
    var rainbowClass;
    rainbowClass = "prim rainbow";
    new Ring({
      selector: ".circle .ring:nth-child(1)",
      repeat: 4,
      radius: 350,
      itemClass: rainbowClass
    });
    new Ring({
      selector: ".circle .ring:nth-child(2)",
      repeat: 3,
      radius: 300,
      itemClass: rainbowClass
    });
    new Ring({
      selector: ".circle .ring:nth-child(3)",
      repeat: 5,
      radius: 400,
      itemClass: rainbowClass
    });
    return new Ring({
      selector: ".sphere .ring",
      repeat: 2,
      radius: 250,
      itemClass: rainbowClass
    });
  };

  initTwitterWidget = function() {
    return new TWTR.Widget({
      version: 2,
      id: 'tweets-search',
      type: 'search',
      search: '#kyapi',
      interval: 30000,
      title: '',
      subject: '☆（ゝω・）v',
      width: 'auto',
      height: 200,
      theme: {
        shell: {
          background: '#ffdee6',
          color: '#ff9999'
        },
        tweets: {
          background: '#ffffff'
        },
        color: '#444444',
        links: '#fc8637'
      },
      features: {
        scrollbar: true
      },
      loop: false,
      live: true,
      behavior: 'all'
    }).render().start();
  };

  twttr.anywhere(function(T) {
    return T("#tweets-box").tweetBox({
      label: "☆（ゝω・）v",
      defaultContent: "☆（ゝω・）v #kyapi",
      width: 380,
      height: 150
    });
  });

  toRainbow = function() {
    return new Rainbow;
  };

  $(function() {
    initBox();
    initRing();
    toRainbow();
    return initTwitterWidget();
  });

}).call(this);

