/*$(document).ready( function() {*/
var animationSpeed = 250;
var currentWidth = $(window).width();
var breakPoint = 991;
var state = 0;
var v = void 0;

var navWraper      = $('#mainnavwrap');
var toogleButton   = $('#toogle-nav');
var megaMenuUl     = $('.mm');

var pageWraper     = $('#mw-inner');
var contentWraper  = $('#wrp');
var dropDownButton = $('.with-ul');
var overlay        = $('#overlay');





/********* FUNCTIONS *************/




function formatMobileNav(){	
	var t;
	navWraper.addClass("offcanvas");
	contentWraper.wrap('<div id="mw-outer"></div>').wrap('<div id="mw-inner"></div>');
	$('#mw-outer').prepend('<div id="overlay">&nbsp;</div>');
	navWraper.find("li").each(function() {
		var t;
		t = $(this);
		if (t.has("ul").length) { t.addClass("with-ul").find("ul").hide(); }
	});
	t = navWraper.find(".with-ul"); /* a*/
	t.append('<span class="ddb">&nbsp;</span>');
	pageWraper     = $('body').find('#mw-inner');
	dropDownButton = navWraper.find(".ddb");
	//alert(dropDownButton.length);
	
}

function removeMobileFormating(){
	contentWraper.unwrap().unwrap();
	$('#overlay').remove();
	navWraper.removeClass("offcanvas");
	navWraper.find("li.with-ul").each(function() {
		$(this).removeClass("with-ul").removeAttr('style');
	});
	dropDownButton.remove();
	navWraper.find("ul").each(function() {
		$(this).css('display' , '');
	});
}


function formatNav(){
	
	if(currentWidth <= breakPoint){
		
	  // ukoliko je mobilni i jos nije formatirano
	  if($('span.open').length == 0) {
		formatMobileNav();
	  }
			
	} else {
		
	  // ukoliko je desktop	
	
	
	}
	
} // formatNav


function openMobileNav() {
	
	//alert(pageWraper.length+' , '+navWraper.length);
	
	pageWraper.animate({
		left: "70%"
	}, {
		duration: animationSpeed
	});
	overlay.show();
	overlay.animate({
		left: "70%"
	}, {
		duration: animationSpeed
	});
	navWraper.animate({
		left: 0
	}, {
		duration: animationSpeed
	});
	
	return 1;	
}

$('.with-ul').on('click', function() {
	alert('click '+$(this).attr('class'));  
});

function reFindTags(){
	
	/*
	navWraper      = $('body').find('#mainnavwrap');
	toogleButton   = $('body').find('#toogle-nav');
	megaMenuUl     = $('body').find('.mm');
	pageWraper     = $('body').find('#mw-inner');
	contentWraper  = $('body').find('#wrp');
	dropDownButton = $('body').find('.with-ul');
	overlay        = $('body').find('#overlay');*/
	
//alert('ddl: '+dropDownButton.length);
}


function closeMobileNav() {
	
	//alert(pageWraper.length+' , '+navWraper.length);
	pageWraper.animate({
	  left: 0
	}, animationSpeed );
	overlay.hide();
	overlay.animate({
		left: 0
	}, {
		duration: animationSpeed
	}); 
	navWraper.animate({
	  left: "-70%"
	}, animationSpeed );
	 
	return 0;
}

overlay.on('click', function(){
	alert('click');
	closeMobileNav();
	return state = 0;
});

function resizeAct(){
	if(currentWidth > breakPoint){	
	
		navWraper.removeAttr('style');
		if(pageWraper.length) {	
		removeMobileFormating();		
		}
		$('#sw:hidden').show();
		
	} else {
		//alert(pageWraper.length);
		if(pageWraper.length == 0) {
		formatMobileNav();
		}
	} 
	
	reFindTags();
}


/*********  INICIJELNO FORMATIRANJE i Resize*******/


formatNav();

$(window).resize(function () {
	currentWidth = $(window).width();
	clearTimeout(v);
    v = setTimeout(resizeAct, 200);
	

});


/*********  TRIGERS  *************/




$(window).touchwipe({
  wipeLeft: function() {
  // Close
  return state = closeMobileNav();
  },
  wipeRight: function() {
  // Open
  return state = openMobileNav();
  },
  preventDefaultEvents: false
});

toogleButton.on( "click", function() {
 // alert( 'tb >'+state+'<' );
	  if (!state) {
		//alert('to open');  
		openMobileNav();
		//alert('to open 2');  
		return state = 1;
      } 
	   // alert('to close');  
	    closeMobileNav();  
		//alert('to close 2');  
        return state = 0;  
  
});

$(document).on( "click", '.ddb', function() {
  
  n = $(this).parent('.with-ul').find("ul:first");
  n.stop(true, true).slideToggle({
	  duration: animationSpeed
  })
  return true;
});



/*************** HOVER ****************/


/*********** GENERAL ***************/

 $.fn.hoverIntent && $("#mainnav").hoverIntent({		
		
		over: function(e) {
			if(currentWidth > breakPoint) {
			 
			  $(this).addClass("active").children("ul").fadeIn(200);
			 
			}
		},
		out: function() {
			if( currentWidth > breakPoint ) {
				$(this).removeClass("active").children("ul").fadeOut(75);
				
			}
		},
		selector: "li:not(.tc .cc, .mm li)",
		timeout: 300,
		interval: 6   
	
 }); //hoverIntent	
 

/************ MEGA MENU ******************/ 


$.fn.hoverIntent && $(".mm").hoverIntent({
		
		
		
		over: function(e) {
			
			if(currentWidth > breakPoint) {
			  var mv = $('.container').width();
			  var ulh = megaMenuUl.height();	 
			  var v  = $(this).width();
			  var ulw = Math.ceil(mv-v+22); 
			  var itemsInRow = 12;
			  
			  var wraped = $(this).children("ul").find(".mr").length;
			  
			  if(!wraped){
			  var items = $(this).children("ul").find("li");
			  for(var i = 0; i < items.length; i+=itemsInRow) {
				items.slice(i, i+itemsInRow).wrapAll("<div class='mr'></div>");
			  }
			  }
			 
			  $(this).addClass("active").children("ul").css({'width' : ulw, 'min-height' : ulh}).fadeIn(200);
			  //equalHeight($(this).find('div'));
			}
			
		}, // end over
		out: function() {
			if( currentWidth > breakPoint ) {
				$(this).removeClass("active").children("ul").fadeOut(75).css({'width' : '', 'min-height' : ''});
				//$(this).find('div').removeAttr('style');
			}
			
			if($(this).children("ul").find(".mr").length){
			  var divs = $(this).children("ul").find(".mr");
			  divs.each(function(){
				  $(this).replaceWith( $(this).children('li') );
			  });
			  }
		},
		selector: "li",
		timeout: 300,
		interval: 60
		
   
	
 }); //hoverIntent	
 
 
 $("#mainnav ul li ul a").not("ul.mm > li > a, .offcanvas a").on('click', function() {
	 
	 $(this).parent().parent().toggle();
	 $('ul.mm li.active ul').hide();
	 $('ul.mm li.active').removeClass('active');
	 $('.dc ul.mm').hide();
	 $('.dc .cc.active').removeClass('active');
	 
 });
 
 $(".offcanvas ul li ul li a").on('click', function() {
 closeMobileNav(); 
 }); 
 
 

