$(document).ready(function() {
	
	if ($("#rootMenu")) {
		$("#rootMenu").find(".menuItemList").each(function(index, element) {
			
			$(element).find("a:first").css("border", "none");
			
			/*
			$(element).find(".listParent a:first").css("border", "none");
			*/
			
			$(element).find("a").css("whiteSpace", "nowrap");

			if ($(element).width() < $(element).parent().find("p").width()) {
				$(element).css("width", $(element).parent().find("p").width());
			} else if ($(element).width() > 300) {
				$(element).css("width", "300px");
			} else {
				$(element).css("width", ($(element).width()) + "px");//hyphenation fix
			}

			$(element).find("a").css("whiteSpace", "normal");
			
			$(element).attr("height", $(element).height());
			$(element).css({
				"clip" : "rect(" + $(element).height() + "px, " + $(element).width() + "px, " + $(element).height() + "px, 0px)",
				"marginTop" : -$(element).attr("height"),
				"visibility" : "visible"
			});
		});
		
		
		//fix last item position (right)
		/*
		var widthFix = $(".menuItemList").last().outerWidth() - $(".menuItemList").last().parent().find("p").outerWidth();
		if($(".menuItemList").last().outerWidth() > $(".menuItemList").last().parent().find("p").outerWidth()) 
		{
			$(".menuItemList").last().css("marginLeft", -widthFix);
		}
		*/
		var timer_rootMenu;

		$("#rootMenu").find("p").bind({
			mouseenter: function() {
				var thisElement = this;
				if ($(thisElement).parent().find(".menuItemList").length) {
					timer_rootMenu = window.setTimeout(function() {
						
						$(thisElement).parent().find(".menuItemList").each(function(index, element) {
							$(element).parent().find("p a").addClass("over");
							
							//width fix if changed & hyphenation fix
							$(element).css("width", "auto");
							$(element).find("a").css("whiteSpace", "nowrap");
							if ($(element).width() < $(element).parent().find("p").width()) {
								$(element).css("width", $(element).parent().find("p").width());
							} else if ($(element).width() > 300) {
								$(element).css("width", "300px");
							} else {
								$(element).css("width", ($(element).outerWidth()) + "px");//hyphenation fix
							}
							
							$(element).find("a").css("whiteSpace", "normal");
							
							//height fix if changed
							$(element).css("height", "auto");
							if ($(element).height() != $(element).attr("height")) {
								$(element).css("height", $(element).height());
								$(element).attr("height", $(element).height());
								$(element).css({
									"marginTop" : -$(element).attr("height")
								});	
							}
							$(element).clearQueue().stop().animate({
								"marginTop" : "0"
							}, {
								duration: 600,
								step: function(now, fx) {
									$(element).css({
										"clip" : "rect(" + (-now) + "px, " + $(element).width() + "px, " + $(element).height() + "px, 0px)"
									});
								}
							});
							
							$(thisElement).parent().find(".menuItemList").css({
								"z-index" : 180
							});
							
							//fix last item position (right)
							/*
							var widthFix = $(".menuItemList").last().outerWidth() - $(".menuItemList").last().parent().find("p").outerWidth();
							if($(".menuItemList").last().outerWidth() > $(".menuItemList").last().parent().find("p").outerWidth()) 
							{
								$(".menuItemList").last().css("marginLeft", -widthFix);
							}
							*/
							
						});
					}, 100);
				}
			},
			mouseleave: function() {
				var thisElement = this;
				if ($(thisElement).parent().find(".menuItemList").length) {
					window.clearTimeout(timer_rootMenu);
					$(this).parent().find(".menuItemList").each(function(index, element) {
						$(element).stop().delay(100).animate({
							"marginTop" : -$(element).attr("height")
						}, {
							duration: 400,
							step: function(now, fx) {
								$(element).css({
									"clip" : "rect(" + (-now) + "px, " + $(element).width() + "px, " + $(element).height() + "px, 0px)",
									"z-index" : 170
								});
							},
							complete: function(){
								$(this).parent().find("p a").removeClass("over");
							}
						});
					});
				}
			}
		});

		$("#rootMenu").find(".menuItemList").bind({
			mouseenter: function() {
				$(this).each(function(index, element) {
					$(element).parent().find("p a").addClass("over");
					
					$(element).clearQueue().stop().animate({
						"marginTop" : "0"
					}, {
						duration: 600,
						step: function(now, fx) {
							$(element).css({
								"clip" : "rect(" + (-now) + "px, " + $(element).width() + "px, " + $(element).height() + "px, 0px)"
							});
						}
					});

				});
			},
			mouseleave: function() {
				$(this).each(function(index, element) {
					
					$(element).stop().delay(100).animate({
						"marginTop" : -$(element).attr("height")
					}, {
						duration: 400,
						step: function(now, fx) {
							$(element).css({
								"clip" : "rect(" + (-now) + "px, " + $(element).width() + "px, " + $(element).height() + "px, 0px)"
							});
						},
						complete: function(){
							$(this).parent().find("p a").removeClass("over");
						}
					});

				});
			}
		});

	}
});
