1jQuery(document).ready(function($){
2
3    // add fitvids functionality
4    // $(".entry-content").fitVids();
5    // $(".excerpt-content").fitVids();
6
7    // set width use to move main to reveal menu when toggled
8	if( $(window).width() < 600 ) {
9		var leftWidth = $(window).width() * .8889;
10	} else {
11		var leftWidth = $(window).width() * .5;
12	}
13
14    // adjust the same value when the screen is resized
15	$(window).resize(function() {
16
17		if( $(window).width() < 600 ) {
18			leftWidth = $(window).width() * .8889;
19		} else {
20			leftWidth = $(window).width() * .5;
21		}
22	});
23
24    // no longer using tappy library here b/c doesn't work when loaded asynchronously
25    $('#toggle-button').bind('click', toggleNav);
26
27    // uses the leftWidth calculated earlier to translate the position of main & footer
28	function toggleNav() {
29        console.log('clicked');
30
31        // if nav already open
32        if ($('#site-header').hasClass('toggled')) {
33
34            // return main and footer to original position
35            $('.main').css('transform', 'translateX(0)');
36            $('.site-footer').css('transform', 'translateX(0)');
37
38            // remove toggled class
39            $('#site-header').removeClass('toggled');
40
41            // unbind the scroll watching function
42            $(window).unbind('scroll');
43        }
44        // nav was closed, so...
45        else {
46
47            // add toggled class
48            $('#site-header').addClass('toggled');
49
50            // transform from the right side if rtl
51            if( $('body').hasClass('rtl') ){
52                $('.main').css('transform', 'translateX(-' + leftWidth + 'px)');
53                $('.site-footer').css('transform', 'translateX(-' + leftWidth + 'px)');
54            }
55            // transform from left side if not
56            else {
57                $('.main').css('transform', 'translateX(' + leftWidth + 'px)');
58                $('.site-footer').css('transform', 'translateX(' + leftWidth + 'px)');
59            }
60
61            // watch scrolling to close nav when scrolled past menu
62            $(window).scroll(onScroll);
63        }
64	}
65
66    function onScroll() {
67
68        if($('#menu-primary-items').length){
69            var menuItemsBottom = $('#menu-primary-items').offset().top + $('#menu-primary-items').height();
70        } else {
71            var menuItemsBottom = $('.menu-unset').offset().top + $('.menu-unset').height();
72        }
73
74        // keep updating var on scroll
75        var topDistance = $(window).scrollTop();
76        if (topDistance > menuItemsBottom) {
77            $(window).unbind('scroll');
78            toggleNav();
79        }
80    }
81
82    $('.menu-item a, .page_item a').focus(function(){
83        $(this).parents('ul').addClass('focused');
84    });
85    $('.menu-item a, .page_item a').focusout(function(){
86        $(this).parents('ul').removeClass('focused');
87    });
88
89    // toggles search bar open when keyboard focuses on it
90    $( ".blog .search-field" ).focus(function() {
91
92        if( !$('.search-form-container').hasClass('open') ) {
93            $('.search-form-container').find('span').addClass('open')
94            $('.excerpts-container').addClass('open');
95            $('.search-form-container').addClass('open');
96        }
97    });
98
99    $('.search-form-container').find('span').bind('click', searchToggle);
100
101    // toggles the open class that hides and reveals the search bar
102    function searchToggle() {
103
104		if( $(this).hasClass('open') ) {
105			$(this).removeClass('open');
106			$('.excerpts-container').removeClass('open');
107			$('.search-form-container').removeClass('open');
108		} else {
109			$(this).addClass('open');
110			$('.excerpts-container').addClass('open');
111			$('.search-form-container').addClass('open');
112		}
113    }
114
115    // hides the search bar if clicked anywhere outside the search bar container
116	$(document).click(function(event) {
117		if($(event.target).parents().index($('.search-form-container')) == -1) {
118			if($('.search-form-container').find('span').hasClass("open")) {
119				$('.search-form-container').find('span').removeClass('open')
120				$('.excerpts-container').removeClass('open')
121				$('.search-form-container').removeClass('open')
122			}
123		}
124	})
125
126    // immediately sets the li's max-height equal to the a tag in order to hide the children
127
128    $(window).load(menuItemHeight());
129
130	function menuItemHeight() {
131
132        if($('.menu-primary-items').length){
133            var ParentMenuItems = $('.menu-primary-items .menu-item-has-children');
134        } else {
135            var ParentMenuItems = $('.menu-unset .page_item_has_children');
136        }
137		ParentMenuItems.each(function(){
138			var theHeight = $(this).children('a').outerHeight();
139			$(this).css('max-height', theHeight);
140		});
141	}
142
143    // adjusts heights to hide and reveal children of drop-down menus up to 3 tiers deep
144    menuItemToggle();
145
146	function menuItemToggle() {
147
148        // establishing variables
149		var liHeight = true;
150		var subMenuHeight = true;
151		var parentHeight = true;
152
153        if($('.menu-primary-items').length){
154            var ParentMenuItems = $('.menu-primary-items .menu-item-has-children');
155        } else {
156            var ParentMenuItems = $('.menu-unset .page_item_has_children');
157        }
158
159        ParentMenuItems.children('a').toggle(function(){
160                // gets height of a tag
161				liHeight = $(this).outerHeight();
162                // gets height of the children
163				subMenuHeight = $(this).siblings('ul').height();
164                // gets the height of the containing ul if other than the whole menu container
165				parentHeight = $(this).parents('ul:not(.menu-primary-items)').height();
166                // changes the menu items height to include the sub-menu's height, it's own height, and it's parent's height
167				$(this).parents('li').css('max-height', subMenuHeight + liHeight + parentHeight);
168                // remove negative z-index
169				$(this).siblings('ul').css('z-index', 0);
170			}, function(){
171                // reset to get height including it's children
172				liHeight = $(this).outerHeight();
173                // sets parents to total height to keep them open
174				$(this).parents('li').css('max-height', liHeight + subMenuHeight + parentHeight);
175                // closes immediate parent
176				$(this).parent('li').css('max-height', liHeight);
177                // hides ul again
178				$(this).siblings('ul').css('z-index', -1);
179		});
180	}
181
182    /* lazy load images */
183    function lazyLoadImages(){
184
185        $('.lazy').each(function(){
186            var distanceToTop = $(this).offset().top;
187            var scroll = $(window).scrollTop();
188            var windowHeight = $(window).height();
189            var isVisible = distanceToTop - scroll < windowHeight;
190            if (isVisible) {
191
192                if( $(this).hasClass('lazy-image') ){
193                    $(this).attr('src', $(this).attr('data-src')).removeClass('lazy-image');
194                }
195                if( $(this).hasClass('lazy-bg-image') ){
196                    $(this).css('background-image', 'url("' + $(this).attr('data-background') + '")').removeClass('lazy-bg-image');
197                }
198            }
199        });
200    }
201    lazyLoadImages();
202
203    $(window).scroll(function() {
204        lazyLoadImages();
205    });
206
207});
208
209/* fix for skip-to-content link bug in Chrome & IE9 */
210window.addEventListener("hashchange", function(event) {
211
212    var element = document.getElementById(location.hash.substring(1));
213
214    if (element) {
215
216        if (!/^(?:a|select|input|button|textarea)$/i.test(element.tagName)) {
217            element.tabIndex = -1;
218        }
219
220        element.focus();
221    }
222
223}, false);