/* VARIABLES */
var menuWidth = 0, menuChildren, pages, siteTitle, currentPage, actionPage, actionText;

/*** DOCUMENT LOADED ***/
$(document).ready(function() {
	menuChildren = $("#menu").children();							// Cache menu children
	pages = $(".page"); 											// Cache pages
	siteTitle = $(".page:first h1").text(); 						// Get site title
	actionPage = $(menuChildren).last();
	actionText = $(actionPage).text();
	$(actionPage).hide();
	$(".action").append('<a href="#' + (menuChildren.length - 1) + '">' + actionText + '</a>')
		.bind("click", function() {
			changePage(menuChildren.length - 1);
		});
	$(".noJSWarning2").remove(); 									// Remove Javascript warning
	$(".loadingText").show(); 										// Show "Loading..." text
	$("#container").css("visibility", "hidden");					// Hide entire page to fade it in when everything is loaded
});

/*** IMAGES LOADED ***/
$(window).load(function() {
	$("div").last().filter(":empty").remove(); 						// Chrome Vertical Bar Hack
	$(".loadingText").remove(); 									// Remove Loading Text when all images are loaded
	
	$(pages).hide();

	var location = document.location.toString(); 					// Get content of location bar
	var hash = location.split("#"); 								// Create array out of location bar
	if (hash.length < 2) { 											// If location bar container hash
		$(".page").filter(":first").show(); 						// Show the first page
		currentPage = 0; 											// No hash: Set current page to index 0
	} else {
		$(".page").eq(hash[1]).show(); 								// Show the right page
		currentPage = hash[1]; 										// Hash: Set current page to right index
	}
	
	if (!$.browser.msie) {											// Browser specific hacks
		$(pages).css("position", "absolute");						// Other browsers need this to stack (ie can't fade)
		$(pages).css("background-color", "transparent");			// A background is only needed in ie to fade properly
		$(".tour, .action").css("position", "relative");			// Divs don't fade in ie when they have a relative position
	}
	
	$("#container").hide().css("visibility", "visible").fadeIn(1000, function() { // Fade in the entire site
		$(".gallery").first().css("visibility", "visible");			// Show gallery after fade in (the gallery fades by itself)
	});
	document.title = siteTitle; 									// Set document title
	
	/*** CREATE MENU ***/
	$(menuChildren).parent().show().end().each(function(index){ 	// Give menu clickevents and hrefs based on index
		if (index < pages.length - 1) {
			menuWidth += $(this).width() + 25; 						// Menu Width
			if (index != $(menuChildren).length - 2) {
				$(this).after("<div class='middot'>&middot;</div>");// Insert middot seperator between menu items
			}
			var menuContainerWidth = $("#menuContainer").width(); 	// Calculate menu width
			menuWidth = (menuWidth > menuContainerWidth) ? menuContainerWidth : menuWidth; // Constrain menu width
			
			$(this).bind("click", function() {						// Menu Click Event
				changePage(index); 									// Change page on click
			})
			
			$(this).attr("href", "#" + index); 						// Menu href
		}
	}).eq(currentPage).addClass("selected").parent().css("width", menuWidth); // Select first menu item
	
	/*** FRONT PAGE MENU ***/
	$(".frontPageMenu a").each(function(index) {
		$(this).attr("href", "#" + (index + 1));					// Add correct links to front menu
	});
	
	/*** TOUR MENU ***/
	var tourIndex = parseInt(currentPage) + 1; 						// Calc tour index
	tourIndex = (tourIndex > (pages.length - 1)) ? 0 : tourIndex; 	// Loop tour index
	$(".tour .tourNumber")
		.text(" " + ((tourIndex == 0) ? (parseInt(currentPage) + 1) : tourIndex) + "/" + menuChildren.length); // Set tour "page X of Y"
	$(".tour a").click(function() { 								// Tour click event
		changePage("next"); 										// Change page to "next"
	}).attr("href", "#" + tourIndex); 								// Set tour href property
});

/*** CHANGE PAGE ***/
function changePage(pageIndex) {
	if (pageIndex == "next") { 									// Go to next page
		if (currentPage == pages.length - 1) { 					// Loop page change
			pageIndex = 0; 										// Set new page to index 0
		} else {
			pageIndex = parseInt(currentPage) + 1; 				// Set new page to next page
		}
	}
	
	$(pages).filter(":visible").fadeOut(250, function() { 		// Fade out current page
		$(".tour .tourNumber").text(" " + (parseInt(pageIndex) + 1) + "/" + menuChildren.length); // Set new tour "page X of Y"
		$(pages).eq(pageIndex).fadeIn(250, function() { 		// Fade in new page
			document.title = siteTitle + " - " + $(menuChildren).eq(pageIndex).text(); // Set new document title
			
			var tourIndex = parseInt(pageIndex) + 1; 			// Calc tour index
			tourIndex = (tourIndex > (pages.length - 1)) ? 0 : tourIndex; // Loop tour index
			$(".tour a").attr("href", "#" + tourIndex); 		// Set tour href property
		});
	});
	
	$(menuChildren)
		.filter(".selected")									// Find currently selected menu item
		.removeClass("selected")								// Remove class "selected" from the menu item
		.end()													// Terminate last filter (select all menu items)
		.eq(pageIndex)											// Find menu item representing new page
		.addClass("selected"); 									// Select new menu item
	
	currentPage = pageIndex; 									// Set current page
}