var Wick = Wick || {};
Wick.CookieChatBack = (function(){
    function init() {
        var cookieBar = $('#cookie-bar'),
            helpButton = $('.zEWidget-launcher'),
            backToTopButton = $("#back-to-top"),
            defaultMargin = 40,
            cookieMargin = 0,
            helpMargin = 0,
            tradeProMargin = 0;
    
        if (helpButton.length) {
            helpButton.css({
                visibility: 'visible',
                margin: defaultMargin
            });
            helpMargin = helpButton.height() + parseInt(helpButton.css('margin-bottom'));
        }

        if (cookieBar.length) {
            cookieMargin = cookieBar.height();
            helpMargin = 0;
            helpButton.css({
                visibility: 'hidden'
            });
        }

        if (navigator.userAgent.match(/TradeProMobile_iOS|TradeProMobile_Android|DIYMobile_iOS|DIYMobile_Android/i)) {
            tradeProMargin = 35;
        }

        backToTopButton.css({
            bottom: cookieMargin + defaultMargin + helpMargin + tradeProMargin,
            right: defaultMargin
        });
    }
    
    $(document).ready(function() {
        init();
        var cookieAccept = setInterval(function () {
            init();
            if (!$('#cookie-bar').length) {
                clearInterval(cookieAccept);
            }
        }, 2000);
    });
    
    $( window ).resize(_.debounce(function() {
        init();
    }, 200));
})();
