(function() {

  this.def_tag('CURTAIN_TAG_ID',"NODE_TAG",function(__scope,__class){

    if(!this.__ctor__){this.__ctor__= function CURTAIN_TAG_ID(){__class.__super__.__ctor__.apply(this, arguments)}; this.__ctor__.prototype = this.prototype; }

    this.prototype.enable = function(action) {
      if (!this.parent()) __tagid('app').append(this);
      this["@action"] = action;
      __class.__super__.prototype.enable.apply(this, arguments);
      return this.show();
    }; __method_added(this,"enable");

    this.prototype.ontap = function() {
      if (this["@action"]) return this["@action"](this);
    }; __method_added(this,"ontap");

  });

  this.def_tag('OL_TAG',"NODE_TAG",function(__scope,__class){

    if(!this.__ctor__){this.__ctor__= function OL_TAG(){__class.__super__.__ctor__.apply(this, arguments)}; this.__ctor__.prototype = this.prototype; }

    this.prototype.awaken = function() {
      return this.render();
    }; __method_added(this,"awaken");

    this.prototype.refresh = function() {
      return this.render();
    }; __method_added(this,"refresh");

    this.prototype.render = function() {
      var _this = this;
      if (!this["@data"]) return;
      return this["@data"].each(function(item, idx) {
        return _this.append(_this.member(item, idx));
      });
    }; __method_added(this,"render");

    this.prototype.ondatamove = function(item, to, from) {
      return true;
    }; __method_added(this,"ondatamove");

    this.prototype.ondataadd = function(item, idx) {
      this.append(this.member(item, idx));
      return true;
    }; __method_added(this,"ondataadd");

    this.prototype.member = function(obj, i) {
      if (this["@template"]) {
        return this["@template"](obj)['[]'](0);
      } else {
        return __tag('li',null,null,{
          'data': obj
        });
      }
    }; __method_added(this,"member");

  });

  this.def_tag('LI_TAG',"NODE_TAG",function(__scope,__class){

    if(!this.__ctor__){this.__ctor__= function LI_TAG(){__class.__super__.__ctor__.apply(this, arguments)}; this.__ctor__.prototype = this.prototype; }

    this.prototype.awaken = function() {
      var _this = this;
      this["tabindex="](0);
      this.on('data:destroy', function() {
        return _this["destroy!"]();
      });
      this.on('*', function(e) {
        var m;
        if (m = e['[]'](/^data:change:(\w+)$/)) {
          return _this.ondatachange(m['[]'](1), arguments[1], arguments[2]);
        }
      });
      return this.render();
    }; __method_added(this,"awaken");

    this.prototype.render = function() {
      if (this.template() && this.data()) {
        return this["body="](this["@template"](this.data()));
      }
    }; __method_added(this,"render");

    this.prototype.ondatachange = function(key, value, previous) {
      return this.p('ondatachange!', key, value, previous);
    }; __method_added(this,"ondatachange");

  });

  this.def_tag('LEVELSLIST_TAG',"OL_TAG",function(__scope,__class){

    if(!this.__ctor__){this.__ctor__= function LEVELSLIST_TAG(){__class.__super__.__ctor__.apply(this, arguments)}; this.__ctor__.prototype = this.prototype; }

    this.prototype.onkeydown = function(event) {
      var sel;
      if (!(sel = __sel([['.selected']], this))) return;
      if (event.char() === '1') {
        this.data().move(sel.data().id(), -1);
        return sel.move(-1);
      } else if (event.char() === '2') {
        this.data().move(sel.data().id(), 1);
        return sel.move(1);
      }
    }; __method_added(this,"onkeydown");

    this.prototype.ondrop = function(touch) {
      var dragdata;
      dragdata = touch["@dragtarget"].data();
      if (!this.data()["include?"](dragdata.id())) {
        return this.data().add(dragdata.id());
      }
    }; __method_added(this,"ondrop");

    this.prototype.ondragenter = function(touch) {
      return this.flag('droptarget');
    }; __method_added(this,"ondragenter");

    this.prototype.ondragleave = function(touch) {
      return this.unflag('droptarget');
    }; __method_added(this,"ondragleave");

  });

  this.def_tag('LEVELITEM_TAG',"LI_TAG",function(__scope,__class){

    if(!this.__ctor__){this.__ctor__= function LEVELITEM_TAG(){__class.__super__.__ctor__.apply(this, arguments)}; this.__ctor__.prototype = this.prototype; }

    this.prototype.translate = function(x, y, z) {
      return this["@node"].style.webkitTransform = "translate3d(" + x + "px," + y + "px," + (z || 0) + ")";
    }; __method_added(this,"translate");

    this.prototype.ontouchstart = function(touch) {
      return true;
    }; __method_added(this,"ontouchstart");

    this.prototype.ontouchmove = function(touch) {
      if (!touch["dragdrop?"]()) return touch["dragtarget="](this);
    }; __method_added(this,"ontouchmove");

    this.prototype.ondragstart = function() {
      return this.flag('dragging');
    }; __method_added(this,"ondragstart");

    this.prototype.ondragmove = function(touch) {
      return this.translate(touch["@movedX"], touch["@movedY"]);
    }; __method_added(this,"ondragmove");

    this.prototype.ondragend = function() {
      this.unflag('dragging');
      return this.translate(0, 0);
    }; __method_added(this,"ondragend");

    this.prototype.ondrop = function(touch) {
      var dragdata;
      this.unflag('droptarget');
      dragdata = touch["@dragtarget"].data();
      if (this.parent().data()["include?"](dragdata.id())) {
        this.parent().data().move(dragdata.id(), {
          before: this.data().id()
        });
        touch["@dragtarget"].before(this);
      } else {
        this.parent().data().add(dragdata.id());
      }
      return true;
    }; __method_added(this,"ondrop");

    this.prototype.ondragenter = function(touch) {
      return this.flag('droptarget');
    }; __method_added(this,"ondragenter");

    this.prototype.ondragleave = function(touch) {
      return this.unflag('droptarget');
    }; __method_added(this,"ondragleave");

    this.prototype.ontap = function() {
      __tagid('levelcard')["data="](this.data());
      return __tagid('levelcard').show();
    }; __method_added(this,"ontap");

    this.prototype.ondatachange = function(key, value) {
      var field;
      this.p('ondatachange!', arguments);
      if (key === 'name') {
        return __sel([['.name']], this)["text="](value);
      } else if (key === 'published') {
        if (field = __sel([['.published']], this)) {
          if (value) {
            return field.show();
          } else {
            return field.hide();
          }
        }
      }
    }; __method_added(this,"ondatachange");

    this.prototype.awaken = function() {
      var _this = this;
      __class.__super__.prototype.awaken.apply(this, arguments);
      ['metal', 'rock', 'dirt', 'food', 'slowdown', 'speedup', 'bonus'].each(function(type) {
        if (_this.data()["include?"](type)) return _this.flag(type);
      });
      __sel([['.length']], this)["text="](this.data().real_length());
      return this;
    }; __method_added(this,"awaken");

  });

  this.def_tag('LEVELPACKS_TAG',"OL_TAG",function(__scope,__class){

    if(!this.__ctor__){this.__ctor__= function LEVELPACKS_TAG(){__class.__super__.__ctor__.apply(this, arguments)}; this.__ctor__.prototype = this.prototype; }



  });

  this.def_tag('LEVELPACK_TAG',"LI_TAG",function(__scope,__class){

    if(!this.__ctor__){this.__ctor__= function LEVELPACK_TAG(){__class.__super__.__ctor__.apply(this, arguments)}; this.__ctor__.prototype = this.prototype; }

    this.prototype.toggle = function() {
      if (this["flagged?"]('collapsed')) {
        return this.unflag('collapsed');
      } else {
        return this.flag('collapsed');
      }
    }; __method_added(this,"toggle");

  });

  this.def_tag('NEWLEVEL_TAG_ID',"MODAL_TAG",function(__scope,__class){

    if(!this.__ctor__){this.__ctor__= function NEWLEVEL_TAG_ID(){__class.__super__.__ctor__.apply(this, arguments)}; this.__ctor__.prototype = this.prototype; }

    this.prototype.submit = function() {
      var data;
      var _this = this;
      data = {};
      __msel([['formfield']], this).each(function(field) {
        return data[field.name()] = field.value();
      });
      __scope.Level.create(data, function(level) {
        __tagid('app').user().levels().rpush(level.id());
        return _this.hide();
      });
      return this;
    }; __method_added(this,"submit");

  });

  this.def_tag('LEVELCARD_TAG_ID',"MODAL_TAG",function(__scope,__class){

    if(!this.__ctor__){this.__ctor__= function LEVELCARD_TAG_ID(){__class.__super__.__ctor__.apply(this, arguments)}; this.__ctor__.prototype = this.prototype; }

    this.prototype.edit = function() {
      this.hide();      return $router.go("level/" + this.data().id());
    }; __method_added(this,"edit");

    this.prototype.refresh = function() {
      __sel([['.name']], this)["value="](this["@data"].name());
      return __sel([['.credits']], this)["value="](this["@data"].credits());
    }; __method_added(this,"refresh");

    this.prototype.publish = function() {
      this["@data"]["published="](true);
      return this["@data"].save();
    }; __method_added(this,"publish");

    this.prototype.destroy = function() {
      if (confirm('Are you sure you want to delete the level?')) {
        this["@data"]["destroy!"]();
        return this.hide();
      }
    }; __method_added(this,"destroy");

    this.prototype.onformchange = function(event) {
      this["@data"]["name="](__sel([['.name']], this).value());
      this["@data"]["credits="](__sel([['.credits']], this).value());
      this["@data"].save();
      return true;
    }; __method_added(this,"onformchange");

  });

  this.def_tag('APP_TAG_ID',"NODE_TAG",function(__scope,__class){

    this.attr('user');

    this.__ctor__ = function APP_TAG_ID(node, attributes, children) {
      __class.__super__.__ctor__.apply(this, arguments);
      $app = this;
      $db = this["@db"] = new (__ctor(__scope.Rome))({
        path: "/db"
      });
      this[">>"]($doc.body);
      return this;
    }; this.__ctor__.prototype = this.prototype;;

    this.prototype.login = function() {
      var _this = this;
      return __scope.FB.login(function(resp) {
        if (resp.status === 'connected') {
          return _this.authenticate(resp.authResponse);
        }
      });
    }; __method_added(this,"login");

    this.prototype.awaken = function() {
      var _this = this;
      return $win.fbAsyncInit = function() {
        __scope.FB.init({
          appId: '116182285116946',
          oauth: true,
          cookie: false,
          status: true
        });
        return __scope.FB.getLoginStatus(function(resp) {
          if (resp.status === 'connected') {
            _this.p(resp);
            return _this.authenticate(resp.authResponse);
          }
        });
      };
    }; __method_added(this,"awaken");

    this.prototype.authenticate = function(resp) {
      var user_id;
      var _this = this;
      this.p(resp);
      __tagid('login').hide();
      user_id = resp.userID;
      $doc.cookie = "userid=" + user_id + "; path=/";
      __scope.User.get(user_id, function(user) {
        _this["user="](user);
        $router.start();
        __tagid('my_levels')["data="](user.levels());
        return __tagid('levelpacks')["data="](user.levelpacks());
      });
      return __scope.FB.api('/me', function(response) {
        _this.user()["name="](response.name);
        return __sel([['#newlevel',' ','.credits']], _this)["value="](response.name);
      });
    }; __method_added(this,"authenticate");

    this.prototype.level = function() {
      return this["@level"];
    }; __method_added(this,"level");

    this.prototype["level="] = function(level) {
      if (level === this["@level"] || !this["@user"]) return this;
      if (this["@level"] = level) {
        __tagid('home').hide();
        __tagid('editor').show(this["@level"]);
        return __tagid('title')["text="](level.name());
      } else {
        __tagid('title')["text="]("Editor");
        __tagid('editor').hide();
        return __tagid('home').show();
      }
    }; __method_added(this,"level=");

    this.prototype.newlevel = function() {
      return __tagid('newlevel').show();
    }; __method_added(this,"newlevel");

  });

}).call($$base);

