$(document).ready(function(){
	// Manipulate the code to transform the fallback-solution. Add classes and attributes.
	$(".navigation").find("ul:hidden")
		.addClass("toggleTarget")
		.attr("aria-expanded", "false");
	$(".navigation").find("ul:hidden").prev()
		.addClass("toggleTrigger")
		.attr("href", "javascript:void(0);");
	// Assign a id to the toggle-triggers and toggle-targets to get a relation for accessibility.
	$(".navigation").find("ul:hidden").each(function(idCount) {
			$(this).attr('id', "toggleTrigger"+idCount );
			$(this).prev().attr('aria-controls', "toggleTrigger"+idCount )
		}
	);
	// Run toggle-process on click.
	$(".toggleTrigger").click(function() {
		navigationToggleEvent(this);
	});
	// Run toggle-process on defined keyboard-actions.
	$(".toggleTrigger").bind("keydown", function(keyEvent) {
		if (((keyEvent.keyCode == 39 || keyEvent.keyCode == 40) && !$(this).next(".toggleTarget").is(':visible')) || ((keyEvent.keyCode == 37 || keyEvent.keyCode ==  38) && $(this).next(".toggleTarget").is(':visible')) || (keyEvent.keyCode == 32)) {
			navigationToggleEvent(this);
		}
	});
});
// Change attributes on every toggle-process.
function navigationStateSwitcher() {
	if ($(this).is(':visible')) {
		$(this).prev().addClass("active");
		$(this).attr("aria-expanded", "true");
	} else {
		$(this).prev().removeClass("active");
		$(this).attr("aria-expanded", "false");
	}
}
// Close all open toggle-targets other than the requested and open the requested toggle-target.
function navigationToggleEvent(e) {
	if ($(e).next().is(':hidden')) {
		navigationCloseOpenToggleTargets();
		$(e).next().slideToggle("fast", navigationStateSwitcher);
	} else {
		$(e).next().slideToggle("fast", navigationStateSwitcher);
	}
}
// Close open toggle-targets.
function navigationCloseOpenToggleTargets() {
	$(".navigation > ul").find(".toggleTarget:visible").slideToggle("fast", navigationStateSwitcher);
}

