// source --> https://www.streamees.com/wp-content/plugins/wp-responsive-menu-pro/js/wprmenu.js?ver=6.9.4 
jQuery(document).ready(function( $ ) {
	var bar = $('#wprmenu_bar'), //top bar that shows/hides the menu
		bar_height = bar.outerHeight(true), //the bar height
		from_width = wprmenu.from_width,
		menu_icon = $('.wprmenu_icon,.wpr_title');
		menu = $('#wprmenu_menu'), //the menu div
		menu_ul = $('#wprmenu_menu_ul'), //the menu ul
		menu_a = menu_ul.find('a'), //single menu link
		body = $('body'),
		html = $('html'),
		animation_speed = 300,
		ab = $('#wpadminbar'),
		menu_enabled = (bar.length > 0 && menu.length > 0)? true : false,
		menu_width = menu.width(),
		target_height = (window.innerHeight < body.height())? body.height() : window.innerHeight,
		target_width = (window.innerWidth < body.width())? body.width() : window.innerWidth;
		if(menu_enabled) {
			menu_ul.find('li').first().css({'border-top':'none'});
			$(document).mouseup(function (e) {
    		if ( !menu.is(e.target) && menu.has( e.target ).length === 0) {
        		if(menu.is(':visible') && !menu.hasClass('top')) {
    				$.sidr('close', 'wprmenu_menu');
    			}
    		}
		});
		//ENABLE NESTING

		//add arrow element to the parent li items and chide its child uls
		menu.find('ul.sub-menu').each(function() {
			var sub_ul = $(this),
				parent_a = sub_ul.prev('a'),
				parent_li = parent_a.parent('li').first();
			parent_a.addClass('wprmenu_parent_item');
			parent_li.addClass('wprmenu_parent_item_li');

			var expand = parent_a.before('<span class="wprmenu_icon wprmenu_icon_par '+ wprmenu.submenu_open_icon +'"></span> ').find('.wprmenu_icon_par');

			sub_ul.hide();
		});

		//adjust the a width on parent uls so it fits nicely with th eicon elemnt
		function adjust_expandable_items() {
			$('.wprmenu_parent_item_li').each(function() {
				var t = $(this),
					main_ul_width = 0,
					icon = t.find('.wprmenu_icon_par').first(),
					link = t.find('a.wprmenu_parent_item').first();
				if(menu.hasClass('top')) {
					main_ul_width = window.innerWidth;
				} else {
					main_ul_width = menu_ul.innerWidth();
				}
				if(t.find('.wprmenu_clear').length == 0) link.after('<br class="wprmenu_clear"/>');
			});
		}
		adjust_expandable_items();
		
		//Toggle search
		$('.toggle-search').click(function(){
			$('.search-expand').toggle(200);
		});

		//expand / collapse action (SUBLEVELS)
		$('.wprmenu_icon_par').on('click',function() {
			var t = $(this),
			child_ul = t.parent('li').find('ul.sub-menu').first();
			child_ul.slideToggle(300);
			t.toggleClass( wprmenu.submenu_open_icon ).toggleClass( wprmenu.submenu_close_icon );
			t.parent('li').first().toggleClass('wprmenu_no_border_bottom');
		});
		if( wprmenu.parent_click == 'yes' ) {
			$('.wprmenu_parent_item').on('click', function(e){
				e.preventDefault();
				var t = $(this),
				icon_par = t.parent('li').find('wprmenu_icon_par');
				child_ul = t.parent('li').find('ul.sub-menu').first();
				child_ul.slideToggle(300);
				icon_par.toggleClass( wprmenu.submenu_open_icon ).toggleClass( wprmenu.submenu_close_icon );
				t.parent('li').first().toggleClass('wprmenu_no_border_bottom');
			});
		}
		//helper - close all submenus when menu is hiding
		function close_sub_uls() {
			menu.find('ul.sub-menu').each(function() {
				var ul = $(this),
					icon = ul.parent('li').find('.wprmenu_icon_par'),
					li = ul.parent('li');

				if(ul.is(':visible')) ul.slideUp(300);
				icon.removeClass( wprmenu.submenu_close_icon ).addClass( wprmenu.submenu_open_icon );
				li.removeClass('wprmenu_no_border_bottom');
			});
		}

		function open_sub_uls() {
			menu.find('ul.sub-menu').each(function() {
				var ul = $(this),
					icon = ul.parent('li').find('.wprmenu_icon_par'),
					li = ul.parent('li');

				if(ul.is(':hidden')) ul.slideDown();
				icon.addClass( wprmenu.submenu_close_icon ).removeClass( wprmenu.submenu_open_icon );
				li.addClass('wprmenu_no_border_bottom');
			});
		}
		//fix the scaling issue by adding/replacing viewport metatag
		var mt = $('meta[name=viewport]');
		mt = mt.length ? mt : $('<meta name="viewport" />').appendTo('head');
		if(wprmenu.zooming == 'no') {
			mt.attr('content', 'user-scalable=no, width=device-width, maximum-scale=1, minimum-scale=1');
		} else {
			mt.attr('content', 'user-scalable=yes, width=device-width, initial-scale=1.0, minimum-scale=1');
		}


	
		//Additional fixes on change device orientation
		/*
		if( if (typeof($.browser) != 'undefined') && $.browser.mozilla ) {
		screen.addEventListener("orientationchange", function() {updateOrientation()}); //firefox
		} else if( window.addEventListener ) {
		window.addEventListener('orientationchange', updateOrientation, false);
		}
		else {
		window.attachEvent( "orientationchange" );
		}*/



		function updateOrientation() {
			window.scrollBy(1,1);
			window.scrollBy(-1,-1);

			menu_width = menu.width();

			//update the page posion for left menu
			if(menu.is(':visible') && menu.hasClass('left')) {
				body.css({'left':menu_width});
				body.scrollLeft(0);
			}
		}

		//apply the SIDR for the left/right menu
		if(menu.hasClass('left') || menu.hasClass('right')) {

			//appy sidr
			var hor_pos = (menu.hasClass('left'))? 'left' : 'right';
			menu_icon.sidr({
				name:'wprmenu_menu',
				side: hor_pos,
				speed: animation_speed,
				onOpen: function(){ $( '.wpr_open' ).hide(); $( '.wpr_close' ).show(); if( wprmenu.submenu_display == 'yes' ) open_sub_uls(); $('.search-expand').hide(200); },
				onClose: function(){ $( '.wpr_open' ).show(); $( '.wpr_close' ).hide(); close_sub_uls();  }
			});

			//when link is clicked - hide the menu first and then change location to new page
			menu_a.on('click', function(e) {
				if( wprmenu.parent_click != 'yes' || !$(this).hasClass('wprmenu_parent_item') )
					$.sidr('close', 'wprmenu_menu');
			});

			if( wprmenu.swipe != 'no' ) {
				$('body').touchwipe({
					wipeLeft: function() {
					  // Close
					  $.sidr('close', 'wprmenu_menu');
					},
					wipeRight: function() {
					  // Open
					  $.sidr('open', 'wprmenu_menu');
					},
					min_move_x: 60,
					min_move_y: 60,
					preventDefaultEvents: false
				});
			}

	      	$(window).resize(function(){
	      		target_width = (window.innerWidth < body.width())? body.width() : window.innerWidth;
				if(target_width > from_width && menu.is(':visible')) {
    				$.sidr('close', 'wprmenu_menu');
				}
			});


		} else if(menu.hasClass('top')) { //The top positioned menu

			body.prepend(menu);

			//show / hide the menu
			menu_icon.on('click', function(e) {
				$('.search-expand').hide(200);
				//scroll window top
				$("html, body").animate({ scrollTop: 0 }, animation_speed);
				if( wprmenu.submenu_display == 'no' )
					close_sub_uls();
				menu.stop(true, false).slideToggle(animation_speed);
			});


			//when link is clicked - hide the menu first and then change location to new page
			if( wprmenu.parent_click != 'yes' ){
				menu_a.on('click', function(e) {
					e.preventDefault();
					var url = $(this).attr('href');
					menu.slideUp(animation_speed,function() {
						//go to the url from the link
						window.location.href = url;
					});
				});
			}


			$(window).resize(function(){
	      		target_width = (window.innerWidth < body.width())? body.width() : window.innerWidth;
				if(target_width > from_width && menu.is(':visible')) {
					if( wprmenu.submenu_display == 'no' )
						close_sub_uls();
    				menu.slideUp(animation_speed, function() {});
				}
			});
		
		} //end if class left / top /right
		//If all submenu is set to open by default.
		if( wprmenu.submenu_display == 'yes' )
			open_sub_uls();
	} //end if menu enabled
});