/*! Pushy - v0.9.1 - 2013-9-16
* Pushy is a responsive off-canvas navigation menu using CSS transforms & transitions.
* https://github.com/christophery/pushy/
* by Christopher Yee */

jQuery(function() {
	var pushy = jQuery('.pushy'), //menu css class
		body = jQuery('body'),
		container = jQuery('#container, .footer-container'), //container css class
		push = jQuery('.push'), //css class to add pushy capability
		siteOverlay = jQuery('.overlay-hide'), //site overlay
		pushyClass = "pushy-left pushy-open", //menu position & menu open class
		pushyActiveClass = "pushy-active", //css class to toggle site overlay
		containerClass = "container-push", //container open class
		pushClass = "push-push", //css class to add pushy capability
		menuBtn = jQuery('.menu-btn, .pushy a'), //css classes to toggle the menu
		menuSpeed = 200, //jQuery fallback menu speed
		menuWidth = pushy.width() + "px"; //jQuery fallback menu width

	function togglePushy(){
		body.toggleClass(pushyActiveClass); //toggle site overlay
		pushy.toggleClass(pushyClass);
		container.toggleClass(containerClass);
		push.toggleClass(pushClass); //css class to add pushy capability
	}

	function openPushyFallback(){
		body.addClass(pushyActiveClass);
		pushy.animate({left: menuWidth}, menuSpeed);
		container.animate({left: menuWidth}, menuSpeed);
		push.animate({left: menuWidth}, menuSpeed); //css class to add pushy capability
	}

	function closePushyFallback(){
		body.removeClass(pushyActiveClass);
		pushy.animate({left: "-" + menuWidth}, menuSpeed);
		container.animate({left: "0px"}, menuSpeed);
		push.animate({left: "0px"}, menuSpeed); //css class to add pushy capability
	}

	if(Modernizr.csstransforms3d){
		//toggle menu
		menuBtn.click(function() {
			togglePushy();
		});
		//close menu when clicking site overlay
		// siteOverlay.click(function(){ 
		// 	togglePushy();
		// });
		jQuery('.page-wrapper').click(function(){ 

			body.removeClass(pushyActiveClass); //remove site overlay
			pushy.addClass('pushy-left');
			pushy.removeClass('pushy-open');
			container.removeClass(containerClass);
			push.removeClass(pushClass); //css class to add pushy capability
		});
	}else{
		//jQuery fallback
		pushy.css({left: "-" + menuWidth}); //hide menu by default
		container.css({"overflow-x": "hidden"}); //fixes IE scrollbar issue

		//keep track of menu state (open/close)
		var state = true;

		//toggle menu
		menuBtn.click(function() {
			if (state) {
				openPushyFallback();
				state = false;
			} else {
				closePushyFallback();
				state = true;
			}
		});

		jQuery('.page-wrapper').click(function() {
			if (!state) {
				closePushyFallback();
				state = true;
			}
		});

		//close menu when clicking site overlay
		// siteOverlay.click(function(){ 
		// 	if (state) {
		// 		openPushyFallback();
		// 		state = false;
		// 	} else {
		// 		closePushyFallback();
		// 		state = true;
		// 	}
		// });
	}
});