// when the DOM is ready...
$(document).ready(function () {

/* SCROLL INTO VIEW */
	function scrollIntoView(this_anchor){
		// Add class for div-click navigation
		$(".items").addClass("deselected_item");
		// Lower Opacity of all Anchor Divs
		$(".items").animate({
		  "opacity": .2
		}, 500);
		// Move Menu to new Anchor Div
		$("#menu").css({left:$(this_anchor).offset().left, paddingLeft:'5em'});
		// Scroll to new anchor div
		$.scrollTo($(this_anchor), 1000, {axis:'x'} );
		// Move Next & Prev Buttons to new Anchor Div
		var buttons = $("#buttons");
		$("#buttons").remove();
		$(this_anchor).append(buttons);
		// Show Prev or Next link if hidden previously
		$("#buttons a").show();
		// Hide Prev or Next link if necessary (first or last Anchor Div)
		$(".items:last").find("#buttons a.next").hide();
		$(".items:first").find("#buttons a.prev").hide();
		// Attach sibling next and previous anchors to the Next/Prev links
		$("#buttons a.next").attr({href: "#"+$(this_anchor).next().attr("id")});
		$("#buttons a.prev").attr({href: "#"+$(this_anchor).prev().attr("id")});
		// Remove deselected Cursor
		$(this_anchor).removeClass("deselected_item");
		// Raise Opacity of the new Anchor Div
		$(this_anchor).animate({
		  "opacity": 1
		}, 500);
	}

/* PAGE LOAD */
	// Hide Menu
	$("#menu ul").hide();
	// Set Opacity of Anchor Divs
	$(".items").not(":first").animate({
	  "opacity": .2
	}, 1);
	// Place initial Next Prev
	var buttons = $("#buttons");
	$("#buttons").remove();
	$(".items:first").append(buttons);
	$("#buttons a.next").attr({href: "#"+$(".items:first").next().attr("id")});
	$(".items:first").find("#buttons a.prev").hide();
	// Active Deselected Item Navigation
	$(".items").not(":first").addClass("deselected_item");


/* MENU FADE IN/OUT */
	$("#hover_nav_area").hover(function(){
		$("#menu ul").fadeIn();
	}, function(){
		$("#menu ul").fadeOut();
	});


/* NAV & NEXT/PREV CLICK */
	$(".nav li a, #buttons a").live("click", function(){
		// execute the scroll into view function
		scrollIntoView($(this).attr("href"));
		// Don't follow link
		return false;
	});

/* DIV CLICK NAVIGATION*/
	$(".deselected_item").live("click", function(){
		scrollIntoView("#"+$(this).attr("id"));
		// Don't follow any accidental link clicks
		return false;
	});

/* BACK TO START */
	$("#to_start").click(function(){
		var start_div = $(".items:first").attr("id");
		scrollIntoView("#"+start_div);
    });
		

/* MANUAL SCROLLING AUTO ADJUST */
	var t;
	$(window).scroll(function () { 
		clearTimeout(t);
		t = setTimeout(function() {
			$(".items").each(function(){
				// Window Offset
				var viewLeft = $(window).scrollLeft();
				// Element Offset
				var elemLeft = $(this).offset().left;
				// Select the element closest to being aligned and scroll to it
				if (elemLeft + 100 >= viewLeft){
						// Determines if at the end, so it doesn't keep trying to scroll
						if ($(this).find("a.next").is(":hidden")){
							return false;
						}
					// if there is no change in location, don't execute the Scroll Into View function
					if (elemLeft == viewLeft){
						if ($(this).is(":not(#welcome)")){
							return false;
						}
						// Determines if at the start, so it doesn't keep trying to scroll
						if ($(this).find("a.prev").is(":hidden")){
							return false;
						}
					}
					// Execute the scroll into view function
					scrollIntoView("#"+$(this).attr("id"));
					return false;
				}
			});
		}, 500);
    });

});