 canvasSizeUpdateBlocked = false
 randomFillNum = 2000
 randomFillPercent = 0.4
+
+class DefaultConfig
+  getGrid: -> [7,3]
+  getCellData: -> ""
+  getFunctionCode: -> "B 3 S 2 3"
+  
+class UriConfig
+  constructor: ->
+    @keys = parseUri(""+window.location).queryKey
+    
+  getGrid: ->  
+    if @keys.grid?
+      try
+        match = @keys.grid.match /{(\d+)[,;](\d+)}/
+        throw new Error("Syntax is bad: #{@keys.grid}") unless match
+        n = parseIntChecked match[1]
+        m = parseIntChecked match[2]
+        return [n,m]
+      catch e
+        alert "Bad grid paramters: #{@keys.grid}"
+    return [7,3]
+  getCellData: ->@keys.cells
+  getFunctionCode: ->
+    if @keys.rule?
+      @keys.rule.replace /_/g, ' '
+    else
+      "B 3 S 2 3"
 
 class Application
   constructor: ->

    updateCanvasSize()
    if found > 0
      @navigator.navigateToResult 0
    

updateCanvasSize = ->
  return if canvasSizeUpdateBlocked

  getGroup: -> @tessellation.group
  getTransitionFunc: -> @transitionFunc

  initialize: (config = new DefaultConfig)->
    [n,m] = config.getGrid()
    @tessellation = new Tessellation n, m
    console.log "Running knuth-bendix algorithm...."
    rewriteRuleset = knuthBendix vdRule @getGroup().n, @getGroup().m
    console.log "Finished"    
    @appendRewrite = makeAppendRewrite rewriteRuleset
    @getNeighbors = mooreNeighborhood @getGroup().n, @getGroup().m, @appendRewrite

    cellData = config.getCellData()
    if cellData
      console.log "import: #{cellData}"
      @importData cellData
    else
      @cells = new NodeHashMap
      @cells.put unity, 1
    
    @observer = new @ObserverClass @tessellation, @appendRewrite, minVisibleSize
    @observer.onFinish = -> redraw()
