$(function(){
	// General Scroll Variable, arrow span, prev ul height array and level indicator
	var scroll 	     = 0,
		span 	     = '<span class="arrow"></span>',
		old_height   = new Array(),
		level        = 0,
		//lis_with_uls = $("#innerer_wrap ul li:has(ul)"),
		lis_with_uls = $('div[class~="mod_navigation"] ul li:has(ul)'),
		nav_header	 = $("#nav_header");
		
	// Reset of the scroll in case of non-hard refresh
	$("#outer_wrap").animate({scrollLeft: "0"});

	// Appending the little arrow to indicate the next depth
	lis_with_uls.append(span);
			
	// Look for all the a s in the li s that have ul s in them
	
	lis_with_uls.find("a").click(function(e) {
		$('html, body').animate({scrollTop:0}, 'fast');
		// Caching $(this), the parental li and next ul;
		var $this     = $(this),
			$parentLi = $this.parent(),
			$nextUl   = $parentLi.find("ul:first");
			
			
		if ($parentLi.find("ul").css("display")) {
			// Setting the top margin, left scroll distance and the end height
			
			var offset = $parentLi.offset().top - 180;
			
			
			if(fe_public){
				offset=200;
			}
			
			var height = $nextUl.height();
				scroll += 235;
				old_height[level] = $("#outer_wrap").height();
				level++;

			lis_with_uls.find("ul:not(:first)").css("display", "none");
			
			bread($this.text(), level);
			$nextUl.css({display: "block", marginTop: "-" + offset + "px"});					
			$("#outer_wrap").animate({scrollLeft: scroll, height: height}, 300);	
			e.preventDefault();	
		}
		
	});
	
	// Breadcrumbs 
	
		function bread(new_part, lvl) {
			
			// Declaring all the variables
			
			var	to_add  = new_part,
				old_nav = '',
				levl    = lvl,
				all	    = '<span class="level 0 small_nav">Alles</span>',
				output  = '';
				
			// Removing the All	and then getting the rest

			
			if (nav_header.find(".base_level").css("color")) {
				nav_header.html("");
			} else {		
				nav_header.find("span.0").remove();
			}
			
			
			
			// Add the class 
			
			nav_header.find(".level").addClass("small_nav")	;
			
			
			// Get the old nav and then clear the container
			
			old_nav = nav_header.html();
			nav_header.html("");
			
			
			
			// Add the new Part and re-add the All
				
			output = all + old_nav + " <b>&gt</b> ";
			nav_header.append(output + '<span class="level ' + levl + '">' + to_add + '</span>');
				
		} // End of Function
	

		
	nav_header.delegate("span.small_nav", "click", function() {	
		if ($(this).css("display") === "inline") {
			// Declaring Variables
			var	set_back_string,
				set_back_int,
				org_scroll = scroll;
				scroll = 235;
			
	
			
			set_back_sting = $(this).attr("class").split(" ");
			set_back_int = parseInt(set_back_sting[1]);

			
			$("#outer_wrap").stop(true, true).animate({scrollLeft: scroll * set_back_int, height: old_height[set_back_int]}, 200);
			 
			
			if (set_back_int === 0) {
				nav_header.html('<span class="base_level">Bitte wählen:</span>');
				lis_with_uls.find("ul").css("display", "none");
				level = 0;
				scroll = 0;
			} else {
			 	var x =  nav_header.find("span.level").length;
			
				for(i=set_back_int+1; i <= x; i++) {
					nav_header.find("span." + i).remove();
					
				}
				// End of for-loop
				
				nav_header.find("span." + set_back_int).removeClass("small_nav").end().find("b:not(:first)").remove();
				level = level - set_back_int;
				scroll = org_scroll - 235 * set_back_int;
				
			} // end of if-statement				
			
		} // end of top if-statement
		
	});


	// Active Class
	
	var breads=new Array();
	var i=0;
	$('div[class~="mod_breadcrumb"]').find("a").each(function(){
		var hr=$(this).attr("href");
		var tmp=hr.split("=");
		breads[i]=tmp[1];
		i++;
	});


	lis_with_uls.find("a").each(function() {
		var url = $(this).attr("href"),
			splitted_url,
			page_class;
			
		splitted_url = url.split("=");
		page_class = splitted_url[1];
		if(jQuery.inArray(page_class,breads)!=-1){
			$(this).addClass("pageActive");
		}
	
	});

});
