function KeyHandler(object)
{
    this.containerIdentFunction = null;
    this.object = object;
    this.registeredEvents = new Array();
}


KeyHandler.prototype.handleEvent = _keyHandler_handleEvent;
KeyHandler.prototype.registerEvent = _keyHandler_registerEvent;
KeyHandler.prototype.getContainerIdentFunction =
    _keyHandler_getContainerIdentFunction;
KeyHandler.prototype.setContainerIdentFunction =
    _keyHandler_setContainerIdentFunction;


function _keyHandler_handleEvent(event)
{
    var target = null;
    var keyCode = null;

    if (! event)
    {
        event = window.event;
    }

    if (event.target)
    {
        target = event.target;
    }
    else if (event.srcElement)
    {
        target = event.srcElement;
    }

    if (target != null && target.nodeType == 3)
    {
        target = target.parentNode;
    }

    var targetId = target != null && target.id != null && target.id != "" ?
        String(target.id) : null;


    if (event.which)
    {
        keyCode = event.which;
    }
    else if (event.keyCode)
    {
        keyCode = event.keyCode;
    }

    var altKeyId = event.altKey ? 1 : 0;
    var ctrlKeyId = event.ctrlKey ? 1 : 0;
    var shiftKeyId = event.shiftKey ? 1 : 0;

    /*
    notification(0, "Last pressed key: " + (altKeyId ? "[alt]" : "") +
        (ctrlKeyId ? "[ctrl]" : "") + (shiftKeyId ? "[shift]" : "") + keyCode +
        (target != null && String(target.id) != null &&
            String(target.id) != "" ? (" on " + String(target.id)) : ""));
    */
    //notification(0, "Registered Keys: " + this.registeredEvents.length);
    //notification(0, "Container: " + this.getContainerIdentFunction());

    //if (altKeyId == 1 || ctrlKeyId == 1 || shiftKeyId == 1)
    //{
    //    event.preventDefault();
    //}

    if (this.registeredEvents != null)
    {
        if (this.registeredEvents[altKeyId])
        {
            if (this.registeredEvents[altKeyId][ctrlKeyId])
            {
                if (this.registeredEvents[altKeyId][ctrlKeyId][shiftKeyId])
                {
                    if (this.registeredEvents
                        [altKeyId][ctrlKeyId][shiftKeyId][keyCode])
                    {
                        var keyFunctions = this.registeredEvents
                            [altKeyId][ctrlKeyId][shiftKeyId][keyCode];

                        var keyFunction = null;

                        if (keyFunctions[targetId])
                        {
                            keyFunction = keyFunctions[targetId];
                        }
                        else
                        {
                            for (var nextTargetNodeId in keyFunctions)
                            {
                                var nextTargetNode =
                                    this.getChildNodeById(nextTargetNodeId);

                                if (nextTargetNode == null)
                                {
                                    nextTargetNode = this.getChildNodeByName(
                                        nextTargetNodeId);
                                }

                                if (nextTargetNode != null)
                                {
                                    if (getChildNodeById(nextTargetNode,
                                            targetId) != null ||
                                        getChildNodeByName(nextTargetNode,
                                            targetId) != null)
                                    {
                                        keyFunction =
                                            keyFunctions[nextTargetNodeId];
                                    }
                                }
                            }

                            if (keyFunction == null && keyFunctions["null"])
                            {
                                keyFunction = keyFunctions["null"];
                            }
                        }

                        if (keyFunction != null)
                        {
                            event.preventDefault();

                            //notification(0, keyFunction);

                            keyFunction(target);
                        }
                    }
                }
            }
        }
    }
}


function _keyHandler_registerEvent(
    altKey, ctrlKey, shiftKey, keyCode, nodeId, functionToExcute)
{
    var altKeyId = altKey ? 1 : 0;
    var ctrlKeyId = ctrlKey ? 1 : 0;
    var shiftKeyId = shiftKey ? 1 : 0;
    nodeId = nodeId != null && nodeId != "" ? nodeId : "null";

    //notification(0, "Adding key: " + (altKeyId ? "[alt]" : "") +
    //    (ctrlKeyId ? "[ctrl]" : "") + (shiftKeyId ? "[shift]" : "") + keyCode +
    //    (nodeId != "null" ? (" on " + nodeId) : "") +
    //    (this.getContainerIdentFunction() != null ?
    //        (" in container " + this.getContainerIdentFunction()) : ""));

    if (! this.registeredEvents[altKeyId])
    {
        this.registeredEvents[altKeyId] = new Array();
    }

    if (! this.registeredEvents[altKeyId][ctrlKeyId])
    {
        this.registeredEvents[altKeyId][ctrlKeyId] = new Array();
    }

    if (! this.registeredEvents[altKeyId][ctrlKeyId][shiftKeyId])
    {
        this.registeredEvents[altKeyId][ctrlKeyId][shiftKeyId] = new Array();
    }

    if (! this.registeredEvents[altKeyId][ctrlKeyId][shiftKeyId][keyCode])
    {
        this.registeredEvents[altKeyId][ctrlKeyId][shiftKeyId][keyCode] =
            new Array();
    }

    this.registeredEvents[altKeyId][ctrlKeyId][shiftKeyId][keyCode][nodeId] =
        functionToExcute;
}


function _keyHandler_getContainerIdentFunction()
{
    return this.containerIdentFunction;
}


function _keyHandler_setContainerIdentFunction(containerIdentFunction)
{
    this.containerIdentFunction = containerIdentFunction;

    if (this.getObject() != null)
    {
        this.getObject().onkeydown = new Function(
            "event", containerIdentFunction + ".handleEvent(event);");
    }
}
