$(function(){
    $('#Menu>ul>li.SubMenu>ul').hide();
   /* MouseOver Function */
	$('#Menu>ul>li').mouseover(function(){
        if ($('#Menu>ul>li>ul ul:animated').size() == 0) {
            $rootElement = $(this);
            $siblingElements = $rootElement.siblings();
			$listElement = $siblingElements.find('ul:visible');
            if ($listElement.size() > 0) {
                $listElement.slideUp(350, function(){
	                $rootElement.find('ul').slideDown(350);
					$rootElement.find('a.Arrow').removeClass('Arrow');
                });
            }
            else {				
                $rootElement.find('ul').slideDown(350);
				$rootElement.find('a.Arrow').removeClass('Arrow');
            }
        }
    });
	/* /MouseOver Function */
	
	/* MouseOut Function */
	$('#Menu>ul').mouseout(function(){
        if ($('#Menu ul:animated').size() == 0) {
            $heading = $(this);
            $expandedSiblings = $heading.siblings().find('ul:visible');
            if ($expandedSiblings.size() > 0) {
                $expandedSiblings.slideDown(350, function(){					
                    $heading.find('ul').slideUp(350);
                });
				$heading.parrent().find('a.Main').addClass('Arrow');
            }
            else {
				$heading.find('a.Main').addClass('Arrow');
            }
        }
    });
	/* /MouseOut Function */
});


$(document).ready(function() {
	$('#Menu>ul>li').each(function() {
		//Align submenu with parent
		$(this).find('ul').css('margin-left', function() {
			return -(($(this).parent().find('ul').outerWidth(true) - $(this).parent().outerWidth(true)) /2)
		});
		//Set witdh to Arrow li Container
		$(this).find('li.Arrow').width(function() {
			return $(this).parent().outerWidth() -35 
		});
	});
	
	//Make submenu visible if root page is active
    var pathName = window.location.href;
	
	if(pathName.indexOf("fjallaventyr") !== -1) {
		showSubMenu("fjallaventyr");	
	}
	else if(pathName.indexOf("fiske") !== -1) {
		showSubMenu("fiske");
	}
	else if(pathName.indexOf("jakt") !== -1) {
		showSubMenu("fiske");
	}
	else if(pathName.indexOf("golf") !== -1) {
		showSubMenu("golf");
	}
	else if(pathName.indexOf("matupplevelser") !== -1) {
		showSubMenu("matupplevelser");
	}
	else if(pathName.indexOf("evenemang") !== -1) {
		showSubMenu("evenemang");
	}
	else if(pathName.indexOf("om-vemdalen") !== -1) {
		showSubMenu("om-vemdalen-kontakt");
	}
	
	function showSubMenu(page) {
	  	$('#Menu>ul>li').each(function() {
			$(this).find('*[href]').each(function(index){
				if($(this).attr("href") == page+'.html') {
					//Set correct submenu as active
					$(this).parent().find('ul').slideDown(0);
					//Remove down attow for active submenu
					$(this).parent().find('a.Main').removeClass('Arrow');
				}
			});
		});
	}
});
