(function() {
    var bridgeEvents = APP.bridgeEvents = APP.bridgeEvents || {};
    APP.control = APP.control || {};

    //     bridgeEvents.consoleResized = function(_height) {
    //         // if (activating) {
    //         //APP.slider.style.webkitTransition = 'top .2s linear';
    //         //APP.slider.style.top = 0; //slide down
    //         // activating = 0;
    //         // }
    //     }

    bridgeEvents.register = register;

    function register(IGTcontrol) {
        var element;
        console.log(IGTcontrol);
        // debugger;

        switch (IGTcontrol.type) {
            case "balance":
                APP.control.balance = IGTcontrol;
                break;
            case 'boolean':
                element = document.createElement('input');
                element.setAttribute('type', 'checkbox');
                element.setAttribute('name', IGTcontrol.name);
                element.addEventListener('change', function() {
                    IGTcontrol.setValue(this.checked);
                });
                element.checked = IGTcontrol.value;
                element.disabled = !IGTcontrol.enabled;
                IGTcontrol.addEvent('change', function(value) {
                    element.checked = value;
                });
                IGTcontrol.addEvent('enable', function(enabled) {
                    element.disabled = !enabled;
                });
                options.appendChild(createControl(IGTcontrol.text, element));
                break;

            case 'list':
                switch (IGTcontrol.name) {
                    case 'sound':
                        var thisSpan = document.getElementById('soundSpan');
                        var thisButton = document.getElementById('sound');
                        try {
                            if (IGTcontrol.value == 0) {
                                thisSpan.checked = false;
                                IGTcontrol.setValue('0');
                                document.getElementById('sound').checked = true;
                            } else {
                                thisSpan.checked = true;
                                IGTcontrol.setValue('1');
                                document.getElementById('sound').checked = false;
                            }
                            thisButton.addEventListener('change', function() {
                                if (thisButton.checked) {
                                    thisSpan.checked = false;
                                    IGTcontrol.setValue('0');
                                } else {
                                    thisSpan.checked = true;
                                    IGTcontrol.setValue('1');
                                }
                            });
                            IGTcontrol.addEvent('change', function(value) {
                                if (value == 1) {
                                    document.getElementById('sound').checked = false;
                                } else {
                                    document.getElementById('sound').checked = true;
                                }
                                APP.slider.updateLayout();
                            });
                        } catch (err) {
                            console.log(
                                'Unable to find button with ID of ' +
                                IGTcontrol.name +
                                ' --- ' +
                                err
                            );
                        }
                        break;

                    default:
                        element = document.createElement('select');
                        element.setAttribute('name', IGTcontrol.name);
                        element.addEventListener('change', function() {
                            IGTcontrol.setValue(this.value);
                        });
                        IGTcontrol.values.forEach(function(value, index) {
                            var option = document.createElement('option');
                            option.setAttribute('value', value);
                            option.appendChild(
                                document.createTextNode(IGTcontrol.valueText[index])
                            );
                            element.appendChild(option);
                        });
                        element.value = IGTcontrol.value;
                        element.disabled = !IGTcontrol.enabled;
                        IGTcontrol.addEvent('change', function(value) {
                            element.value = value;
                            APP.slider.updateLayout();
                        });
                        IGTcontrol.addEvent('enable', function(enabled) {
                            element.disabled = !enabled;
                        });
                        break;
                }
                break;

            case 'command':
                var thisButton = document.getElementById(IGTcontrol.name);
                try {
                    thisButton.addEventListener('click', function() {
                        IGTcontrol.setValue();
                        APP.slider.hideConsole();
                    });
                    thisButton.addEventListener('enable', function(enabled) {
                        element.disabled = !enabled;
                    });
                } catch (err) {
                    console.log(
                        'Unable to find button with ID of ' +
                        IGTcontrol.name +
                        ' --- ' +
                        err
                    );
                }

                try {
                    if (keepAliveEventsFlag == true) {
                        keepAlive.triggerKeepAlive(keepAliveURL);
                    }
                } catch (err) {}
                break;

            case 'stake':
                break;

            case 'PromotionalFreeSpinsCount':
                element = document.createElement('span');
                IGTcontrol.addEvent('change', function(value, formattedValue) {
                    var thisField = document.getElementById(IGTcontrol.name + "Span").getElementsByTagName("input")[0];
                    thisField.value = formattedValue;
                    thisField.style.width = ((thisField.value.length + 1) * .5) + 'em';
                    APP.slider.updateLayout();

                    element.innerText = formattedValue;
                    APP.slider.updateLayout();

                    if (value == 0 && enabled) {
                        if (totalPayout == 0) {
                            document.getElementById('dialogStrong').innerHTML = translate('pfsMessage1');
                        } else {
                            document.getElementById('dialogStrong').innerHTML = translate('pfsMessageWin').replace('[freeSpinWin]', IGTbridge.currency.formatL(totalPayout));
                        }
                        document.getElementById('dialogNormal').innerHTML = translate('pfsMessage2');
                        document.getElementById('dialogButton1').style.visibility = 'hidden';
                        document.getElementById('dialogButton2Text').innerHTML = translate('pfsButton2');

                        var pfsParams = ["playMode", "freespin_stake_per_line", "freespin_lines", "freespin_tokenID", "freespin_num"];
                        if (typeof configData['general']['pfsParams'] != 'undefined') {
                            pfsParams = configData['general']['pfsParams'];
                        }

                        var secureToken = getSecureToken();

                        var cashPlayURL = window.location.protocol + '//' + window.location.host + '/skb/gateway';

                        for (i = 0; i < params.length; i++) {
                            var thisParam = params[i].split("=")[0];
                            if (pfsParams.indexOf(thisParam) == -1) {
                                cashPlayURL = buildURL(cashPlayURL, thisParam);
                            }
                        }


                        cashPlayURL = addSecureToken(cashPlayURL, secureToken);

                        document.getElementById('dialogButton2').href = cashPlayURL;
                        //document.getElementById('dialogButton2').setAttribute('target', '_self');
                        //Set Target
                        document.getElementById('dialogButton2').setAttribute('target', getTargetValue('pfsButton2'));
                        //setTargetValue(document.getElementById('dialogButton2'),'rcButton2','target2');

                        var thisPop = APP.slider.hidePopup();
                        APP.slider.showPopup(document.getElementById('dialogBox'));

                    } else if (value > 0 && firstLaunch && enabled) {
                        firstLaunch = false;

                        document.getElementById('dialogStrong').innerHTML = translate('pfsWelcome1');
                        document.getElementById('dialogNormal').innerHTML = translate('pfsWelcome2').replace('[freeSpins]', value);
                        document.getElementById('dialogButton1').style.visibility = 'hidden';
                        document.getElementById('dialogButton2Text').innerHTML = translate('pfsWelcomeButton2');
                        document.getElementById('dialogButton2').href = getURL('pfsWelcomeButton2');
                        //Note: Set Target not needed as this is Welcome screen

                        var thisPop = APP.slider.hidePopup();
                        APP.slider.showPopup(document.getElementById('dialogBox'));
                    }
                });
                break;

            case 'range':
            default:
        }
        //whenever content is appended, move the slider up
        element && APP.slider.updateLayout();
    }

})();