(function($) {

	"use strict";

	$(document).ready(function($) {

		var $document = $(document),
			$body = $('body'),
			$window = $(window),
			$wrapper = $('#wrapper'),
			$toggle = $('.site-menu-toggle'),
			$menu_panel = $('.site-menu-panel'),
			$search_panel = $('.tb-floating-search'),
			$header = $('.site-header');

		// ---------------------------------------------------------
		// Mobile Menu
		// ---------------------------------------------------------

		// Build mobile menu
		$wrapper.after('<div class="site-mobile-panel tb-mobile-menu-wrapper"><div class="wrap"></div></div>');

		// Cache new side panel
		var $mobile_panel = $('.site-mobile-panel > .wrap');

		// Add search box, if exists in header
		$('.tb-floating-search .tb-search').first().clone().addClass('mini').appendTo( $mobile_panel );

		// Add primary menu
		$menu_panel.find('.tb-primary-menu').clone().appendTo( $mobile_panel );
		$mobile_panel.find('.tb-primary-menu').removeClass('sf-menu sf-js-enabled sf-arrows tb-primary-menu').addClass('tb-mobile-menu tb-side-menu'); // "tb-side-menu" class allows for level 2+ tree styling

		// Add social icons
		$menu_panel.find('.tb-social-icons').clone().appendTo( $mobile_panel );

		// Apply superfish to new moble menu
		if ( themeblvd.superfish == 'true' ) {
			$mobile_panel.find('.tb-mobile-menu').superfish({
				speed: 200,
				popUpSelector: '.non-mega-sub-menu, .sf-mega'
			});
		}

		if ( $header.hasClass('mobile-light') ) {
			$mobile_panel.find('.tb-mobile-menu').addClass('mobile-light');
			$mobile_panel.closest('.site-mobile-panel').addClass('mobile-light');
		} else {
			$mobile_panel.find('.tb-mobile-menu').addClass('mobile-dark');
			$mobile_panel.closest('.site-mobile-panel').addClass('mobile-dark');
		}

		// ---------------------------------------------------------
		// Sticky header
		// ---------------------------------------------------------

		if ( $body.hasClass('tb-suck-up') ) {

			if ( $window.width() > 767 ) {
				if ( $document.scrollTop() > 150 ) {
					$header.removeClass('transparent');
				} else {
					$header.addClass('transparent');
				}
			}

			$window.on('scroll', function(){
				if ( $window.width() > 767 ) {
					if ( $document.scrollTop() > 150 ) {
						$header.removeClass('transparent');
					} else {
						$header.addClass('transparent');
					}
				}
			});
		}

		// ---------------------------------------------------------
		// Panels
		// ---------------------------------------------------------

		// Main panel control button, used to open/close menu panel,
		// but also used to close search panel.
		$toggle.on('click', function(){

			var $el = $(this);

			if ( $el.hasClass('collapse-menu') ) {

				$body.removeClass('gnar-menu-panel-on');
				$toggle.removeClass('collapse collapse-menu');
				$menu_panel.removeClass('on');
				$mobile_panel.closest('.site-mobile-panel').removeClass('on');

			} else if ( ! $el.hasClass('collapse-search') ) {

				$body.addClass('gnar-menu-panel-on');
				$toggle.addClass('collapse collapse-menu');
				$menu_panel.addClass('on');
				$mobile_panel.closest('.site-mobile-panel').addClass('on');

			}

			return false;

		});

		// Open search panel
		$('.gnar-search-trigger').on('click', function(){

			$body.addClass('gnar-search-panel-on no-scroll');
			$toggle.addClass('collapse collapse-search');

			return false;

		});

		// Close search panel on focus out.
		$('.tb-floating-search .search-input').on('focusout', function() {

			$body.removeClass('gnar-search-panel-on no-scroll');
			$search_panel.removeClass('on');

			setTimeout(function () {
				$toggle.removeClass('collapse collapse-search');
			}, 100);

		});

	});

})(jQuery);
