// $Id$

$(function(){
	vList = $("#tourList");
	if(!vList.length)
		return;	
	
	$("#primary h2").each(function(){
		vId = this.id + "_scroll";
		$(this).attr("id", "");
		vSelection = $(this).add($(this).nextAll());

		// find next h2
		vNextHeading = vSelection.index(vSelection.filter("h2").eq(1));
		if(vNextHeading > 0)
			vSelection = vSelection.slice(0, vNextHeading);
		
		vSelection.wrapAll($("<div></div>").addClass("section").attr("id", vId));
	});
	$("#primary div.section").wrapAll($("<div id=\"sectionWrap\"><div></div></div>"));
	
	$("#tourList a").click(function(){
		
		vId = $(this).attr("href") + "_scroll";
		$("#sectionWrap").scrollTo(vId, {
			duration:600,
			axis:"x"
		});		
		
	}).each(function(i){ // "next" links
		if(!i)
			i = 2;
		else 
			i--;
		$("#sectionWrap div.section:eq("+i+")").append($("<p></p>").append(
			$("<a></a>").attr("href", $(this).attr("href")).text("Next: " + $(this).text() + " →").click(pageScroll)
		));
	});

	//  for Firefox: scroll up
	$("html").attr({scrollTop:0,scrollLeft:0});

	$(window).load(function(){
		vCurrentHash = window.location.hash;
		scrollToHash(vCurrentHash);

		// poll for hash changes (back button)
		setInterval(function(){
			if(window.location.hash == vCurrentHash)
				return; // no change
			vCurrentHash = window.location.hash;
			scrollToHash(vCurrentHash);
		}, 1000); 			
	});

	$("#tourList > li").click(showTourSection);	
});

function scrollToHash(pHash)
{
	if(!pHash)
		pHash = $("#tourList a:first").attr("href"); // first link
	
	$("#tourList a[href='"+ pHash + "']").click();	
}

function showTourSection(pEvent)
{
	$("#tourList > li.active").removeClass("active");	
	$(this).addClass("active");

	if($(pEvent.target).is("a"))
		return true;

	window.location = $(this).find("a").click().attr("href");	
	return true;
}

function pageScroll()
{
	$(window).scrollTo(0, 700);
}





