1/**
2 * Functionality specific to Bushwick.
3 *
4 * Provides helper functions to enhance the theme experience.
5 */
6
7( function( $ ) {
8
9	/**
10	 * Masonry.
11	 */
12
13	$( window ).load( function() {
14
15		var widgets_area = $( '.widget-container' );
16
17		$( '.widget-handle' ).on( 'click.bushwick', function( event ) {
18			event.preventDefault();
19			$( this ).toggleClass( 'open' );
20
21			// Remove mejs players from sidebar
22			$( '#secondary .mejs-container' ).each( function( i, el ) {
23				if ( mejs.players[ el.id ] ) {
24					mejs.players[ el.id ].remove();
25				}
26			} );
27
28			if( $( this ).hasClass( 'open' ) ) {
29				$( '#secondary' ).slideDown( 300 );
30				// Trigger resize to make sure widgets fit prefectly.
31				$( this ).trigger( 'resize' );
32				// Masonry blocks
33				widgets_area.imagesLoaded( function() {
34					widgets_area.masonry( {
35						itemSelector: '.widget',
36						gutter: 0,
37						transitionDuration: 0,
38						isOriginLeft: ! $( 'body' ).is( '.rtl' )
39					} );
40				} );
41
42				// Re-initialize mediaelement players.
43				setTimeout( function() {
44					if ( window.wp && window.wp.mediaelement ) {
45						window.wp.mediaelement.initialize();
46					}
47				} );
48
49				// Trigger resize event to display VideoPress player.
50				setTimeout( function(){
51					if ( typeof( Event ) === 'function' ) {
52						window.dispatchEvent( new Event( 'resize' ) );
53					} else {
54						var event = window.document.createEvent( 'UIEvents' );
55						event.initUIEvent( 'resize', true, false, window, 0 );
56						window.dispatchEvent( event );
57					}
58				} );
59			} else {
60				$( '#secondary' ).slideUp( 300 );
61			}
62		} );
63
64		$( '#404-widgets' ).masonry( {
65			itemSelector: '.widget',
66			gutter: 0,
67			transitionDuration: 0,
68			isOriginLeft: ! $( 'body' ).is( '.rtl' )
69		} );
70
71		$( window ).resize( function () {
72
73			// Force layout correction after 1500 milliseconds
74			setTimeout( function () {
75				widgets_area.masonry();
76			}, 1500 );
77
78		} );
79
80	} );
81
82	/**
83	 * Wrap entry title if too many characters.
84	 */
85	$( window ).load( function() {
86		var count = $( '.single .entry-title' ).text().length;
87		if ( count > 150 ) {
88			$( '.single .entry-title' ).wrapInner( '<small />' );
89		}
90	} );
91
92	/**
93	 * Enables menu toggle for small screens.
94	 */
95	( function() {
96		var container, button, menu;
97
98		container = document.getElementById( 'site-navigation' );
99		if ( ! container )
100			return;
101
102		button = container.getElementsByTagName( 'h1' )[0];
103		if ( 'undefined' === typeof button )
104			return;
105
106		menu = container.getElementsByTagName( 'ul' )[0];
107
108		// Hide menu toggle button if menu is empty and return early.
109		if ( 'undefined' === typeof menu ) {
110			button.style.display = 'none';
111			return;
112		}
113
114		if ( -1 === menu.className.indexOf( 'nav-menu' ) )
115			menu.className += ' nav-menu';
116
117		button.onclick = function() {
118			if ( -1 !== container.className.indexOf( 'toggled' ) )
119				container.className = container.className.replace( ' toggled', '' );
120			else
121				container.className += ' toggled';
122		};
123
124	// Fix child menus for touch devices.
125	function fixMenuTouchTaps( container ) {
126		var touchStartFn,
127		parentLink = container.querySelectorAll( '.menu-item-has-children > a, .page_item_has_children > a' );
128
129		if ( 'ontouchstart' in window ) {
130			touchStartFn = function( e ) {
131				var menuItem = this.parentNode;
132
133				if ( ! menuItem.classList.contains( 'focus' ) ) {
134					e.preventDefault();
135					for( var i = 0; i < menuItem.parentNode.children.length; ++i ) {
136						if ( menuItem === menuItem.parentNode.children[i] ) {
137							continue;
138						}
139						menuItem.parentNode.children[i].classList.remove( 'focus' );
140					}
141					menuItem.classList.add( 'focus' );
142				} else {
143					menuItem.classList.remove( 'focus' );
144				}
145			};
146
147			for ( var i = 0; i < parentLink.length; ++i ) {
148				parentLink[i].addEventListener( 'touchstart', touchStartFn, false );
149			}
150		}
151	}
152
153	fixMenuTouchTaps( container );
154	} )();
155
156	$( '.navigation-main' ).find( 'a' ).on( 'focus blur', function() {
157 		$( this ).parents().toggleClass( 'focus' );
158 	} );
159
160	/**
161	 * Makes "skip to content" link work correctly in IE9 and Chrome for better
162	 * accessibility.
163	 *
164	 * @link http://www.nczonline.net/blog/2013/01/15/fixing-skip-to-content-links/
165	 */
166	$( window ).on( 'hashchange.bushwick', function() {
167		var element = document.getElementById( location.hash.substring( 1 ) );
168
169		if ( element ) {
170			if ( ! /^(?:a|select|input|button|textarea)$/i.test( element.tagName ) )
171				element.tabIndex = -1;
172
173			element.focus();
174		}
175	} );
176
177	/**
178	 * Adds a class .home-link class to the navigation list item containing the home link.
179	 */
180	//$( '.navigation-main a[href="' + bushwick_functions_vars.home_url + '"]' ).closest( 'li').addClass( 'home-link' );
181
182} )( jQuery );
183