define( [
    "jquery",
    "./key.code"
],
    function (
        jq,
        KeyCode
    ) {

        /**
         * @constructor
         */
        return function ( accessibilityKey, accessibilityLocation, accessibilityPositionX, accessibilityPositionY, context ) {

            var accessibilityTooltip,
                keyOptions = KeyCode.getKeyOptions( accessibilityKey );

            function createAccessibilityTooltip() {
                if (keyOptions) {
                    var accessibilityPositionCss = getAccessibilityPosition(accessibilityLocation, accessibilityPositionX, accessibilityPositionY);

                    accessibilityTooltip = jq(document.createElement("div"))
                        .css({
                            "position": "absolute",
                            "z-index": "99",
                            "border": "0 solid white",
                            "padding": "0 5px",
                            "width": "auto",
                            "height": "18px",
                            "font-size": "12px",
                            "font-family": "arial, helvetica, sans-serif",
                            "background-color": "#FFFFAA",
                            "vertical-align": "middle",
                            "text-align": "center"
                        })
                        .hide();
                    accessibilityTooltip.css(accessibilityPositionCss);
                    accessibilityTooltip.html(keyOptions["label"]);
                }

                function getAccessibilityPosition( accessibilityLocation, accessibilityPositionX, accessibilityPositionY ) {
                    var accessibilityPositions = [
                        //top-left
                        {
                            "top": "0px",
                            "left": "0px",
                            "margin-left": "-6px",
                            "margin-top": "-6px"
                        },
                        //top-right
                        {
                            "top": "0px",
                            "right": "0px",
                            "margin-right": "-6px",
                            "margin-top": "-6px"
                        },
                        //bottom-left
                        {
                            "bottom": "0px",
                            "left": "0px",
                            "margin-left": "-6px",
                            "margin-bottom": "-6px"
                        },
                        //bottom-right
                        {
                            "bottom": "0px",
                            "right": "0px",
                            "margin-right": "-6px",
                            "margin-bottom": "-6px"
                        }
                    ];

                    if ( accessibilityLocation != 4 ) {
                        return accessibilityPositions[accessibilityLocation];
                    } else {
                        return {
                            "top": "0px",
                            "left": "0px",
                            "margin-left": accessibilityPositionX + "px",
                            "margin-top": accessibilityPositionY + "px"
                        };
                    }
                }

                function registerTabNavigation(id, order) {
                    var alreadyRegistered = false,
                        registeredTabNavigations = context.stage.variable.get("registeredTabNavigations", "tts");

                    if (!registeredTabNavigations || !(registeredTabNavigations.length > 0)) {
                        registeredTabNavigations = new Array();
                    } else {
                        for (var i = 0; i < registeredTabNavigations.length; i++) {
                            if (registeredTabNavigations[i].id == id) {
                                alreadyRegistered = true;
                                break;
                            }
                        }
                    }

                    if (!alreadyRegistered) {
                        registeredTabNavigations.push({ id: id, order: order });
                        context.stage.variable.set("registeredTabNavigations", registeredTabNavigations, "tts");
                    }
                }

                return {
                    getElement: function () {
                        if (keyOptions) {
                            return accessibilityTooltip.get(0);
                        } else {
                            return false;
                        }
                    },
                    show: function () {
                        if (keyOptions) {
                            accessibilityTooltip.show();
                        }
                    },
                    hide: function () {
                        if (keyOptions) {
                            accessibilityTooltip.hide();
                        }
                    },
                    getKeyCode: function () {
                        if (keyOptions) {
                            return keyOptions["keyCode"];
                        } else {
                            return false;
                        }
                    },
                    registerTabNavigation: function (id, order) {
                        registerTabNavigation(id, order);
                    }
                }
            }

            return {
                build: createAccessibilityTooltip
            };
        };
    } );
