$(document).ready(function(){
	// home page
	if ($("#mainMenu").length > 0) {
		$("<ul id='secondLevel'><\/ul>").appendTo("#mainContent");
	}
	
	$("#mainMenu ul ul").each( function (){
		if (($(this).children(".carPic").length == 0) && ($(this).children(".topItem").length == 0)) {
			$("<li class='topItem'>&nbsp;</li>").prependTo($(this));
		}
	})
	
	$("#mainMenu>li").hover(
		function () { // mouse over
			_this = $(this); _prnt = $(this).parent();
			if( typeof _prnt.data("timer") != 'undefined' ) {
				clearTimeout(_prnt.data("timer"));
			}
			if ($(this).children("ul").length > 0) { // ha van almenu
				_prnt.data("timer", setTimeout( function(){
						$("#secondLevel")
							.html(_this.find("ul").html())
							.animate({'left':'180px'},300)
					}, 300 ) );
			} else {$(this).mouseout()}
		},
		function () { // mouse out
			_this = $(this); _prnt = $(this).parent();
			if( typeof _prnt.data("timer") != 'undefined' ) {
				clearTimeout(_prnt.data("timer"));
			}
			_prnt.data("timer", setTimeout( function(){
				$("#secondLevel")
					.animate({'left':'0px'},300)
					.empty();
				}, 300 ) );
		}
	);
	
	$("#secondLevel").hover(
		function () {
			clearTimeout($("#mainMenu").data("timer"));
			clearTimeout($(this).data("timer"));
		},
		function () {
			$(this).data("timer", setTimeout( function(){
				$("#secondLevel")
					.animate({'left':'0px'},300)
					.empty();
				}, 300 ) );
		}
	);
	
	$("#secondLevel>li").live("mouseover",	function () {
			if ($(this).children("ul").length > 0) { // ha van almenu
				$("ul", $(this)).show();
			} else {$("ul", $(this)).hide();}
		}
	)
	$("#secondLevel>li").live("mouseout", function () {
			$("ul", $(this)).hide();
		}
	)
	
	// inner pages
	$("#innerMenu ul").hide();
	$("#innerMenu ul:last").show();
	$("#innerMenu h2").mouseover( function(){
		_this = $(this);
		if (typeof timer != "undefined") {
			clearTimeout(timer);
		}
		if (!$(this).hasClass("active")) {
			timer = setTimeout( function(){
					_this.addClass("active");
					_this.siblings("h2").removeClass("active");
					_this.siblings("ul").slideUp("slow").removeClass("active");
					_this.next("ul").slideDown("slow").addClass("active");
				},300)
		}
	});
});

