define("smashThePigBackground", ["viewBuilderApi", "componentmodelComponentApi", "clientkernelLoaderApi", "displayEngineApi", "clientkernelEventbusApi", "smashthepigComponentsBackgroundPort", "clientkernelModulesApi"],
    function(viewBuilderApi, componentmodelComponentApi, clientkernelLoaderApi, displayEngineApi, clientkernelEventbusApi, smashthepigComponentsBackgroundPort, clientkernelModulesApi) {
        "use strict";

        function $iterator(o) {
            if (o instanceof Array) return function() {
                return {
                    cur: 0,
                    arr: o,
                    hasNext: function() {
                        return this.cur < this.arr.length;
                    },
                    next: function() {
                        return this.arr[this.cur++];
                    }
                };
            };
            return typeof(o.iterator) == 'function' ? $bind(o, o.iterator) : o.iterator;
        };;
        var $fid = 0;

        function $bind(o, m) {
            if (m == null) return null;
            if (m.__id__ == null) m.__id__ = $fid++;
            var f;
            if (o.hx__closures__ == null) o.hx__closures__ = {};
            else f = o.hx__closures__[m.__id__];
            if (f == null) {
                f = function() {
                    return f.method.apply(f.scope, arguments);
                };
                f.scope = o;
                f.method = m;
                o.hx__closures__[m.__id__] = f;
            }
            return f;
        };
        var $_;
        var $estr = function() {
            return _hcmn_.js_Boot.__string_rec(this, '');
        };
        if (typeof haxe == 'undefined') {
            var haxe = {};
        };

        haxe.IMap = function() {}
        haxe.IMap.prototype = {

        };
        if (typeof smashThePigBackground == 'undefined') {
            var smashThePigBackground = {};
        };
        smashThePigBackground.Activator = function(context) {};
        smashThePigBackground.Activator = ck_implements(smashThePigBackground.Activator, clientkernelModulesApi.IModuleActivator);
        smashThePigBackground.Activator.__interfaces__ = [clientkernelModulesApi.IModuleActivator];
        smashThePigBackground.Activator.__name__ = ["smashThePigBackground", "Activator"];
        smashThePigBackground.Activator.prototype.start = function(context) {
            context.getServiceTracker([clientkernelLoaderApi.IAssetRegistry]).then(function(context1) {
                smashThePigBackground.ExternalService.initialize(context1);
                context1.registerService(new smashThePigBackground.BackgroundFactory(context1), smashthepigComponentsBackgroundPort.BackgroundDeclaration, null);
            });
        };
        smashThePigBackground.Activator.prototype.stop = function(context) {};
        smashThePigBackground.Activator.main = function() {};
        smashThePigBackground.Activator.prototype.__class__ = smashThePigBackground.Activator;
        smashThePigBackground.Background = function(context) {
            this.image_url = null;
            var _g = this;
            this.context = context;
            this.port = context.getImplementedPort(smashthepigComponentsBackgroundPort.IBackgroundImplementationPort);
            smashThePigBackground.ExternalService.assetRegistry.getAsset("smash-the-pig-background-configuration").getContent().then(function(cfg) {
                _g.config = cfg;
                _g.port.getChange().subscribe($bind(_g, _g.handleChange));
                _g.port.prepareBackground().subscribe($bind(_g, _g.loadBackground));
                if (window.document.getElementById("backgroundContainer") == null) {
                    var _this = window.document;
                    _g.backgroundContainer = _this.createElement("div");
                    _g.backgroundContainer.id = "backgroundContainer";
                    _g.backgroundContainer.style.backgroundImage = "";
                    window.document.body.appendChild(_g.backgroundContainer);
                }
            });
        };
        smashThePigBackground.Background.__name__ = ["smashThePigBackground", "Background"];
        smashThePigBackground.Background.prototype.context = null;
        smashThePigBackground.Background.prototype.port = null;
        smashThePigBackground.Background.prototype.backgroundContainer = null;
        smashThePigBackground.Background.prototype.config = null;
        smashThePigBackground.Background.prototype.image_url = null;
        smashThePigBackground.Background.prototype.handleChange = function(e) {
            this.backgroundContainer.className = "fade";
            new smashThePigBackground.Crossfade(this.image_url, this.config.fadeSpeed, $bind(this, this.onImageSet));
        };
        smashThePigBackground.Background.prototype.loadBackground = function(e) {
            var _g = this;
            var image_no = e.getPayload().image_no;
            this.getImageAsset(image_no).getContent().then(function(content) {
                _g.image_url = _g.getImageAsset(image_no).getUrl();
                if (_g.backgroundContainer.style.backgroundImage == "") _g.backgroundContainer.style.backgroundImage = "url('" + _g.image_url + "')";
            });
        };
        smashThePigBackground.Background.prototype.getImageAsset = function(imageNum) {
            var imgId = this.config.backgrounds[imageNum];
            return smashThePigBackground.ExternalService.assetRegistry.getAsset(imgId);
        };
        smashThePigBackground.Background.prototype.onImageSet = function() {
            this.port.getChanged().publish(null);
        };
        smashThePigBackground.Background.prototype.__class__ = smashThePigBackground.Background;
        smashThePigBackground.BackgroundFactory = function(context) {
            this.context = context;
        };
        smashThePigBackground.BackgroundFactory = ck_implements(smashThePigBackground.BackgroundFactory, clientkernelModulesApi.IService);
        smashThePigBackground.BackgroundFactory = ck_implements(smashThePigBackground.BackgroundFactory, componentmodelComponentApi.IComponentFactory);
        smashThePigBackground.BackgroundFactory.__interfaces__ = [clientkernelModulesApi.IService, componentmodelComponentApi.IComponentFactory];
        smashThePigBackground.BackgroundFactory.__name__ = ["smashThePigBackground", "BackgroundFactory"];
        smashThePigBackground.BackgroundFactory.prototype.vbf = null;
        smashThePigBackground.BackgroundFactory.prototype.context = null;
        smashThePigBackground.BackgroundFactory.prototype.getComponentDeclaration = function() {
            return smashthepigComponentsBackgroundPort.BackgroundDeclaration;
        };
        smashThePigBackground.BackgroundFactory.prototype.createComponent = function(context) {
            new smashThePigBackground.Background(context);
        };
        smashThePigBackground.BackgroundFactory.prototype.__class__ = smashThePigBackground.BackgroundFactory;
        smashThePigBackground.Crossfade = function(img_url, step, callback) {
            this.callbackDelay = 650;
            this.opacity = 0;
            if (smashThePigBackground.Crossfade.style == null) smashThePigBackground.ExternalService.assetRegistry.getAsset("smash-the-pig-background-css").getContent().then(function(data) {
                smashThePigBackground.Crossfade.style = data;
                window.document.head.appendChild(smashThePigBackground.Crossfade.style);
            });
            this.img_url = img_url;
            this.step = step;
            this.callback = callback;
            this.start();
        };
        smashThePigBackground.Crossfade.__name__ = ["smashThePigBackground", "Crossfade"];
        smashThePigBackground.Crossfade.prototype.img_url = null;
        smashThePigBackground.Crossfade.prototype.step = null;
        smashThePigBackground.Crossfade.prototype.tempDiv = null;
        smashThePigBackground.Crossfade.prototype.opacity = null;
        smashThePigBackground.Crossfade.prototype.callback = null;
        smashThePigBackground.Crossfade.prototype.callbackDelay = null;
        smashThePigBackground.Crossfade.prototype.start = function() {
            this.tempDiv = window.document.getElementById("backgroundContainer");
            if (_hcmn_.Std.parseInt(this.tempDiv.style.opacity) == 1) this.tempDiv.style.opacity = "0";
            else this.tempDiv.style.opacity = "1";
            this.end();
        };
        smashThePigBackground.Crossfade.prototype.end = function() {
            if (this.callback != null) _hcmn_.haxe_Timer.delay(this.callback, this.callbackDelay);
        };
        smashThePigBackground.Crossfade.style = null;
        smashThePigBackground.Crossfade.prototype.__class__ = smashThePigBackground.Crossfade;
        smashThePigBackground.ExternalService = function() {};
        smashThePigBackground.ExternalService.__name__ = ["smashThePigBackground", "ExternalService"];
        smashThePigBackground.ExternalService.assetRegistry = null;
        smashThePigBackground.ExternalService.variableRegistry = null;
        smashThePigBackground.ExternalService.initialize = function(context) {
            smashThePigBackground.ExternalService.assetRegistry = context.getAnyService(clientkernelLoaderApi.IAssetRegistry, null);
            smashThePigBackground.ExternalService.variableRegistry = context.getAnyService(clientkernelLoaderApi.IVariableRegistry, null);
        };
        smashThePigBackground.ExternalService.prototype.__class__ = smashThePigBackground.ExternalService;
        smashThePigBackground.Background.CFG_ID = "smash-the-pig-background-configuration";;
        return {
            exports: smashThePigBackground,
            activator: smashThePigBackground.Activator
        };
    });
//# sourceURL=smashThePigBackground.js