// com.tts.initial.html5.pausebutton
// HTML4 / HTML5: No differences
define([
    "./togglebutton.builder",
    "./locals",
    "jquery",
    "./accessibility.builder"
    ], function (
        ToggleButtonBuilder,
        uiText,
        jq,
        AccessibilityBuilder
    ) {

	"use strict";

	/**
	 * @constructor
	 */
	return function (context, metadata) {

		var shellElement,
            button,
            buttonInitialized = false,
            instance,
			hasStageTriggerSupport = true,
			isInAuthoringMode = false,
            pointerUpEventNames,
            pointerDownEventNames,
            pointerMoveEventNames,
            pointerLeaveEventNames,
            accessibilityTooltip,
            accessibilityKey = metadata.parameters.get( "accessibility-key", null ),
            accessibilityLocation = metadata.parameters.asNumber( "accessibility-location", 0 ),
            accessibilityPositionX = metadata.parameters.asNumber( "accessibility-position-x", -6 ),
            accessibilityPositionY = metadata.parameters.asNumber( "accessibility-position-y", -6 );

		function initializeButton(shellElement) {
            if( buttonInitialized ){
                return;
            }
            buttonInitialized = true;

            var imageParams = loadImage("pause", metadata.parameters.get("bg_image", null));

            button = new ToggleButtonBuilder({
                image : imageParams,
                instance : instance,
                tooltip : uiText.get("player.navigation.button.pause", metadata.stage.getContentLanguage()),
            }, context, metadata).build();

            shellElement.css({
                "width" : imageParams.width / 2 + "px",
                "height" : imageParams.height / 3 + "px"
            });

            shellElement.append( button.getElement() );

            if ( accessibilityKey ) {
                accessibilityTooltip = new AccessibilityBuilder( accessibilityKey, accessibilityLocation, accessibilityPositionX, accessibilityPositionY ).build();
                shellElement.append( accessibilityTooltip.getElement() );
            }

            if (isInAuthoringMode) {
                createAuthoringShield();
            }
        }

        function loadImage(key, image) {
            var result = {};

                if (image) {
                    result.fileNameURL = context.stage.getSharedPath() + image.fileName;
                    result.width = image.width;
                    result.height = image.height;
                }
                else {
                    switch(key){
                        case "pause": 
                            result.fileNameURL = context.getPath() + "pause.png";
                            result.width = 100;
                            result.height = 150;
                            break;
                        default:
                            return null;
                    } 
                }      

            return result;
        }

        // create an object shield so we are able to select and drag element
		function createAuthoringShield() {
			shellElement.append(jq(document.createElement("div"))
				.css({
					"position": "absolute",
					"left": 0,
					"top": 0,
					"background-color": "#ffffff",
					"opacity": "0.4",
					"width": "100%",
					"height": "100%",
					"z-index": "9999"
				}));
		}

		function unlockNavigation() {
			if (isInAuthoringMode || !hasStageTriggerSupport) {
				return;
			}
			context.stage.trigger.unlock( metadata.getId() );
		}

        function getHotspotElementNodeName() {
            return button.getElement().nodeName
                ? button.getElement().nodeName
                : "document";
        }

        function initPointerEventNames() {
            if( !pointerUpEventNames ) {
                var nodeName = getHotspotElementNodeName();
                pointerDownEventNames = context.device.getDownEvents( nodeName );
                pointerUpEventNames = context.device.getUpEvents( nodeName );
                pointerMoveEventNames = context.device.getMoveEvents( nodeName );
                pointerLeaveEventNames = context.device.getReleaseEvents( nodeName );
            }
        }

        function bindEvents() {
			if (isInAuthoringMode) {
				return;
			}

			if (!button) {
				return;
			}

			removeEvents();
            context.device.attachFirstSupportedEvent( button.getElement(), pointerDownEventNames, button.onMouseDown );
            context.device.attachFirstSupportedEvent( button.getElement(), pointerMoveEventNames, button.onMouseOver );
            context.device.attachFirstSupportedEvent( button.getElement(), pointerLeaveEventNames, button.onMouseLeave );
            context.device.attachFirstSupportedEvent( button.getElement(), pointerUpEventNames, button.onMouseUp );
        }

		function removeEvents() {
			unlockNavigation();
			if (!button) {
				return;
			}

            initPointerEventNames();
            context.device.removeFirstSupportedEvent( button.getElement(), pointerDownEventNames, button.onMouseDown );
            context.device.removeFirstSupportedEvent( button.getElement(), pointerMoveEventNames, button.onMouseOver );
            context.device.removeFirstSupportedEvent( button.getElement(), pointerLeaveEventNames, button.onMouseLeave );
            context.device.removeFirstSupportedEvent( button.getElement(), pointerUpEventNames, button.onMouseUp );
        }

		// revealing public API
		return {
			onLoading: function () {
                context.page.event.bindOnEnter(this);
                context.page.event.bindOnExit( this );
                context.stage.accessibility.bindOnAccessibilityModeChanged( this );

                instance = this;

                hasStageTriggerSupport = !!context.stage.trigger;
				isInAuthoringMode = !!context.stage.isInAuthoringMode();

                shellElement = jq(context.getElement());
                
                initializeButton(shellElement);
			},

			onEnterPage: function () {
                bindEvents();
            },
            
            onExitPage: function(){
                removeEvents();
            },

            onAccessibilityModeChanged: function ( dataHTML5, dataHTML4 ) {
                var params = dataHTML4 || dataHTML5;

                if ( params.isActive ) {
                    accessibilityTooltip.show();
                } else {
                    accessibilityTooltip.hide();
                }
            },

            onAccessibilityKeyPressed: function ( dataHTML5, dataHTML4 ) {
                var params = dataHTML4 || dataHTML5;

                if ( params.keyCode == accessibilityTooltip.getKeyCode() ) {
                    button.pause();
                }
            }
		};
	};
});
