// com.tts.initial.html5.backgroundmusic
// HTML4 / HTML5: No differences
define([
	"jquery"
],
	function (
		jq
	) {

	"use strict";

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

		var instance,
			shellElement,
			isInitialized = false,
			fileName = loadFile("silence", metadata.parameters.get("filename", null)),
			volume = parseFloat(metadata.parameters.get("volume")),
			loop = metadata.parameters.asBool("loop", true),
			backgroundAudioElement,
			isInAuthoringMode;

		function loadFile(key, filename) {
			if (filename) {
				return context.stage.getSharedPath() + filename.fileName;
			} else {
				return context.getPath() + "silence.mp3";
			}
		}

		function createBackgroundAudio() {
			if (isInitialized) {
				return;
			}
			isInitialized = true;

			shellElement.attr({
				"aria-hidden": "true",
				"speak": "none",
				"tabindex": "-1"
			})
				.css({
					"width": metadata.getWidth() + "px",
					"height": metadata.getHeight() + "px",
					"display": "none",
					"visibility": "hidden"
				});

			if (isInAuthoringMode) {
				createAuthoringShield(shellElement);
			}
		}

		function getCurrentAudioToggleState() {
			return context.stage.audio.isOn();
		}

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

		// revealing public API
		return {
			onLoading: function () {
				instance = this;

				context.page.event.bindOnEnter(instance);
				context.stage.event.bindOnAudioControlChanged(instance);

				isInAuthoringMode = !!context.stage.isInAuthoringMode();
				shellElement = jq(context.getElement());

				createBackgroundAudio();
			},

			onEnterPage: function () {
				if (!(window.top.document.getElementById("background-audio"))) {
					backgroundAudioElement = document.createElement('audio');
					backgroundAudioElement.id = 'background-audio';
					backgroundAudioElement.controls = false;
					backgroundAudioElement.src = fileName;
					backgroundAudioElement.type = 'audio/mpeg';
					backgroundAudioElement.volume = volume;
					backgroundAudioElement.loop = loop;
					backgroundAudioElement.autoplay = true;

					window.top.document.body.appendChild(backgroundAudioElement);
					document.body.addEventListener("click", function () {
						backgroundAudioElement.play();
					});

					if (!(context.stage.audio.isOn())) {
						backgroundAudioElement.muted = true;
					}
				}
			},
			onAudioControlChanged: function () {
				var backgroundAudioElement = window.top.document.getElementById("background-audio");

				if (backgroundAudioElement) {
					if (context.stage.audio.isOn()) {
						backgroundAudioElement.muted = false;
					} else {
						backgroundAudioElement.muted = true;
					}
				}
			}
		};
	};
});
