var Wick = Wick || {};
Wick.StickyHeader = (function () {

    var position = $(window).scrollTop(),
        headerTopLine = $('.header__top-line'),
        headerWrapper = $('.header-wrapper'),
        stickyHeaderWrapper = $('.sticky-header-wrapper'),
        stickyHeaderWrapperHeight = headerWrapper.height(),
        flag = 1;
    stickyHeaderWrapper.height(stickyHeaderWrapperHeight);

    $(window).scroll(function () {
        var mainNav = $('.main-nav.js-visible'),
            mainNavDisplay = mainNav.css('display');

        var scroll = $(window).scrollTop();

        if (mainNavDisplay !== 'block') {
            if (scroll > position && flag === 1) {
                hideTop(scroll, position);
            } else if (scroll < position && flag === 0) {
                showTop(scroll, position);
            }
        } else if (mainNavDisplay === 'block' && flag === 0){
            showTop(scroll, position);
        }
        position = scroll;
    });


    function hideTop(pageOffsetTop, scrolledPosition) {
        if (pageOffsetTop + scrolledPosition <= 0) {
            return;
        }
        headerTopLine.each(function (index) {
            headerTopLine[index].classList.add('header-top-line-hide');
        });

        flag = 0;
    }

    function showTop() {
        headerTopLine.each(function (index) {
            headerTopLine[index].classList.remove('header-top-line-hide');
        });

        flag = 1;
    }

    function goBack() {
        $('.header-back-btn').click(function(e){
            e.stopPropagation();
            e.preventDefault();
            if (navigator.userAgent.match(/TradeProMobile_iOS|TradeProMobile_Android|DIYMobile_iOS|DIYMobile_Android/i)) {
	            window.history.back();
            }
        });
    }

    goBack();

})();
