/**
* BOOTSTRAP EXTENDED CUSTOMIZED MENU - JS INITIALIZE
* BY: Zul Zolkaffly (02/11/2016)
*/


$(function() {
	$.fn.rotate = function(fromangle, toangle, duration, easing, complete) {
		var args = $.speed(duration, easing, complete);
		var step = args.step;
		return this.each(function(i, e) {
			args.step = function(now) {
				$.style(e, 'transform', 'rotate(' + now + 'deg)');
				if (step) return step.apply(this, arguments);
			};

			$({deg: fromangle}).animate({deg: toangle}, args);
		});
	};
	
	function OpenMenu() {	
		CloseSearch();
		
		$('.menu').css('width','100%');
		$('.menu-toggle-btn').find('.icon-bar:nth-child(2)').css('opacity','0');
		$('.menu-toggle-btn').find('.icon-bar:first-child').animate({ 'margin-top' : '9px'},100).rotate(0, 45, 500, "linear");
		$('.menu-toggle-btn').find('.icon-bar:last-child').animate({ 'margin-top' : '-15px'},100).rotate(0, -45, 500, "linear");
		
		$('.menu-nav').css('margin-left','0%');
		$('.menu').removeClass('menu-closed').addClass('menu-open');
		$('.menu-overlay').css('background','rgba(255,255,255,0.25)');
		
		//$('body').css('overflow-y','hidden');
	}
	
	function CloseMenu() {
		var isopen = $('.menu').hasClass('menu-open');
		if (!isopen) return false;
		
		$('.menu').css('width',0);
		$('.menu-toggle-btn').find('.icon-bar:nth-child(2)').css('opacity','1');
		$('.menu-toggle-btn').find('.icon-bar:first-child').animate({ 'margin-top' : '5px'},100).rotate(45, 0, 500, "linear");
		$('.menu-toggle-btn').find('.icon-bar:last-child').animate({ 'margin-top' : '5px'},100).rotate(-45, 0, 500, "linear");
		
		$('.menu-nav').css('margin-left', -1*$('.menu-nav').outerWidth()	);
		$('.menu-overlay').css('background','none');
		$('.menu').addClass('menu-closed').removeClass('menu-open');
		
		//$('body').css('overflow-y','scroll');	
	}
	
	function StartSearch() {
		var keyword = $('.menu-search-form input[type="text"]').val();
				keyword = $.trim(keyword);
				if(keyword == "") return;					
				keyword = keyword.replace(/[^A-Za-z0-9 ]/g,'');
				keyword = keyword.replace(/\s{2,}/g,' ');
				keyword = keyword.replace(/\s/g, "/");
				keyword = keyword.replace(/[|&;$%@"<>()~`!+,]/g, "");
				keyword = keyword.replace('"','');
				keyword = keyword.replace("'",'');		
				keyword = keyword.replace(' ','/');
		var url = $('.menu-search-form').attr('action');
				url += '/'+keyword;
		window.location.href = url;
	}
	
	function OpenSearch() {		
		CloseMenu();
		//var left = ($('.menu-header').hasClass('horizontal')) ? 130 : 0;
		$('.menu-search-form').css('left',0).removeClass('menu-search-closed').addClass('menu-search-open');
				
		$('.menu-search-form input[type="text"]').val('');	
		$('.menu-search-form a').off('click').on('click',CloseSearch);
		$('.menu-search-form button').off('click').on('click',StartSearch);
		$('.menu-search-form').off('keypress').on('keypress',function(e) {
			var keycode = (e.keyCode ? e.keyCode : e.which);
			if(keycode == '13') StartSearch();
		});
	}
	
	function CloseSearch() {
		$('.menu-search-form').css('left','-300vw').addClass('menu-search-closed').removeClass('menu-search-open');
	}
	
	$('.menu').addClass('menu-closed').removeClass('menu-open');
	
	$('.menu-toggle-btn').off('click').on('click',function() {
		var isopen = $('.menu').hasClass('menu-open');
		
		if(isopen) {
			CloseMenu();
		} else {
			OpenMenu();
		}
	});
	
	$('.menu-toggle-search').off('click').on('click',function() {
		var isopen = $('.menu-search-form').hasClass('menu-search-open');
		
		if(isopen) {
			CloseSearch();
		} else {
			OpenSearch();
		}
	});
	
	$('.menu-dropdown-toggle').off('click').on('click',function() {
		var $menu = $(this).parent().children('.menu-dropdown-menu');
		if(	$menu.is(':visible')	) {
			$('.menu-dropdown-menu').slideUp().parent().children('span.glyphicon').removeClass('glyphicon-chevron-down').addClass('glyphicon-chevron-right');
		} else {
			$('.menu-dropdown-menu').slideUp().parent().children('span.glyphicon').removeClass('glyphicon-chevron-down').addClass('glyphicon-chevron-right');
			$menu.slideDown().parent().children('span.glyphicon').removeClass('glyphicon-chevron-right').addClass('glyphicon-chevron-down');
		}
		return false;
	});
	
	$('menu *:not([class*="menu-"])').off('click').on('click',function(e) {
		//if($(e.target).parents('#navigation').length) {
			$('.menu-dropdown-menu').slideUp().parent().children('span.glyphicon').removeClass('glyphicon-chevron-down').addClass('glyphicon-chevron-right');
		//}
	})
	
});