(function() {
  var __hasProp = Object.prototype.hasOwnProperty;

  $block_size = 32;

  $tosrp = function(x,y,sh,br){
    var BS = 32;
    var HBS = 16;
    var SH = sh || window.innerHeight;
    y = SH - y

    var diag = Math.floor((y - x + HBS) / BS);
    var elem = Math.floor((x + y + HBS) / BS) - diag;
    var row = 2 * diag + elem;
    var col = Math.floor(elem / 2);
    
    row = row + (br != undefined ? br : $bottom_row);

    return {r: row, c: col}
};

  $router = {
    start: function() {
      var method, regex, route, _ref;
      var _this = this;
      this["@regexes"] = [];
      _ref = this.routes;
      for (route in _ref) {
        if (!__hasProp.call(_ref, route)) continue;
        method = _ref[route];
        route = route.replace(/\:(\w+)/g, "([^\\/]+)");
        regex = new (__ctor(__scope.RegExp))("^" + route + "$");
        regex['__route__'] = $router[method];
        this["@regexes"].push(regex);
      }
      $win.onhashchange = function() {
        return _this.test();
      };
      return this.test();
    },
    test: function() {
      var match, path, regex, _i, _len, _primitive, _ref;
      path = $doc.location.hash.substr(1);
      _ref = this["@regexes"];
      for (_i = 0, _len = (_ref.len ? _ref.len() : _ref.length),_primitive = (_ref instanceof Array || !_ref.at); _i < _len; _i++) {
        regex = _primitive ? _ref[_i] : _ref.at(_i);
        if (match = path.match(regex)) {
          return regex['__route__'].apply($router, match.slice(1));
        }
      }
      return this.root();
    },
    go: function(url) {
      return $doc.location.hash = "#" + url;
    },
    routes: {
      "": "root",
      "level/:id": "level"
    },
    level: function(id) {
      var _this = this;
      return __scope.Level.get(id, function() {
        return __tagid('app')["level="](arguments[0]);
      });
    },
    root: function() {
      return __tagid('app')["level="](null);
    }
  };

  __tag(undefined, "app",null,{},[
    __tag('header', "header",null,{},[__tag('h1', "title",null,{},["Editor"])]), __tag(undefined, "newlevel",[ "hidden"],{
      'onsubmit': 'adding_level'
    },[
      __tag('header',null,null,{},["create new level"]), __tag('section',null,null,{},[
        __tag('textfield',null,[ "name"],{
          'label': 'Levelname',
          'name': 'name',
          'value': 'Unnamed level'
        }), __tag('textfield',null,[ "credits"],{
          'label': 'Author / Credits',
          'name': 'credits'
        }), __tag('slider',null,[ "large"],{
          'min': 10,
          'max': 40,
          'step': 1,
          'value': 20,
          'name': 'width',
          'label': 'width of level-pattern'
        })
      ]), __tag('footer',null,null,{},[
        __tag('button',null,null,{
          'ontap': 'submit'
        },["CREATE"])
      ])
    ]), __tag(undefined, "levelcard",[ "hidden"],{},[
      __tag('section',null,null,{},[
        __tag('textfield',null,[ "name"],{
          'label': 'Levelname',
          'name': 'name'
        }), __tag('textfield',null,[ "credits"],{
          'label': 'Author / Credits',
          'name': 'credits'
        })
      ]), __tag('footer',null,null,{},[
        __tag('button',null,[ "small", "destructive"],{
          'ontap': 'destroy'
        },["REMOVE"]), __tag('button',null,[ "small"],{
          'ontap': 'publish'
        },["PUBLISH"]), __tag('button',null,[ "small"],{
          'ontap': 'edit'
        },["EDIT"])
      ])
    ]), __tag('div', "login",[ "center"],{},[
      __tag('button',null,null,{
        'ontap': 'login'
      },["LOG IN WITH FACEBOOK"])
    ]), __tag(undefined, "home",[ "hidden"],{},[
      __tag('section',null,[ "levels"],{},[
        __tag('header',null,null,{},["My levels"]), __tag('levelslist', "my_levels",null,{},function(level_id) {
          return [
            __tag('levelitem',null,null,{
              'data': __scope.Level.get(level_id)
            },function(level) {
              return [
                __tag('span',null,[ "nr"],{},[level.id()]), __tag('span',null,[ "length"],{}), __tag('span',null,[ "name"],{},[level.name()]), __tag('div',null,[ "published"],{
                  'hidden': !level.published()
                },['published']), __tag('div',null,[ "symbols"],{},[__tag('div',null,[ "metal"],{}), __tag('div',null,[ "rock"],{}), __tag('div',null,[ "dirt"],{}), __tag('div',null,[ "bonus"],{}), __tag('div',null,[ "slowdown"],{}), __tag('div',null,[ "speedup"],{}), __tag('div',null,[ "heart"],{})])
              ];
            })
          ];
        }), __tag('footer',null,null,{},[
          __tag('button',null,[ "small"],{
            'ontap': 'newlevel'
          },["NEW LEVEL"])
        ])
      ]), __tag('section',null,[ "levels"],{},[
        __tag('levelpacks', "levelpacks",null,{},function(pack_id) {
          return [
            __tag('levelpack',null,null,{
              'data': __scope.Levelpack.get(pack_id)
            },function(pack) {
              return [
                __tag('header',null,[ "name"],{
                  'ontap': 'toggle'
                },[pack.name()]), __tag('levelslist',null,null,{
                  'data': pack.levels()
                },function(level_id) {
                  return [
                    __tag('levelitem',null,null,{
                      'data': __scope.Level.get(level_id)
                    },function(level) {
                      return [__tag('span',null,[ "nr"],{},[level.id()]), __tag('span',null,[ "length"],{}), __tag('span',null,[ "name"],{},[level.name()]), __tag('div',null,[ "symbols"],{},[__tag('div',null,[ "metal"],{}), __tag('div',null,[ "rock"],{}), __tag('div',null,[ "dirt"],{}), __tag('div',null,[ "bonus"],{}), __tag('div',null,[ "slowdown"],{}), __tag('div',null,[ "speedup"],{}), __tag('div',null,[ "heart"],{})])];
                    })
                  ];
                }), __tag('footer',null,null,{})
              ];
            })
          ];
        })
      ])
    ]), __tag(undefined, "editor",[ "hidden"],{},[
      __tag(undefined, "tools",null,{},[
        __tag('timertool',null,null,{
          'shortcut': '1'
        }), __tag('blocktool',null,null,{
          'shortcut': '2',
          'block': __scope.Block.goal
        }), __tag('blocktool',null,null,{
          'shortcut': '3',
          'block': __scope.Block.star
        }), __tag('blocktool',null,null,{
          'shortcut': '4',
          'block': __scope.Block.food
        }), __tag('blocktool',null,null,{
          'shortcut': '5',
          'block': __scope.Block.metal
        }), __tag('blocktool',null,null,{
          'shortcut': '6',
          'block': __scope.Block.rock
        }), __tag('blocktool',null,null,{
          'shortcut': '7',
          'block': __scope.Block.dirt
        }), __tag('blocktool',null,null,{
          'shortcut': '8',
          'block': __scope.Block.bonus
        }), __tag('blocktool',null,null,{
          'shortcut': '9',
          'block': __scope.Block.focusbig
        }), __tag('custombrushtool',null,null,{
          'shortcut': '0'
        }), __tag('blocktool',null,null,{
          'block': __scope.Block.speedup
        }), __tag('blocktool',null,null,{
          'block': __scope.Block.slowdown
        }), __tag('blocktool',null,null,{
          'block': __scope.Block.mesh
        }), __tag('blocktool',null,null,{
          'block': __scope.Block.path
        }), __tag('blocktool',null,null,{
          'block': __scope.Block.empty
        })
      ]), __tag(undefined, "brushpanel",null,{},[__tag('header',null,null,{},["custom brush"]), __tag(undefined, "brushcanvas",null,{})]), __tag(undefined, "timings",[ "hidden"],{},[
        __tag('header',null,null,{},["timings"]), __tag('slider',null,null,{
          'min': -6,
          'max': 6,
          'step': 1,
          'value': 0,
          'label': "snake distance from row"
        })
      ]), __tag(undefined, "timerpanel",null,{},[
        __tag('header',null,null,{},["Timings"]), __tag('section',null,null,{},[
          __tag('switcher',null,[ "start_state"],{
            'min': 0,
            'max': 1,
            'step': 1,
            'value': 0,
            'label': "starting state"
          }), __tag('slider',null,[ "row_delay"],{
            'min': -20,
            'max': 20,
            'step': 1,
            'value': 0,
            'label': "row delay"
          }), __tag('slider',null,[ "col_delay"],{
            'min': -20,
            'max': 20,
            'step': 1,
            'value': 0,
            'label': "col delay"
          }), __tag('slider',null,[ "own_delay"],{
            'min': -256,
            'max': 256,
            'step': 8,
            'value': 0,
            'label': "own delay"
          }), __tag('slider',null,[ "on"],{
            'min': 8,
            'max': 256,
            'step': 8,
            'value': 32,
            'label': "toggle after"
          }), __tag('slider',null,[ "off"],{
            'min': 8,
            'max': 256,
            'step': 8,
            'value': 32,
            'label': "toggle back after"
          })
        ]), __tag('footer',null,null,{},[
          __tag('button',null,[ "small", "destructive"],{
            'ontap': 'remove_selected'
          },["delete selected"]), __tag('button',null,[ "small"],{
            'ontap': 'apply'
          },["apply"])
        ])
      ]), __tag(undefined, "foreground",null,{}), __tag('canvas', "world",null,{})
    ])
  ]);

}).call($$base);

