// Generated by CoffeeScript 1.6.3
(function() {
  var DynamicBackgroud,
    __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };

  DynamicBackgroud = (function() {
    var BACKROUND_IMG, CONTAINER_ID, DURATION, LIGHTSIZE, LIGHT_IMG, OFFSET;

    CONTAINER_ID = 'bg';

    DURATION = 1500;

    LIGHTSIZE = 0.4;

    OFFSET = {
      x: 0.4,
      y: 0.2
    };

    LIGHT_IMG = 'img/light.png';

    BACKROUND_IMG = 'img/bg.jpg';

    function DynamicBackgroud() {
      this.dance = __bind(this.dance, this);
      this.win = $(window);
      this.winWidth = this.win.width();
      this.winHeight = this.win.height();
      this.newWidth = this.winWidth;
      this.newHeight = this.winHeight;
      this.lightSize = this.winWidth * LIGHTSIZE;
      this.duration = DURATION;
      this.prepareStage();
      this.prepareBg();
      this.prepareLights();
      this.bindResize();
      this.bindMovement();
    }

    DynamicBackgroud.prototype.prepareBg = function() {
      var image,
        _this = this;
      this.background = new Kinetic.Layer();
      this.stage.add(this.background);
      image = new Image();
      image.onload = function() {
        var height, img, ratio, width;
        ratio = image.width / image.height;
        width = _this.winWidth;
        height = _this.winWidth / ratio;
        img = new Kinetic.Image({
          x: 0,
          y: 0,
          image: image,
          width: width,
          height: height
        });
        _this.background.add(img);
        return _this.stage.draw();
      };
      return image.src = BACKROUND_IMG;
    };

    DynamicBackgroud.prototype.prepareLights = function() {
      this.lights = new Kinetic.Layer();
      this.stage.add(this.lights);
      this.buildLight({
        x: 0,
        y: 0
      });
      return this.buildLight({
        x: this.winWidth * OFFSET.x,
        y: this.winHeight * OFFSET.y
      });
    };

    DynamicBackgroud.prototype.buildLight = function(opts) {
      var image,
        _this = this;
      image = new Image();
      image.onload = function() {
        var img;
        img = new Kinetic.Image({
          x: opts.x,
          y: opts.y,
          image: image,
          width: _this.lightSize,
          height: _this.lightSize
        });
        _this.lights.add(img);
        return _this.stage.draw();
      };
      return image.src = LIGHT_IMG;
    };

    DynamicBackgroud.prototype.prepareStage = function() {
      return this.stage = new Kinetic.Stage({
        container: CONTAINER_ID,
        width: this.winWidth,
        height: this.winHeight
      });
    };

    DynamicBackgroud.prototype.bindResize = function() {
      var _this = this;
      return this.win.bind('resize orientationchange', function() {
        _this.newWidth = _this.win.width();
        _this.newHeight = _this.win.height();
        _this.stage.setScale(_this.getScale());
        _this.stage.setWidth(_this.newWidth);
        _this.stage.setHeight(_this.newHeight);
        return _this.stage.draw();
      });
    };

    DynamicBackgroud.prototype.durationInSeconds = function() {
      return this.duration / 1000;
    };

    DynamicBackgroud.prototype.getScale = function() {
      var scaleX, scaleY;
      scaleX = this.newWidth / this.winWidth;
      scaleY = this.newHeight / this.winHeight;
      if (scaleX > scaleY) {
        return scaleX;
      } else {
        return scaleY;
      }
    };

    DynamicBackgroud.prototype.dance = function() {
      var height, shape, width, xPos, yPos, _i, _len, _ref, _results;
      _ref = this.lights.getChildren();
      _results = [];
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        shape = _ref[_i];
        xPos = this.newWidth * Math.random() - this.lightSize / 2;
        yPos = this.newHeight * Math.random() - this.lightSize / 2;
        width = this.lightSize * (Math.random() + 0.5);
        height = this.lightSize * (Math.random() + 0.5);
        _results.push(new Kinetic.Tween({
          node: shape,
          duration: this.durationInSeconds(),
          x: xPos,
          y: yPos,
          rotation: Math.random() * 1,
          width: width,
          height: height,
          easing: Kinetic.Easings.EaseInOut
        }).play());
      }
      return _results;
    };

    DynamicBackgroud.prototype.bindMovement = function() {
      return setInterval(this.dance, this.duration);
    };

    return DynamicBackgroud;

  })();

  $(function() {
    return new DynamicBackgroud();
  });

}).call(this);
