/**
 * Forked from Twenty Thirteen.
 */

jQuery(document).ready(function($) {
								
	var body    = $( 'body' ),
	    _window = $( window ),
		nav, button, menu;

	nav = $( '.main-navigation' );
	button = nav.find( '.menu-toggle' );
	menu = nav.find( '.nav-menu' );

	/**
	 * Adds a top margin to the footer if the sidebar widget area is higher
	 * than the rest of the page, to help the footer always visually clear
	 * the sidebar.
	 */
	$( function() {
		if ( body.is( '.sidebar' ) ) {
			var sidebar   = $( '#secondary .widget-area' ),
			    secondary = ( 0 === sidebar.length ) ? -40 : sidebar.height(),
			    margin    = $( '#tertiary .widget-area' ).height() - $( '#content' ).height() - secondary;

			if ( margin > 0 && _window.innerWidth() > 999 ) {
				$( '#colophon' ).css( 'margin-top', margin + 'px' );
			}
		}
	} );

	/**
	 * Enables menu toggle for small screens.
	 */
	( function() {
		if ( ! nav || ! button ) {
			return;
		}

		// Hide button if menu is missing or empty.
		if ( ! menu || ! menu.children().length ) {
			button.hide();
			return;
		}

		button.on( 'click.pmthemes', function() {
            nav.toggleClass( 'toggled-on' );
            if ( nav.hasClass( 'toggled-on' ) ) {
                $( this ).attr( 'aria-expanded', 'true' );
                menu.attr( 'aria-expanded', 'true' );
            } else {
                $( this ).attr( 'aria-expanded', 'false' );
                menu.attr( 'aria-expanded', 'false' );
            }
        } );

		// Fix sub-menus for touch devices.
		if ( 'ontouchstart' in window ) {
			menu.find( '.menu-item-has-children > a, .page_item_has_children > a' ).on( 'touchstart.pmthemes', function( e ) {
				var el = $( this ).parent( 'li' );

				if ( ! el.hasClass( 'focus' ) ) {
					e.preventDefault();
					el.toggleClass( 'focus' );
					el.siblings( '.focus' ).removeClass( 'focus' );
				}
			} );
		}

		// Better focus for hidden submenu items for accessibility.
		menu.find( 'a' ).on( 'focus.pmthemes blur.pmthemes', function() {
			$( this ).parents( '.menu-item, .page_item' ).toggleClass( 'focus' );
		} );
	} )();
	/**
	 * @summary Add or remove ARIA attributes.
	 * Uses jQuery's width() function to determine the size of the window and add
	 * the default ARIA attributes for the menu toggle if it's visible.
	 * @since Twenty Thirteen 1.5
	 */
	function onResizeARIA() {
		if ( 643 > _window.width() ) {
			button.attr( 'aria-expanded', 'false' );
			menu.attr( 'aria-expanded', 'false' );
			button.attr( 'aria-controls', 'primary-menu' );
		} else {
			button.removeAttr( 'aria-expanded' );
			menu.removeAttr( 'aria-expanded' );
			button.removeAttr( 'aria-controls' );
		}
	}
	_window
		.on( 'load.pmthemes', onResizeARIA )
		.on( 'resize.pmthemes', function() {
			onResizeARIA();
	} );
						
	/** Mobile Top Navigation */
	/* Add toggle effect */
	$('#topnav-icon').on('click', function(){
		$('#topnav-menu').slideToggle();
		$(this).toggleClass('active');
	});
	
	/** Mobile Footer Navigation */
	/* Add toggle effect */
	$('#footernav-icon').on('click', function(){
		$('#footernav-menu').slideToggle();
		$(this).toggleClass('active');
	});

	/** Dongle Seach Form */
	$('.search-icon').on('click', function(){
		$('#wrapper .search-form-top').slideToggle();
		$(this).toggleClass('active');
	});

	/** Lol to Top */
	$('.scrollToTop').hide();
	$(window).scroll(function () {
		if( $(this).scrollTop() > 100 ) {
			$('.scrollToTop').fadeIn(300);
		}
		else {
			$('.scrollToTop').fadeOut(300);
		}
	});
	$('.scrollToTop').click(function(){
		$('html, body').animate({scrollTop:0}, 500 );
		return false;
	});

	/** Sticky Menu */
	$(window).scroll(function() {
		if (screen.width >= 1340) {
		var yPos = ( $(window).scrollTop() );
			if(yPos > 165) { // show sticky menu after screen has scrolled down 200px from the top
				$(".stickynav").fadeIn();
			} else {
				$(".stickynav").fadeOut();
			}
		}
	});

});