define("smashThePigRules", ["componentmodelComponentApi", "slotModelApi", "clientkernelEventbusApi", "clientkernelModulesApi", "clientkernelLoggerApi", "moneyApi", "RulesDeclaration"],
    function(componentmodelComponentApi, slotModelApi, clientkernelEventbusApi, clientkernelModulesApi, clientkernelLoggerApi, moneyApi, RulesDeclaration) {
        "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 smashThePigRules == 'undefined') {
            var smashThePigRules = {};
        };
        smashThePigRules.LoggerFactory = function() {};
        smashThePigRules.LoggerFactory.__name__ = ["smashThePigRules", "LoggerFactory"];
        smashThePigRules.LoggerFactory.lf = null;
        smashThePigRules.LoggerFactory.init = function(lf) {
            smashThePigRules.LoggerFactory.lf = lf;
        };
        smashThePigRules.LoggerFactory.getLogger = function(name, that) {
            if (that != null) return smashThePigRules.LoggerFactory.getOrCreate(_hcmn_.Type.getClassName(_hcmn_.Type.getClass(that)).split(".").pop());
            else if (name != null && name != "") return smashThePigRules.LoggerFactory.getOrCreate(name);
            else return smashThePigRules.LoggerFactory.getOrCreate("slotmodel.mpt");
        };
        smashThePigRules.LoggerFactory.getOrCreate = function(key) {
            if (!smashThePigRules.LoggerFactory.loggers.exists(key)) {
                var value = smashThePigRules.LoggerFactory.lf.getLogger(key);
                smashThePigRules.LoggerFactory.loggers.set(key, value);
            }
            return smashThePigRules.LoggerFactory.loggers.get(key);
        };
        smashThePigRules.LoggerFactory.prototype.__class__ = smashThePigRules.LoggerFactory;
        smashThePigRules.ModuleActivator = function() {};
        smashThePigRules.ModuleActivator = ck_implements(smashThePigRules.ModuleActivator, clientkernelModulesApi.IModuleActivator);
        smashThePigRules.ModuleActivator.__interfaces__ = [clientkernelModulesApi.IModuleActivator];
        smashThePigRules.ModuleActivator.__name__ = ["smashThePigRules", "ModuleActivator"];
        smashThePigRules.ModuleActivator.prototype.factory = null;
        smashThePigRules.ModuleActivator.prototype.registration = null;
        smashThePigRules.ModuleActivator.prototype.start = function(context) {
            var _g = this;
            context.getServiceTracker([clientkernelLoggerApi.ILoggerFactory, clientkernelEventbusApi.IEventBus]).then(function(mContext) {
                smashThePigRules.LoggerFactory.init(mContext.getAnyService(clientkernelLoggerApi.ILoggerFactory, null));
                _g.factory = new smashThePigRules.RulesFactory(context);
                _g.registration = context.registerService(_g.factory, RulesDeclaration.RulesDeclaration, null);
            });
        };
        smashThePigRules.ModuleActivator.prototype.stop = function(context) {
            if (this.factory.rules != null) {
                this.registration.unregister();
                this.factory.rules.stop();
            }
        };
        smashThePigRules.ModuleActivator.main = function() {};
        smashThePigRules.ModuleActivator.prototype.__class__ = smashThePigRules.ModuleActivator;
        smashThePigRules.Rules = function(smPort) {
            this.logger = smashThePigRules.LoggerFactory.getLogger(null, this);
            this.smPort = smPort;
            this.helpInstance = null;
            this.paytableInstance = null;
            this.subscribe();
        };
        smashThePigRules.Rules.__name__ = ["smashThePigRules", "Rules"];
        smashThePigRules.Rules.prototype.logger = null;
        smashThePigRules.Rules.prototype.smPort = null;
        smashThePigRules.Rules.prototype.helpInstance = null;
        smashThePigRules.Rules.prototype.paytableInstance = null;
        smashThePigRules.Rules.prototype.subscriptions = null;
        smashThePigRules.Rules.prototype.subscribe = function() {
            this.subscriptions = [];
            this.subscriptions.push(this.smPort.getStages().getIdleStage().getOpenHelp().subscribe($bind(this, this.openHelp)));
            this.subscriptions.push(this.smPort.getStages().getIdleStage().getOpenPaytable().subscribe($bind(this, this.openPaytable)));
        };
        smashThePigRules.Rules.prototype.openHelp = function(event) {
            var _g = this;
            console.log("");
            var url = event.getPayload();
            this.logger.debug("openHelp with url: " + url);
            if (url == null || url == "") {
                this.logger.warn("url == null || url == \"\"");
                return;
            }
            if (window.document.getElementById("rulesInstance") != null) {
                this.closeHelp(null);
                return;
            }
            this.helpInstance = new smashThePigRules.RulesInstance(url, "Help", $bind(this, this.helpClosed));
            _hcmn_.Reflect.setProperty(window, "closeHelp", function() {
                var frame = window.document.getElementById("rulesInstance");
                var closeHelpEvt = window.document.createEvent("Event");
                closeHelpEvt.initEvent("closeHelp", false, false);
                window.dispatchEvent(closeHelpEvt);
                _g.closeHelp(null);
            });
        };
        smashThePigRules.Rules.prototype.openPaytable = function(event) {
            this.closeHelp(null);
        };
        smashThePigRules.Rules.prototype.closeHelp = function(event) {
            this.logger.debug("closeHelp");
            if (this.helpInstance == null) {
                this.logger.warn("helpInstance == null");
                return;
            }
            this.helpInstance.close();
        };
        smashThePigRules.Rules.prototype.helpClosed = function() {
            this.logger.debug("helpClosed");
            this.helpInstance.close();
            this.helpInstance = null;
        };
        smashThePigRules.Rules.prototype.closePaytable = function(event) {
            this.logger.debug("closePaytable");
            if (this.paytableInstance == null) {
                this.logger.warn("paytableInstance == null");
                return;
            }
            this.paytableInstance.close();
        };
        smashThePigRules.Rules.prototype.paytableClosed = function() {
            this.logger.debug("paytableClosed");
            this.paytableInstance = null;
        };
        smashThePigRules.Rules.prototype.stop = function() {
            this.unsubscribe();
        };
        smashThePigRules.Rules.prototype.unsubscribe = function() {
            if (this.subscriptions == null) return;
            while (this.subscriptions.length > 0) this.subscriptions.pop().unsubscribe();
            this.subscriptions = null;
        };
        smashThePigRules.Rules.prototype.__class__ = smashThePigRules.Rules;
        smashThePigRules.RulesFactory = function(moduleContext) {
            this.moduleContext = moduleContext;
        };
        smashThePigRules.RulesFactory = ck_implements(smashThePigRules.RulesFactory, clientkernelModulesApi.IService);
        smashThePigRules.RulesFactory = ck_implements(smashThePigRules.RulesFactory, componentmodelComponentApi.IComponentFactory);
        smashThePigRules.RulesFactory.__interfaces__ = [clientkernelModulesApi.IService, componentmodelComponentApi.IComponentFactory];
        smashThePigRules.RulesFactory.__name__ = ["smashThePigRules", "RulesFactory"];
        smashThePigRules.RulesFactory.prototype.moduleContext = null;
        smashThePigRules.RulesFactory.prototype.rules = null;
        smashThePigRules.RulesFactory.prototype.getComponentDeclaration = function() {
            return RulesDeclaration.RulesDeclaration;
        };
        smashThePigRules.RulesFactory.prototype.createComponent = function(componentContext) {
            console.log("[RulesFactory] createComponent");
            this.rules = new smashThePigRules.Rules(componentContext.getUsedPort(slotModelApi.ISlotModelPort));
        };
        smashThePigRules.RulesFactory.prototype.__class__ = smashThePigRules.RulesFactory;
        smashThePigRules.RulesInstance = function(url, id, closeCallback) {
            smashThePigRules.LoggerFactory.getLogger("RulesInstance").debug("Creating with url: " + url);
            this.logger = smashThePigRules.LoggerFactory.getLogger(null, this);
            this.id = id;
            this.closeCallback = closeCallback;
            var _this = window.document;
            this.iframe = _this.createElement("iframe");
            this.iframe.marginHeight = "0";
            this.iframe.marginWidth = "0";
            this.iframe.src = url;
            this.iframe.frameBorder = "0";
            this.iframe.setAttribute("style", "width:100%; height:100%; min-height:101%; background:white; position:absolute;");
            this.iframe.height = window.document.body.scrollHeight + "px";
            var _this1 = window.document;
            this.divWrapper = _this1.createElement("div");
            this.setDivWrapperStyle();
            this.divWrapper.appendChild(this.iframe);
            this.divWrapper.id = "rulesInstance";
            this.node = window.document.body.appendChild(this.divWrapper);
            window.addEventListener("resize", $bind(this, this.onResize));
            this.iframe.addEventListener("load", $bind(this, this.onLoad));
        };
        smashThePigRules.RulesInstance.__name__ = ["smashThePigRules", "RulesInstance"];
        smashThePigRules.RulesInstance.prototype.logger = null;
        smashThePigRules.RulesInstance.prototype.iframe = null;
        smashThePigRules.RulesInstance.prototype.node = null;
        smashThePigRules.RulesInstance.prototype.id = null;
        smashThePigRules.RulesInstance.prototype.closeCallback = null;
        smashThePigRules.RulesInstance.prototype.divWrapper = null;
        smashThePigRules.RulesInstance.prototype.onLoad = function(e) {
            this.iframe.height = this.iframe.contentWindow.document.body.scrollHeight + "px";
            this.setDivWrapperStyle();
        };
        smashThePigRules.RulesInstance.prototype.setDivWrapperStyle = function() {
            this.divWrapper.setAttribute("style", "overflow-x: hidden; overflow-y: auto; width:100%; height:100%; position: absolute; z-index: 99999; background:white; top:0px");
            this.divWrapper.style.setProperty("-webkit-overflow-scrolling", "touch");
            this.divWrapper.style.setProperty("-webkit-user-select", "none");
            this.divWrapper.style.setProperty("-webkit-text-size-adjust", "none");
        };
        smashThePigRules.RulesInstance.prototype.onResize = function() {
            this.setDivWrapperStyle();
            _hcmn_.haxe_Timer.delay($bind(this, this.updateIFrame), 0);
        };
        smashThePigRules.RulesInstance.prototype.updateIFrame = function() {
            if (this.iframe == null || this.iframe.contentWindow == null || this.iframe.contentWindow.document == null) {
                window.removeEventListener("resize", $bind(this, this.onResize));
                this.iframe.removeEventListener("load", $bind(this, this.onLoad));
                this.divWrapper = null;
                this.iframe = null;
                return;
            }
            this.iframe.height = this.iframe.contentWindow.document.body.scrollHeight + "px";
        };
        smashThePigRules.RulesInstance.prototype.closeListener = function(event) {
            if ("rules:close" + this.id == event.data) {
                window.document.removeEventListener("message", $bind(this, this.closeListener));
                this.close();
            }
        };
        smashThePigRules.RulesInstance.prototype.close = function() {
            if (this.iframe != null) this.logger.debug("Closing instance with url: " + this.iframe.src);
            if (this.node == null) return;
            if (this.node.parentNode != null) this.node.parentNode.removeChild(this.node);
            this.node = null;
            this.logger.debug("closeCallback: " + _hcmn_.Std.string(this.closeCallback));
            if (this.closeCallback != null) {
                this.logger.debug("closeCallback called");
                this.closeCallback();
            }
        };
        smashThePigRules.RulesInstance.prototype.__class__ = smashThePigRules.RulesInstance;
        smashThePigRules.LoggerFactory.loggers = new _hcmn_.haxe_ds_StringMap();;
        return {
            exports: smashThePigRules,
            activator: smashThePigRules.ModuleActivator
        };
    });
//# sourceURL=smashThePigRules.js