google.setOnLoadCallback(function(){
	if (!($.browser.msie && $.browser.version == "6.0")) {
	//if(true){
		var currentMenuItem = null;
		$.ajaxSetup({
			cache: false
		});
		var _debug = false;
		
		if ($.cookie("firstRun") != "no") {
			$("body").append("<div class=page-overlay></div>");
			$(".page-overlay").animate({
				opacity: 0
			}, 1000, function(){
				$(".page-overlay").remove()
			});
			$.cookie("firstRun", "no");
		}
		else {
			$("#ajax-wrapper").append("<div class=overlay></div>");
			$("#ajax-wrapper div.overlay").css({
				opacity: 1
			}).animate({
				opacity: 0
			}, 500, function(){
				$("#ajax-wrapper div.overlay").remove()
			});
		}
		
		// zmienne konfiguracyjne, podtrzymanie sesji
		var currentPageID = $("#current-page-ID").val();
		//console.log("Aktualna strona: " + currentPageID);
		// czyszczenie menu
		$("li.page_item").each(function(){
			$(this).attr("rel", $(this).attr("class").replace("page_item page-item-", ""));
		});
		
		// eventy menu
		var lastItem;
		var currentTarget;
		var loadingLink = false;
		$(".nav>ul>li>ul").hide().css({
			opacity: 0
		})
		
		$('a:not(#subpage_33 a,.contact-modal-window a, #links a)').live('mouseover mouseout', function(event){
			if (event.type == 'mouseover') {
				// do something on mouseover
				$(this).animate({
					color: "#9a0101"
				}, "normal");
			}
			else {
				// do something on mouseout
				$(this).stop();
				$(this).animate({
					color: "#3f3f3f"
				}, 200);
			}
		});
		
		
		// ustaw menu - usun tymczasowo href, zeby uniknac wczytania strony przed dotarciem do odpowiedniego punktu w animacji;
		// sekcja zosta�a usuni�ta --> patrz kopia
		$(".nav>ul>li a, .top>ul>li a").click(function(){
			pageTracker._trackPageview($(this).attr('href'));
			clearInterface();
			currentMenuItem = $(this).parent();
			var ieMargin;
			if ($.browser.msie) {
				ieMargin = 0
			}
			else {
				ieMargin = 1
			}; // 1px v.margin w ie, nie da sie obejsc..
			$("#ajax-wrapper").append("<div class=overlay></div>");
			$("#ajax-wrapper div.overlay").css({
				opacity: 0
			}).animate({
				opacity: 1
			}, 500);
			
			
			$(".nav>ul>li").each(function(){
				$(this).animate({
					"height": $(this).children("a").height() + ieMargin
				}, 300, function(){
				});
				
				$(this).children("ul").animate({
					opacity: 0
				}, 100);
			})
			//loadingLink = true;
			
			// wczytaj strone
			var serverPath = $("#template-path").val();
			var newID = $(this).parent("li").attr("rel");
			//console.log(serverPath + "/page_content.php");
			
			window.setTimeout(function(){
				jQuery("#ajax-wrapper").load("http://teatrnasz.pl/tn/ajax/", {
					id: newID
				}, function(){
					onLoaded(newID);
				});
			}, 400);
			
			return false;
		});
		
		function onLoading(id){
		
		}
		function onLoaded(id){
			//console.log("(ajax)Nowa strona zaladowana.");
			updateMenu();
			removeSubpageOverlay();
		}
		
		function removeSubpageOverlay(){
			$("#ajax-wrapper div.overlay").css({
				opacity: 1
			}).animate({
				opacity: 0
			}, 500, function(){
				$("#ajax-wrapper div.overlay").remove()
			});
			
		}
		
		function clearInterface(){
			if ($('#player').length) {
				$("#player").jPlayer("clearFile");
				$('#player').remove();
			}
		}
		
		function updateMenu(){
		//	$(".current_page_item a").unwrap();
			$(".current_page_item").removeClass("current_page_item");
			$(".current_page_parent").removeClass("current_page_parent");
			$(".current_page_ancestor").removeClass("current_page_ancestor");
			currentMenuItem.addClass("current_page_item");
			currentMenuItem.parents(".nav>ul>li").addClass("current_page_parent");
			
			
			
			
			//console.log("Aktualizacja menu");
			$(".nav ul>li").each(function(){
				var menuItem;
				
				if ($(this).is(".current_page_item")) {
					if ($(".current_page_parent").length) {
						//console.log("current page parent: " + $(this).parents(".page_item").length);
						menuItem = $(".current_page_parent");
					}
					else {
						menuItem = $(this);
					}
					$(menuItem).animate({
						"height": $(menuItem).children("a").height() + 25
					}, 350, function(){
						$(menuItem).children("ul").css({
							display: "block"
						}).animate({
							opacity: 1
						}, 350);
					});
				}
			})
			//currentMenuItem.children('a').wrap('<h1 />');
		}
		
		function showMenu(){
			//console.log("Wy�wietlanie menu");
			$(".nav ul>li").each(function(){
				var menuItem;
				
				if ($(this).is(".current_page_item")) {
					if ($(".current_page_parent").length) {
						//console.log("current page parent: " + $(this).parents(".page_item").length);
						menuItem = $(".current_page_parent");
					}
					else {
						menuItem = $(this);
					}
					$(menuItem).animate({
						"height": $(menuItem).children("a").height() + 25
					}, 350, function(){
						$(menuItem).children("ul").css({
							display: "block"
						}).animate({
							opacity: 1
						}, 350);
					});
				}
			})
			
			
		}
		
		showMenu();
		
		
	}})
