$(function(){

	if ($.browser.msie)
	{
    	var config = {    
        	 sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)    
	         interval: 1,  // number = milliseconds for onMouseOver polling interval    
    	     over: doOpen,   // function = onMouseOver callback (REQUIRED)    
        	 timeout: 0,   // number = milliseconds delay before onMouseOut    
	         out: doClose    // function = onMouseOut callback (REQUIRED)  
		}
	}
	
	if (!$.browser.msie)
	{
    	var config = {    
        	 sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)    
	         interval: 0,  // number = milliseconds for onMouseOver polling interval    
    	     over: doOpen,   // function = onMouseOver callback (REQUIRED)    
        	 timeout: 10,   // number = milliseconds delay before onMouseOut    
	         out: doClose    // function = onMouseOut callback (REQUIRED)  
		}

    };

    function doOpen() {			
        $(this).addClass("hover");	
		var thisUL = $('ul:first',this);

		var rightJustify = false;

		var level1HoverLI = null;
		var level1UL = null;
		var level1Height = 0;

		var level2HoverLI = null;
		var level2UL = null;
		var level2Height = 0;

		var level3HoverLI = null;
		var level3UL = null;
		var level3Height = 0;

		var level4HoverLI = null;
		var level4UL = null;
		var level4Height = 0;

		var iBiggestUL = 0; //gibt an, welches Level die größte Höhe hat. Daran wird entschieden, welche Höhe massgeblich für ALLE Level sein wird.

		var mp = $('ul:first',this);
		var level = "";

		if ($(this).hasClass('level1'))
		{
			level = "level1";
			level1UL = $(this).parents('ul:first');
			level2UL = $('ul:first', $(this));
			if (level2UL.prev().attr("title") == "Haus & Garten" || level2UL.prev().attr("title") == "Beauty & Fun" || level2UL.prev().attr("title") == "Elektronik" || level2UL.prev().attr("title") == "Lampen & Licht")
				rightJustify = true;
			else
				rightJustify = false;

			level1HoverLI = $(this);
//			level2Height = 24*level2UL.children().size();
			level2Height = (24*level2UL.children().size())+30;

			$('#MenuBackground').show();
			$('#MenuBackground').height(level2Height);
			$('#MenuBackground').width(213);
			$('#MenuBackground').css("top", level1HoverLI.offset().top+30+"px");

			var left = level1HoverLI.offset().left;
			if (rightJustify)
				left = left-106;
			$('#MenuBackground').css("left", left+"px");
		}
		else if ($(this).hasClass('level2'))
		{
			level = "level2";

			level2UL = $(this).parents('ul:first');
			level1HoverLI = level2UL.parents('li:first');

			if (level2UL.prev().attr("title") == "Haus & Garten" || level2UL.prev().attr("title") == "Beauty & Fun" || level2UL.prev().attr("title") == "Elektronik" || level2UL.prev().attr("title") == "Lampen & Licht")
				rightJustify = true;
			else
				rightJustify = false;			

			if ($(this).has('ul').length > 0)
			{
				level3UL = $('ul:first', $(this));
				level2HoverLI = $(this);

				level2Height = (24*level2UL.children().size())+30;
				level3Height = (24*level3UL.children().size())+30;

				level3UL.css("height", level3Height+"px");
				var top = $(this).position().top;
				level3UL.css("padding-top", top+"px");

				if (level2Height > level3Height+top)
				{
				 	$('#MenuBackground').show();
					$('#MenuBackground').height(level2Height);
					$('#MenuBackground').width(427);					
				}
				else
				{
					$('#MenuBackground').show();
					$('#MenuBackground').height(level3Height+top);
					$('#MenuBackground').width(427);
				}

				var left = level2HoverLI.offset().left;
				if (rightJustify)
					left = left-214;

				$('#MenuBackground').css("left", left+"px");				
			}
			else
			{
				$('#MenuBackground').show();
				$('#MenuBackground').height((24*level2UL.children().size())+30);
				$('#MenuBackground').width(213);

				var left = level1HoverLI.offset().left;
				if (rightJustify)
					left = left-106;

				$('#MenuBackground').css("left", left+"px");
			}
		}
		else if ($(this).hasClass('level3'))
		{
			level = "level3";

			level3UL = $(this).parents('ul:first');
			level2HoverLI = level3UL.parents('li:first');

			if ($(this).has('ul').length > 0)
			{
				level2UL = level3UL.parents('ul:first');
				level1UL = level2UL.parents('ul:first');
				level4UL = $('ul:first', $(this));

				if (level2UL.prev().attr("title") == "Haus & Garten" || level2UL.prev().attr("title") == "Beauty & Fun" || level2UL.prev().attr("title") == "Elektronik" || level2UL.prev().attr("title") == "Lampen & Licht")
					rightJustify = true;
				else
					rightJustify = false;	

				level3HoverLI = $(this);

				level2Height = (24*level2UL.children().size())+30;
				level3Height = (24*level3UL.children().size())+30;
				level4Height = (24*level4UL.children().size())+30;

				level4UL.css("height", level4Height+"px");

				var top = $(this).position().top;
				level4UL.css("padding-top", top+"px");

				if (level3Height > level4Height+top)
				{
					if (level2Height > level3Height)
					{
						$('#MenuBackground').show();
						$('#MenuBackground').height(level2Height);
						$('#MenuBackground').width(641);
					}
					else
					{
						$('#MenuBackground').show();
						$('#MenuBackground').height(level3Height);
						$('#MenuBackground').width(641);
					}
				}
				else
				{
					if (level2Height > level4Height+top)
					{
						$('#MenuBackground').show();
						$('#MenuBackground').height(level2Height);
						$('#MenuBackground').width(641);
					}
					else
					{
						$('#MenuBackground').show();
						$('#MenuBackground').height(level4Height+top);
						$('#MenuBackground').width(641);
					}
				}

				var left = level2HoverLI.offset().left;
				if (rightJustify)
					left = left-414;

				$('#MenuBackground').css("left", left+"px");	
			}
			else
			{
				$('#MenuBackground').show();
				$('#MenuBackground').width(427);

/*				var left = level2HoverLI.offset().left;
				if (rightJustify)
					left = left-214;

				$('#MenuBackground').css("left", left+"px");*/
			}
		}
		else if ($(this).attr('class') == "level4 hover")
		{
			level = "level4";
			$('#MenuBackground').show();
			$('#MenuBackground').width(641);		
		}

		if (rightJustify)
		{			
			mp.css("border-left", "0px solid #737373");
			if (level == "level1")
			{
				mp.css("margin-left","-106px");
			}
			else if (level == "level2")
			{
				mp.css("margin-left","-426px");
			}
			else if (level == "level3")			
			{
				mp.css("margin-left","-428px");
			}
		}		

		$('#MenuBackground').show();
		$('ul:first',this).fadeTo('fast', 1).show();	
    }

    function doClose() {	
        $(this).removeClass("hover");
/*			
		var parentUL = $(this).parents('ul:first');
		if (parentUL.attr("class") == "menu_level2")
		{
			parentUL.css("padding-bottom", "0px");
			$('ul:first',this).css("padding-top", top+"px").css("padding-bottom", "0px");
			$('ul:first',this).css("padding-top", top+"px").css("padding-top", "0px");		
		}
*/

		if ( $(".hover").length <= 0 )
			$('#MenuBackground').hide();

		$('#btc').css('display', 'block');
    }

	$("#ETC_Page_Head_Navigation_Menu ul:last-child > li > a").prepend(" <img src=\"http://www.etc-shop.de/images/etc_navigation_arrow.png\" border=\"0\" style=\"margin-left: -5px; padding-right: 5px;\"/> ");
    $("#ETC_Page_Head_Navigation_Menu ul.menu_level1 li ul li:has(ul)").find("img:first").attr({src: "http://www.etc-shop.de/images/etc_navigation_arrow.png" });

	$("#ETC_Page_Body_Left_CategoryNavigation ul:last-child > li > a").prepend(" <img src=\"http://www.etc-shop.de/images/etc_navigation_arrow_black.png\" border=\"0\" style=\"margin-left: -5px; padding-right: 5px;\"/> ");

	var MenuObject = $("#ETC_Page_Body_Left_CategoryNavigation ul.menu_level1 li ul li:has(ul)").find("img:first");
		MenuObject.attr({src: "http://www.etc-shop.de/images/etc_navigation_arrow_black.png" });

	$("#ETC_Page_Body_Left_CategoryNavigation .level2active_link > img").attr({src: "http://www.etc-shop.de/images/etc_navigation_arrowdown_black.png"});	
	$("#ETC_Page_Body_Left_CategoryNavigation .level3active_link > img").attr({src: "http://www.etc-shop.de/images/etc_navigation_arrowdown_black.png"});	

/*    var oldHtml = $("#ETC_Page_Head_Navigation_Menu ul.menu_level1 li ul li:has(ul)").find("a:first").html();

	var newHtml = "<img src=\"http://www.etc-shop.de/images/etc_body_bread_arrow.png\" /> "+oldHtml;

	$("#ETC_Page_Head_Navigation_Menu ul.menu_level1 li ul li:has(ul)").find("a:first").html(newHtml);

	alert(newHtml);*/

	$("#ETC_Page_Head_Navigation_Menu ul.menu_level1 li").hoverIntent(config);
});
