jQuery(function () {
    'use strict';

    var indexURL = jQuery('link[rel="header_link"]').attr('href');
    var iframeTagPart = ['<iframe allowtransparency="true" ' , 'background-color:transparent;filter:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0);"></iframe>'];
    if(indexURL && jQuery('header.clickable').length) {
        if(jQuery('#bd-header-bg').length) {
            jQuery('header').append(
                (jQuery.browser.msie ? iframeTagPart[0] + 'style="position:absolute;height:100%;width:40000px;left:-20000px;' + iframeTagPart[1] : '' ) +
                '<a style="position:absolute;height:100%;width:40000px;left:-20000px;top:0px;z-index:1000;" href="' + indexURL + '"></a>');
        } else {
            jQuery('header').append(
                (jQuery.browser.msie ? iframeTagPart[0] + 'style="position:absolute;height:100%;width:100%;' + iframeTagPart[1] : '' ) +
                '<a style="position:absolute;height:100%;width:100%;" href="' + indexURL + '"></a>');
        }
    }

    function adminAffix(el) {
        el.off('affix-calc.theme.affix').on('affix-calc.theme.affix', function (e) {
            var offset = 0,
                el = jQuery(this),
                fixAtScreen = jQuery(this).data('fixAtScreen'),
                elOffset = parseInt(el.data('offset'), 10) || 0,
                adminHeight = jQuery('#wpadminbar').outerHeight() || 0;

            if (fixAtScreen === 'top' && elOffset < adminHeight) {
                offset = adminHeight - elOffset;
            }

            e.offset = offset;
        });
    }

    jQuery('body').on('added_to_cart', function() {
        jQuery('.add_to_cart_button.added').text('ADDED');
    }).on('affix-init.theme.affix', function (e, el) {
        if (!el) return;
        adminAffix(el);
    });

    adminAffix(jQuery('[data-fix-at-screen]'));

    $('[data-smooth-scroll] a[href*="#"]').each(function () {
        var href = $(this).attr('href');
        if (href.indexOf(window.location.href) === 0) {
            href = href.replace(window.location.href, '');
            if (href.charAt(0) === '#') {
                $(this).attr('href', href);
            }
        }
    });
});

@baseTemplate
