/**
 * Jorvik Custom JS
 *
 * @package Jorvik
 *
 * Distributed under the MIT license - http://opensource.org/licenses/MIT
 */
jQuery(document).ready(function($){
    // Defining a function to adjust mobile menu and search icons position if we have a large Top Bar widget area
    function fullscreen(){

        var topbarheight = $('#top-bar').outerHeight();
        topbarheight = parseInt(topbarheight);

        if ( topbarheight > 0 ) {
            jQuery('.toggle-nav').css({
                'top' : topbarheight + 'px'
            });
            jQuery('.site-search').css({
                'top' : topbarheight + 'px'
            });
        }

        var footerheight = parseInt( $('#colophon').height() );
        footerheight = footerheight - 1;
        jQuery('#page.jorvik-sticky-footer').css({
            'padding-bottom' : footerheight + 'px'
        });

        if ( ! $('#primary-menu').length ) {
            jQuery('.toggle-nav').css({
                'display' : 'none'
            });
        }

    }
  
    fullscreen();

    // Run the function in case of window resize
    jQuery(window).resize(function() {
        fullscreen();         
    });

});


jQuery(function($){

    // WooCommerce sticky add-to-cart panel
    if ( $('.woocommerce div.product form.cart').length ) {
    
        $(window).scroll(function(){
            var scrollTop = $(this).scrollTop();
            var targetTop = $(".woocommerce div.product form.cart").offset().top;
            if( scrollTop > targetTop ){
                $('#wc-sticky-addtocart').addClass('active');
            }else{
                $('#wc-sticky-addtocart').removeClass('active');
            }
        });

        $( '#wc-sticky-addtocart .options-button' ).click( function() {
            $( '#wc-sticky-addtocart table.variations' ).toggleClass( 'active' );
            $( this ).toggleClass( 'active' );
        });
    
    }

    // WooCommerce quantity buttons
    jQuery('div.quantity:not(.buttons_added), td.quantity:not(.buttons_added)').addClass('buttons_added').append('<input type="button" value="+" class="plus" />').prepend('<input type="button" value="-" class="minus" />');

    // Target quantity inputs on product pages
    jQuery('input.qty:not(.product-quantity input.qty)').each( function() {
        var min = parseFloat( jQuery( this ).attr('min') );

        if ( min && min > 0 && parseFloat( jQuery( this ).val() ) < min ) {
            jQuery( this ).val( min );
        }
    });

    jQuery( document ).on('click', '.plus, .minus', function() {

        // Get values
        var $qty        = jQuery( this ).closest('.quantity').find('.qty'),
            currentVal  = parseFloat( $qty.val() ),
            max         = parseFloat( $qty.attr('max') ),
            min         = parseFloat( $qty.attr('min') ),
            step        = $qty.attr('step');

        // Format values
        if ( ! currentVal || currentVal === '' || currentVal === 'NaN') currentVal = 0;
        if ( max === '' || max === 'NaN') max = '';
        if ( min === '' || min === 'NaN') min = 0;
        if ( step === 'any' || step === '' || step === undefined || parseFloat( step ) === 'NaN') step = 1;

        // Change the value
        if ( jQuery( this ).is('.plus') ) {

            if ( max && ( max == currentVal || currentVal > max ) ) {
                $qty.val( max );
            } else {
                $qty.val( currentVal + parseFloat( step ) );
            }

        } else {

            if ( min && ( min == currentVal || currentVal < min ) ) {
                $qty.val( min );
            } else if ( currentVal > 0 ) {
                $qty.val( currentVal - parseFloat( step ) );
            }

        }

        // Trigger change event
        $qty.trigger('change');
    });

    // Mobile Menu
    $('#primary-menu .menu-item-has-children').prepend('<span class="sub-trigger"></span>');

    $( '.toggle-nav' ).click( function() {
        $( '#page' ).toggleClass( 'is-visible' );
        $( '#masthead' ).toggleClass( 'is-visible' );
        $( this ).toggleClass( 'is-visible' );
    });
    $( '.sub-trigger' ).click( function() {
        $( this ).toggleClass( 'is-open' );
        $( this ).siblings( '.sub-menu' ).toggle( 300 );
    });

});