     T: 84, U: 85, V: 86, W: 87, X: 88, Y: 89, Z: 90
 gh.KEYS = KEYS
 
-#automatic initialization
-document.onreadystatechange = ->
-    if @readyState is 'interactive'
-        (gh.init ? Greenhorn.start)()
-
-#listen for key events
-document.onkeydown = (e) ->
-    e.preventDefault()
-    Greenhorn.isDown[e.keyCode] = true
-document.onkeyup = (e) ->
-    e.preventDefault()
-    Greenhorn.isDown[e.keyCode] = false
-
-#asynchronous ID
-_masterID = null
-
-#draws on canvas once per frame
-_masterUpdate = ->
-    #clear previous frame
-    Greenhorn.clear()
-
-    #call custom update
-    gh.update?()
-
-    #draw all Sprites
-    Sprite._drawAll()
-
 #Engine class
 class Greenhorn extends EventEmitter
-    #keyboard input tracking array
-    @isDown = new Array 256
-    key = false for key in @isDown
-
-    #name of the current state
-    @currentState = 'STARTUP'
-
-    #create Engine elements
-    _elmnts =
-        main: document.createElement 'div'
-        title: document.createElement 'h1'
-        leftPanel: document.createElement 'div'
-        leftPanelHeader: document.createElement 'h3'
-        canvas: document.createElement 'canvas'
-        rightPanel: document.createElement 'div'
-        rightPanelHeader: document.createElement 'h3'
-        footer: document.createElement 'div'
+    #Greenhorn configuration object
+    @config:
+        frameRate: 25
+        title: 'GH-TITLE'
+        leftHeader: 'GH-LEFT-PANEL'
+        rightHeader: 'GH-RIGHT-PANEL'
+        footer: 'GH-FOOTER'
+        startUp:
+            size: 50
+            color: '#006400'
+            font: 'sans-serif'
+            text: 'CLICK HERE TO START!'
+    
+    #Greenhorn button defaults
+    @buttonDefaults:
+        type: 'button'
+        onclick: undefined
+        parent: 'gh-right-panel'
+        label: 'Launch the Missiles!'
+    
+    #asynchronous ID
+    _masterID = null
+    #draws on canvas once per frame
+    _masterUpdate = ->
+        Greenhorn.clear()
+        Greenhorn.emit 'update'
+        Sprite._drawAll()
+    
+    #keyboard input tracking array (read only)
+    _isDown = new Array 128
+    key = false for key in _isDown
+    
+    #listen for key events
+    document.onkeydown = (e) ->
+        e.preventDefault()
+        _isDown[e.keyCode] = true
+    document.onkeyup = (e) ->
+        e.preventDefault()
+        _isDown[e.keyCode] = false
+
+    #raw DOM manipulation
+    _main = document.createElement 'div'
+    _title = document.createElement 'h1'
+    _leftPanel = document.createElement 'div'
+    _leftPanelHeader = document.createElement 'h3'
+    @canvas = document.createElement 'canvas'
+    _rightPanel = document.createElement 'div'
+    _rightPanelHeader = document.createElement 'h3'
+    _footer = document.createElement 'div'
+    _context = @canvas.getContext '2d'
 
     #give id's to primary children
-    _elmnts.main.id = 'gh-main'
-    _elmnts.title.id = 'gh-title'
-    _elmnts.leftPanel.id = 'gh-left-panel'
-    _elmnts.canvas.id = 'gh-canvas'
-    _elmnts.rightPanel.id = 'gh-right-panel'
-    _elmnts.footer.id = 'gh-footer'
+    _main.id = 'gh-main'
+    _title.id = 'gh-title'
+    _leftPanel.id = 'gh-left-panel'
+    @canvas.id = 'gh-canvas'
+    _rightPanel.id = 'gh-right-panel'
+    _footer.id = 'gh-footer'
 
     #assign gh-h class to panelHeaders
-    _elmnts.leftPanelHeader.classList.add 'gh-h'
-    _elmnts.rightPanelHeader.classList.add 'gh-h'
+    _leftPanelHeader.classList.add 'gh-h'
+    _rightPanelHeader.classList.add 'gh-h'
 
     #append all primary children to main div
-    _elmnts.main.appendChild _elmnts.title
-    _elmnts.main.appendChild _elmnts.leftPanel
-    _elmnts.main.appendChild _elmnts.canvas
-    _elmnts.main.appendChild _elmnts.rightPanel
-    _elmnts.main.appendChild _elmnts.footer
+    _main.appendChild _title
+    _main.appendChild _leftPanel
+    _main.appendChild @canvas
+    _main.appendChild _rightPanel
+    _main.appendChild _footer
 
     #append headers to panels
-    _elmnts.leftPanel.appendChild _elmnts.leftPanelHeader
-    _elmnts.rightPanel.appendChild _elmnts.rightPanelHeader
+    _leftPanel.appendChild _leftPanelHeader
+    _rightPanel.appendChild _rightPanelHeader
 
     #keep track of mouse events over canvas
-    _elmnts.canvas.onmousemove = (e) ->
+    @canvas.onmousemove = (e) ->
         @mouseX = e.pageX
         @mouseY = e.pageY
-        Sprite.emitAll 'mouse:move'
-    _elmnts.canvas.onmousedown = (e) ->
-        Sprite.emitAll 'mouse:down'
-    _elmnts.canvas.onmouseup = (e) ->
-        Sprite.emitAll 'mouse:up'
-    _elmnts.canvas.ondblclick = (e) ->
-        Sprite.emitAll 'mouse:doubleClick'
-    _elmnts.canvas.oncontextmenu = (e) ->
+        Sprite.emitAll 'mouse:move', e
+    @canvas.onmousedown = (e) ->
+        Sprite.emitAll 'mouse:down', e
+    @canvas.onmouseup = (e) ->
+        Sprite.emitAll 'mouse:up', e
+    @canvas.ondblclick = (e) ->
+        Sprite.emitAll 'mouse:doubleClick', e
+    @canvas.oncontextmenu = (e) ->
         e.preventDefault()
-        Sprite.emitAll 'mouse:rightClick'
-
-    #start all asynchronous functions
-    _startEverything = ->
-        _masterID = setInterval _masterUpdate, 1000 / env.FRAME_RATE
-        Sprite._startAll()
-        Sound._playAll()
-        return
+        Sprite.emitAll 'mouse:rightClick', e
+    
+    #keyPress tester
+    @isDown = (key) ->
+        _isDown[key]
 
     #mouse position getters
     @getMouseX = ->
-        _elmnts.canvas.mouseX - _elmnts.canvas.offsetLeft - _elmnts.canvas.width / 2
-
+        @canvas.mouseX - @canvas.offsetLeft - @canvas.width / 2
     @getMouseY = ->
-        -(_elmnts.canvas.mouseY - _elmnts.canvas.offsetTop - _elmnts.canvas.height / 2)
+        -@canvas.mouseY + @canvas.offsetTop + @canvas.height / 2
 
     #add button to one of the panels
     @addButton = (config = {}) ->
         #add missing keys to config
-        for own key, value of env.BUTTON_DEFAULT_CONFIG
+        for own key, value of @buttonDefaults
             config[key] ?= value
 
         #create element

sedabull@gmail.com
###

#keyboard value mapping object
KEYS =
    ESC: 27, SPACE: 32, PGUP: 33
    PGDOWN: 34, END: 35, HOME: 36
    LEFT: 37, UP: 38, RIGHT: 39, DOWN: 40

        clearInterval _masterID
        _masterID = null
    @clear = ->
        _context.clearRect -@canvas.width / 2, -@canvas.height / 2, @canvas.width, @canvas.height
    
    #start function + helpers
    _firstTime = true
    _start = ->
        _masterID = setInterval _masterUpdate, 1000 / @config.frameRate
        Sprite._startAll()
        Sound._playAll()
        return
    @start = (stateName) =>
        #prevent starting without properly stopping first
        unless @isRunning()
            if _firstTime
                #add engine to a user defined '#gh' div
                if mainDiv = document.querySelector '#gh'
                    mainDiv.appendChild _elmnts.main
                else
                    console.log "There is no '#gh' element!"

                #set the innerHTML of each element
                _title.innerHTML = @config.title
                _leftPanelHeader.innerHTML = @config.leftHeader
                _rightPanelHeader.innerHTML = @config.rightHeader
                _footer.innerHTML = @config.footer
                @canvas.innerHTML = 'Your browser does not support the &ltcanvas&gt tag'

                #set the size of the canvas
                @canvas.width = @canvas.clientWidth
                @canvas.height = @canvas.clientHeight

                #center the canvas origin point
                _context.translate @canvas.width / 2, @canvas.height / 2

                #draw the start screen
                _context.save()
                _context.globalAlpha = 1.0
                _context.textAlign = 'center'
                _context.textBaseline = 'middle'
                _context.font = "#{@config.startUp.size}px #{@config.startUp.font}"
                _context.fillStyle = @config.startUp.color
                _context.fillText @config.startUp.text, 0, 0
                _context.restore()

                #make the entire canvas a start button
                @canvas.onclick = ->
                    _start()
                    Greenhorn.changeState stateName ? 'GREENHORN'
                    @canvas.onclick = (e) ->
                        Sprite.emitAll 'mouse:click', e

                #don't do all this a second time
                _firstTime = false
            else
                _start()
                if stateName then @changeState stateName

#add to namespace object
