﻿// Plugin created by Brock Nusser - http://mlamenu.blogspot.com/

$(function() {
    //setup - all values in pixels
    var navWidth = 150;
    var ulIndent = 0;
    //end setup

    

var pathname = window.location.pathname;
    //prepend expand/collapse icons
    $('#nav_acc li').each(function() {
        if ($(this).children('ul').length > 0) {
            if ($(this).children('ul').is(":visible")) {
                $(this).prepend('<img src="/sites/all/themes/danland/images/arrow_menu_active.png" />');
            }
            else {
                $(this).prepend('<img src="/sites/all/themes/danland/images/arrow_menu.png" />');
            }
        }
		  else {
				 $(this).prepend('<img src="/sites/all/themes/danland/images/arrow_menu.png" />');
			}
    });

    //Required Section
    var slideSpeed = 'slow'; // 'slow', 'normal', 'fast', or miliseconds 
    $('#nav_acc a').each(function() {
        var thisHref = $(this).attr('href');
		 
        if ((window.location.pathname.indexOf(thisHref) == 0) || (window.location.pathname.indexOf('/' + thisHref) == 0)) {
            $(this).parent("li").addClass('actual');
				$(this).parent("li").children('img').attr('src', '/sites/all/themes/danland/images/arrow_menu_active.png');
				if($(this).parent("li").children("ul").length >0){
					$(this).parent("li").children("ul").show();
					$(this).parent("li").addClass("no_padding");
					$(this).parent("li").addClass("no_border");
				}
				if($(this).parent("li").parent("ul").is(":hidden")){
					if ($(this).parent("li").attr("class") == "nivel2 actual"){
						//$(this).parent("li").parent("ul").css({"margin-left":"-10px","#margin-left":"0px"});
						$(this).parent("li").parent("ul").parent("li").addClass("no_padding");
						$(this).parent("li").parent("ul").parent("li").addClass("no_border");
						$(this).parent("li").parent("ul").show();
					}
					else if ($(this).parent("li").attr("class") == "nivel3 actual"){
						//$(this).parent("li").parent("ul").css({"margin-left":"-20px", "#margin-left":"-30px"});
						$(this).parent("li").parent("ul").parent("li").css("border-bottom", "none");
						//$(this).parent("li").parent("ul").parent("li").parent("ul").css("margin-left", "-10px", "border-bottom", "none");
						$(this).parent("li").parent("ul").parent("li").parent("ul").css("border-bottom", "none");
						$(this).parent("li").parent("ul").parent("li").addClass("no_padding");
						$(this).parent("li").parent("ul").parent("li").addClass("no_border");
						$(this).parent("li").parent("ul").parent("li").parent("ul").parent("li").addClass("no_padding");
						$(this).parent("li").parent("ul").parent("li").parent("ul").parent("li").addClass("no_border");
						$(this).parent("li").parent("ul").show();
						$(this).parent("li").parent("ul").parent("li").parent("ul").show();
						
						
					}
					
				}
        }
    });
    
    //prepend expand/collapse icons
    $('#nav_acc li').each(function() {
        if ($(this).children('ul').length > 0) {
			if ($(this).children('ul').is(":visible")) {
				pat = /actual/
				var clase = $(this).attr("class")
				if (pat.test(clase)){
					$(this).children('img').attr('src', '/sites/all/themes/danland/images/arrow_menu_active.png');
				}
				else {
					$(this).children('img').attr('src', '/sites/all/themes/danland/images/arrow_menu.png');
				}
         }
			else {
				$(this).children('img').attr('src', '/sites/all/themes/danland/images/arrow_menu.png');
			}
        }
    });
    
	$('#nav_acc img').click(function() {
		$('#nav_acc li').each(function(){
			$(this).removeClass('actual');
			$(this).removeClass('no_padding');
			$(this).removeClass('no_border');		
			$(this).children('img').attr('src', '/sites/all/themes/danland/images/arrow_menu.png');
		});
		// nivel 1
		if ($(this).parent("li").attr("class") == "nivel1"){
			$(this).parent("li").addClass("actual");
			$(this).attr('src', '/sites/all/themes/danland/images/arrow_menu_active.png');
			$(".nivel2").children("ul").hide();
			$('.nivel1').each(function(){
				//alert($(this).children('ul').length);
				if($(this).children('ul').is(":visible")){
					$(this).children('ul').hide(slideSpeed);
				}
				if($(this).children('ul').length == 0 && $(this).attr("class") == "nivel1"){
					$(this).children("img").attr('src', '/sites/all/themes/danland/images/arrow_menu.png');
				}
			});
			if ($(this).parent("li").children("ul").length > 0){
				if ($(this).parent('li').children('ul').css('display') == "block") {
					$(this).parent("li").children("ul").hide(slideSpeed);
				}
				else {
					$(this).parent("li").children("ul").show(slideSpeed);
					$(this).parent("li").addClass("no_padding");
				}
			}
		}
		// nivel 2
		if ($(this).parent("li").attr("class") == "nivel2"){
			$(this).parent("li").addClass("actual");
			$(this).attr('src', '/sites/all/themes/danland/images/arrow_menu_active.png');
			$(this).parent("li").parent("ul").parent("li").addClass("no_padding");
			$(this).parent("li").parent("ul").parent("li").addClass("no_border");
			$('.nivel2').each(function(){
				if($(this).children('ul').is(":visible")){
					$(this).children('ul').hide(slideSpeed);
				}
				if($(this).children('ul').length == 0 && $(this).attr("class") == "nivel2"){
					$(this).children("img").attr('src', '/sites/all/themes/danland/images/arrow_menu.png');
				}
			});
			if ($(this).parent("li").children("ul").length > 0){
				if ($(this).parent('li').children('ul').css('display') == "block") {
					$(this).parent("li").children("ul").hide(slideSpeed);
				}
				else {
					$(this).parent("li").children("ul").show(slideSpeed);
					$(this).parent("li").addClass("no_padding");
				}
			}
		}
		// nivel 3
		if ($(this).parent("li").attr("class") == "nivel3"){
			$(this).parent("li").addClass("actual");
			$(this).attr('src', '/sites/all/themes/danland/images/arrow_menu_active.png');
			$(this).parent("li").parent("ul").parent("li").addClass("no_padding");
			$(this).parent("li").parent("ul").parent("li").addClass("no_border");
			$(this).parent("li").parent("ul").parent("li").parent("ul").parent("li").addClass("no_padding");
			$(this).parent("li").parent("ul").parent("li").parent("ul").parent("li").addClass("no_border");
			$('.nivel3').each(function(){
				if($(this).children('ul').is(":visible")){
					$(this).children('ul').hide(slideSpeed);
				}
				if($(this).children('ul').length == 0 && $(this).attr("class") == "nivel3"){
					$(this).children("img").attr('src', '/sites/all/themes/danland/images/arrow_menu.png');
				}
			});
			if ($(this).parent("li").children("ul").length > 0){
				if ($(this).parent('li').children('ul').css('display') == "block") {
					$(this).parent("li").children("ul").hide(slideSpeed);
				}
				else {
					$(this).parent("li").children("ul").show(slideSpeed);
					$(this).parent("li").addClass("no_padding");
				}
			}
		}
	});

    $('#nav_acc li').click(function() {
        if ($(this).children('a').length == 0) {
            if ($(this).children('ul').html() != null) {
                $(this).parent('ul').children('li').children('ul').hide(slideSpeed);
                if ($(this).children('ul').css('display') == "block") {
                    $(this).children('ul').hide(slideSpeed);
                } else {
                    $(this).children('ul').show(slideSpeed);
                }
            }
        }
    });
    //End Required Section
});
