////////////////////////////////////////////////////////////////////////////////
// Copyright 2006-2008 James Pike

var dspFgColour = null
var dspBgColour = null
var mode = 'draw'
var iconGrab

function registerControls() {
    // get keypresses
    document.onkeypress = controlKeypressEvent
}

/* key presses ! */
var upKeycode = "k".charCodeAt(0) 
var leftKeycode = "h".charCodeAt(0)
var rightKeycode = "l".charCodeAt(0)
var downKeycode = "j".charCodeAt(0)
var zoominKeycode = "Z".charCodeAt(0)
var zoomoutKeycode = "z".charCodeAt(0)
var saveKeycode = "s".charCodeAt(0)
var clearKeycode = "c".charCodeAt(0)

function controlKeyPressed(keycode) {
    if (upKeycode == keycode) controlGoDirection(0, -1)
    else if (downKeycode == keycode) controlGoDirection(0, 1)
    else if (leftKeycode == keycode) controlGoDirection(-1, 0)
    else if (rightKeycode == keycode) controlGoDirection(1, 0)
    else if (zoominKeycode == keycode || zoomoutKeycode == keycode) {
        // fixme: assume it goes in increments of 2
        var newZoom = zoominKeycode == keycode ? zoom * 2 : zoom / 2
        if (newZoom >= 1 && newZoom <= 8) controlSetZoom(newZoom)
    }
    else if (clearKeycode == keycode) controlClear()
    else if (saveKeycode == keycode) controlSave()
}

function controlKeypressEvent(e) {
    var keycode
    if (window.event)
        keycode = window.event.keyCode
    else
        keycode = e.which
    controlKeyPressed(keycode)
}

/* colour selection */
function controlSetColour(e, colour) {
    /*var rightClick
    if (e.which) {
        rightClick = (e.which == 3);
    }
    else if (e.button) {
        rightClick = (e.button == 2);
    }*/
    var shiftClick = e.shiftKey

    if (shiftClick)
        controlSetBgColour(colour)
    else
        controlSetFgColour(colour)
}

var setFgColourEvent
function controlSetFgColour(colour) {
    dspFgColour.style.backgroundColor = '#' + colour
    setCookie('f', colour)
    setFgColourEvent(colour)
}

var setBgColourEvent
function controlSetBgColour(colour) {
    dspBgColour.style.backgroundColor = '#' + colour
    setCookie('b', colour)
    setBgColourEvent(colour)
}

var setZoomEvent
function controlSetZoom(zoomLevel) {
    setZoom(zoomLevel)
    setZoomEvent(zoomLevel)
}


var setLocationEvent
function controlSetLocation(x, y) {
    setLocation(x, y)
    setLocationEvent(x, y)
}

var modeEvent
function controlSetMode(mode_) {
    //if ('draw' == mode_)
    //    iconGrab.onclick = function() { controlSetMode('drag') }
    //else
    //    iconGrab.onclick = function() { controlSetMode('draw') }

    mode = mode_
    modeEvent(mode_)
}

var moveEvent
function controlGoDirection(xDiff, yDiff) {
    setLocation(mapX + xDiff, mapY + yDiff)
    moveEvent(xDiff, yDiff)
}

var autoSaveEvent
function controlAutoSave(autosave) {
    autoSaveEvent(autosave)
}

var saveEvent
function controlSave() {
    setStatus('Saving...')
    saveEvent()
}

var clearEvent
function controlClear() {
    clearEvent()
}

function initControls() {
    dspFgColour = document.getElementById('dspFgColour')
    dspBgColour = document.getElementById('dspBgColour')
    iconGrab = document.getElementById('iconGrab')

    restoreContext()

    registerControls()
    
    controlAutoSave(false)
    
    controlSetMode(mode);

    mapWidth = canvasWidth / (tileWidth * zoom)
    mapHeight = canvasHeight / (tileHeight * zoom)

    // try to set mapX and mapY from the # string
    controlSetLocation(mapX, mapY)

    // use control versions so options initially chosen are active
    controlSetFgColour(fgColour)
    controlSetBgColour(bgColour)

    // calling this control will trigger a load of all images and the cursor
    controlSetZoom(zoom)
    // looper();
    
    initStatus()
}
