/**
 * Vogue Custom JS Functionality
 *
 */
( function( $ ) {
    
    jQuery( document ).ready( function() {
        
        // Add button to sub-menu item to show nested pages / Only used on mobile
        $( '.main-navigation li.page_item_has_children, .main-navigation li.menu-item-has-children' ).prepend( '<span class="menu-dropdown-btn"><i class="fa fa-angle-down"></i></span>' );
        // Mobile nav button functionality
        $( '.menu-dropdown-btn' ).bind( 'click', function() {
            $(this).parent().toggleClass( 'open-page-item' );
        });
        // The menu button
        $( '.header-menu-button' ).click( function(e){
            $( 'body' ).toggleClass( 'show-main-menu' );
        });
        
        $( '.main-menu-close' ).click( function(e){
            $( '.header-menu-button' ).click();
        });
        
        // Show / Hide Search
        $( '.menu-search .fa-search.search-btn' ).toggle( function(){
            $( 'body').addClass( 'show-site-search' );
            $( '.site-header .search-block' ).animate( { opacity: '1' }, 200 );
            $( '.site-header .search-field' ).focus();
        },function(){
            $( '.site-header .search-block' ).animate( { opacity: '0' }, 200 );
            $( 'body').removeClass( 'show-site-search' );
        });
        
        // Scroll To Top Button Functionality
        $(".scroll-to-top").bind("click", function() {
            $('html, body').animate( { scrollTop: 0 }, 800 );
        });
        $(window).scroll(function(){
            if ($(this).scrollTop() > 400) {
                $('.scroll-to-top').fadeIn();
            } else {
                $('.scroll-to-top').fadeOut();
            }
        });
		
    });
    
    $(window).resize(function () {
        
        vogue_center_slider_elements();
        
    }).resize();
    
    $(window).load(function() {
        
        $( '.side-aligned-social' ).removeClass( 'hide-side-social' );
        
        vogue_home_slider();
        
    });
    
    // Hide Search is user clicks anywhere else
    $(document).mouseup(function (e) {
        var container = $( '.site-header .search-block' );
        if ( !container.is( e.target ) && container.has( e.target ).length === 0 ) {
            $( '.site-header .search-block' ).animate( { opacity: '0' }, 200 );
            $( 'body' ).removeClass( 'show-site-search' );
        }
    });
    
    // Home Page Slider
    function vogue_home_slider() {
        var home_slider_auto = $( '.home-slider-wrap' ).data( 'auto' );
        var home_slider_scroll_effect = $( '.home-slider-wrap' ).data( 'scroll' );
        
        $( '.home-slider' ).carouFredSel({
            responsive: true,
            circular: true,
            infinite: false,
            width: 1200,
            height: 'variable',
            items: {
                visible: 1,
                width: 1200,
                height: 'variable'
            },
            onCreate: function(items) {
                vogue_center_slider_elements();
                $( '.home-slider-wrap' ).removeClass( 'home-slider-remove' );
            },
            scroll: {
                fx: home_slider_scroll_effect,
                duration: 450
            },
            auto: home_slider_auto,
            pagination: '.home-slider-pager',
            prev: '.home-slider-prev',
            next: '.home-slider-next'
        });
    }
    
    function vogue_center_slider_elements() {
        $( '.home-slider-block' ).each( function( i ){
            $( this ).find( '.home-slider-block-inner').height( $( this ).find( '.home-slider-block-bg').outerHeight() );
        });
    }
    
} )( jQuery );