jQuery(document).ready(function(){
   	parentClickAble();
    $(window).resize(parentClickAble);
    DORMENU.init();
});

function parentClickAble()
{
	if($(window).width() >= 0){
        $('#dor-top-menu a.dropdown-toggle').click(function(){
            var redirect_url = $(this).attr('href');
            window.location = redirect_url;
        });
    }
}

var DORMENU = {
	$nav: $('#dor-top-menu'),
	$btn: $('#dor-top-menu > button'),
	$vlinks: $('#dor-top-menu .megamenu'),
	$hlinks: $('#dor-top-menu .hidden-links'),
	numOfItems: 0,
	totalSpace: 0,
	availableSpace: 0,
	numOfVisibleItems: 0,
	requiredSpace: 0,
	breakWidths: [],
	init:function(){
		DORMENU.SetActiveMenu();
		DORMENU.ToggleMenuLine();
		DORMENU.OpenClose();
		DORMENU.$vlinks.children().outerWidth(function(i, w) {
		    DORMENU.totalSpace += w;
		    DORMENU.numOfItems += 1;
		    DORMENU.breakWidths.push(DORMENU.totalSpace);
		});
		// Window listeners
		  $(window).resize(function() {
		  	$widthSrc = $( window ).width();
		    DORMENU.CheckHiddenMenu($widthSrc);
		  });
		  DORMENU.$btn.on('click', function() {
		    DORMENU.$hlinks.toggleClass('hidden');
		  });

		  if(parseInt($( window ).width()) > 991){
		  	$widthSrc = $( window ).width();
		  	DORMENU.CheckHiddenMenu($widthSrc);
		  }
	},
	SetActiveMenu:function(){
		var urlPath = location.pathname;
		var urlFull = location.href;
		var check = jQuery('ul.megamenu > li > a[href="'+urlFull+'"]').html();
		if(typeof check != "undefined"){
			jQuery('ul.megamenu > li > a[href="'+urlFull+'"]').closest("li").addClass("active");
		}else{
			jQuery('a[href="'+urlPath+'"]').closest(".widget-heading").addClass("active");
			jQuery('a[href="'+urlFull+'"]').closest(".widget-heading").addClass("active");
			jQuery('a[href="'+urlPath+'"]').closest("li").addClass("active");
			jQuery('a[href="'+urlFull+'"]').closest("li").addClass("active");
			jQuery('a[href="'+urlFull+'"]').closest("li.parent.dropdown").addClass("active");
			jQuery('a[href="'+urlFull+'"]').closest("li").parents("li").addClass("active");
		}
		
		

	},
	CheckTypeMenu:function(){
		$(".dor-megamenu .widget-inner.block_content").each(function(){
			var check = $(this).closest(".widget-content > .block").find(".widget-heading.title_block").html();
			//alert(typeof check)
			if(typeof check == "undefined"){
				$(this).closest(".widget-content").addClass("menu-none-title");
			}
		});
	},
	OpenClose:function(){
		var htmlOvelay = '<div class="dor-overlay"></div>';
		jQuery(".open_menu").click(function(){
			jQuery( "#dor-top-menu" ).animate({
			    left: 0,
			}, 500);
			jQuery("body").addClass("dor-mobile");
			jQuery('body > main .dor-overlay').remove();
			jQuery('body > main').append(htmlOvelay);
		});
		jQuery("body").on("click",".close_menu, .dor-overlay",function(){
			jQuery( "#dor-top-menu" ).animate({
			    left: -260,
			}, 500);
			jQuery("body").removeClass("dor-mobile");
			jQuery('body > main .dor-overlay').remove();
		});
	},
	ToggleMenuLine:function(){
		jQuery(".caretmobile, .link-cate-custom").click(function(){
			var checkClick = jQuery(this).hasClass("link-cate-custom");
			if(checkClick){
				var checkSublinks = jQuery(this).parent(".widget-heading").parent(".widget-links").find(".panel-group").html();
			}
			var checkStatus = jQuery(this).parent("li").hasClass("dor-menu-open");
			jQuery(this).parent("li").parent("ul").find("li").removeClass("dor-menu-open");
			
			//jQuery(".dor-megamenu .navbar-nav > li").removeClass("dor-menu-open");
			var checkStatusWidget = jQuery(this).closest(".widget-content").hasClass("dor-menu-open");
			jQuery(this).closest("li.dor-menu-open").find(".widget-content").removeClass("dor-menu-open");

			if(!checkStatus)
				jQuery(this).closest("li").addClass("dor-menu-open");
			else
				jQuery(this).closest("li").removeClass("dor-menu-open");
			if(checkStatusWidget){
				jQuery(this).closest(".widget-content").removeClass("dor-menu-open");
			}else{
				jQuery(this).closest(".widget-content").addClass("dor-menu-open");
			}
			DORMENU.CheckTypeMenu();
		});
	},
	CheckHiddenMenu:function($widthSr){
		// Get instant state
		console.log("$widthSr=",$widthSr);
		if(parseInt($widthSr) > 991){
		    DORMENU.availableSpace = DORMENU.$vlinks.width() - 10;
		    DORMENU.numOfVisibleItems = DORMENU.$vlinks.children().length;
		    DORMENU.requiredSpace = DORMENU.breakWidths[DORMENU.numOfVisibleItems - 1];

		    // There is not enought space
		    if (DORMENU.requiredSpace > DORMENU.availableSpace) {
		      DORMENU.$vlinks.children().last().prependTo(DORMENU.$hlinks);
		      DORMENU.numOfVisibleItems -= 1;
		      DORMENU.CheckHiddenMenu($widthSr);
		      // There is more than enough space
		    } else if (DORMENU.availableSpace > DORMENU.breakWidths[DORMENU.numOfVisibleItems]) {
		      DORMENU.$hlinks.children().first().appendTo(DORMENU.$vlinks);
		      DORMENU.numOfVisibleItems += 1;
		    }
		    // Update the button accordingly
		    DORMENU.$btn.attr("count", DORMENU.numOfItems - DORMENU.numOfVisibleItems);
		    if (DORMENU.numOfVisibleItems === DORMENU.numOfItems) {
		      DORMENU.$btn.addClass('hidden');
		    } else DORMENU.$btn.removeClass('hidden');
	    }else{
	    	if(DORMENU.$hlinks.children().length > 0){
	    		DORMENU.$hlinks.children().appendTo(DORMENU.$vlinks);
	    		DORMENU.numOfVisibleItems = 0;
	    	}
	    }
	}
};

/*


$(function() {
  var $nav = $('#dor-top-menu');
  var $btn = $('#dor-top-menu > button');
  var $vlinks = $('#dor-top-menu .megamenu');
  var $hlinks = $('#dor-top-menu .hidden-links');

  var numOfItems = 0;
  var totalSpace = 0;
  var breakWidths = [];

  // Get initial state
  $vlinks.children().outerWidth(function(i, w) {
    totalSpace += w;
    numOfItems += 1;
    breakWidths.push(totalSpace);
  });

  var availableSpace, numOfVisibleItems, requiredSpace;

  function check() {

    // Get instant state
    availableSpace = $vlinks.width() - 10;
    numOfVisibleItems = $vlinks.children().length;
    requiredSpace = breakWidths[numOfVisibleItems - 1];

    // There is not enought space
    if (requiredSpace > availableSpace) {
      $vlinks.children().last().prependTo($hlinks);
      numOfVisibleItems -= 1;
      check();
      // There is more than enough space
    } else if (availableSpace > breakWidths[numOfVisibleItems]) {
      $hlinks.children().first().appendTo($vlinks);
      numOfVisibleItems += 1;
    }
    // Update the button accordingly
    $btn.attr("count", numOfItems - numOfVisibleItems);
    if (numOfVisibleItems === numOfItems) {
      $btn.addClass('hidden');
    } else $btn.removeClass('hidden');
  }

  // Window listeners
  $(window).resize(function() {
  	if(parseInt($( window ).width()) > 991){
    	check();
    }
  });

  $btn.on('click', function() {
    $hlinks.toggleClass('hidden');
  });
  if(parseInt($( window ).width()) > 991){
  	check();
  }
});

*/