$(document).ready(function() {

// function to open/close TOC
$("#toclink").click( function() {

	var triggerScroll;
	triggerScroll = false;

	// if the about panel is opened
	if ($("#about").attr("class") == "opened") {
		$("#aboutcontent").slideToggle("fast", function() {
			$("#about").removeClass("opened");
			$("#toc").removeClass("aboutopened");
		});
		triggerScroll = true;
	}

	// if the toc is opened
	if ($("#toc").attr("class") == "opened") {  
		$("#toccontent").slideToggle("fast", function() {  // close the toc
			$("#toc").removeClass("opened", function() {
				// address our special friend, IE6
				if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
					$("#toc").css("background-position", "50% 0px");
				}
			});
		});
	} else { // open it
		$("#toc").attr("class","opened");
		$("#toc").removeClass("aboutopened");
		// address our special friend, IE6
		if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
			$("#toc").css("background-position", "50% -54px");
		}
		$("#toccontent").slideToggle("2", function() {
			if (triggerScroll == true) { $("html, body").scrollTo( $("#toc"), {duration:1000} ); } //make sure the #toc is in view
		});
	}
	$(this).blur(); // remove the dotted outline
	return false;
});



// function to open/close About Berryman panel
$("#aboutlink").click( function() {

	// if the toc is opened
	if ($("#toc").attr("class") == "opened") {  
		$("#toccontent").slideToggle("fast", function() {  // close the toc
			$("#toc").removeClass("opened")
			// address our special friend, IE6
			if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
				$("#toc").css("background-position", "50% -108px");
			}
		});
	}
	// if the about panel is opened
	if ($("#about").attr("class") == "opened") {  // close the about panel
		$("#aboutcontent").slideToggle("fast", function() {
			$("#about").removeClass("opened");
			$("#toc").removeClass("aboutopened");
			// address our special friend, IE6
			if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
				$("#toc").css("background-position", "50% 0px");
			}
		});
	} else { // open the about panel
		$("#about").addClass("opened");
		$("#aboutcontent").slideToggle("2");
		$("#toc").addClass("aboutopened");
			// address our special friend, IE6
			if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
				$("#toc").css("background-position", "50% -108px");
			}
	}
	$(this).blur(); // remove the dotted outline
	return false;
});






function loadpage(pagenum, doscroll) {
	
	// update the global currentpage number
	currentpage = pagenum;
	
	// if the toc is opened
	if ($("#toc").attr("class") == "opened") {  
		$("#toclink").click();  // close it
	} else {
		// if the about panel is opened
		if ($("#about").attr("class") == "opened") {  
			$("#aboutlink").click();  // close it
		}
	}
	
	$("#currentpage").html('<img src="assets/images/loadingAnimation.gif" alt="" />');
	
	// slideUp the content to hide it
	$("#content").slideUp( function() {
		// fade in the loading animation
		
		// $.post() the taconite file (a php file containing xml-coded instructions) that: 
		// 1. fades out the loading animation
		// 1. updates the pagination
		// 2. changes the class of the #content div to "pageX" 
		// 3. appends to the content of #content and slides it down
		// 5. updates the URL in the browser location bar?????????
		$.post('loadpage.php', { page: currentpage }, function() { if (doscroll == true) { $("html, body").scrollTo( $("#toc"), { duration:1000 } ); } });
	});
	
}




//////////////////////////////////////////////////////////////////////
// things to do on initial page load and anytime content is refreshed

// hide the two sliding panels
$("#toccontent, #aboutcontent").hide();

// initialize the main nextpage link
$("#next").livequery('click', function() {
	loadpage(nextpage, true);
	$(this).blur();
	return false;
});
// initialize the main previouspage link
$("#previous").livequery('click', function() {
	loadpage(previouspage, true);
	$(this).blur();
	return false;
});
// initialize the next page "tape" links
$("#tapenextpage").livequery('click', function() {
	loadpage(nextpage, true);
	$(this).blur();
	return false;
});

$("#logo").livequery('click', function() {
	loadpage(1, true);
	$(this).blur();
	return false;
});




/// initialize the main toc links
$("#introduction").click( function() { loadpage(1, true); return false; });
$("#throwing").click( function() { loadpage(2, true); return false; });
$("#narrowing").click( function() { loadpage(9, true); return false; });
$("#running").click( function() { loadpage(16, true); return false; });
$("#campaign").click( function() { loadpage(22, true); return false; });
$("#voter").click( function() { loadpage(29, true); return false; });
$("#wjb").click( function() { loadpage(36, true); return false; });
$("#homestretch").click( function() { loadpage(42, true); return false; });
$("#results").click( function() { loadpage(47, true); return false; });


// load the default page
loadpage(currentpage, false);

}); // end document.ready



