/* Projects Pages Functions
v 1.0
Ian Scott 2010-06-18

This handles "stickiness" of sections on the project index page
*/

DEBUG=false; // Set to true for debugging

function initProj() {
	//Inititalisation. Basically add the event handlers to our section headings
	if (DEBUG) {
		buglog("Initialising...");
	}
	var wrapper=document.getElementById("projwrap");
	var sections=document.getElementsByClassName("section", wrapper, "div");
	//preload background images
	for (var s=0; s<sections.length; s++) {
		if (DEBUG) {
			buglog(sections[s].id);
		}
		var tmpImage=new Image();
		tmpImage.src="../Images/"+sections[s].id+"back.jpg";
		if (DEBUG) {
			buglog(tmpImage.src);
		}
	}
	addStickEvents(sections);
	document.getElementById("bttlink").onclick=resetSections;
	var sectionLinks=document.getElementsByClassName("seclink",document,"a");
	for (var s=0; s<sectionLinks.length; s++) {
		sectionLinks[s].onclick=function() {
			var linkdest=this.href;
			var fragment=linkdest.match(/#([a-zA-z0-9]+)$/);
			if (fragment) {
				var findSection=fragment[1];
				if (DEBUG) {
					buglog("opening section "+findSection);
				}
				var section=document.getElementById(findSection);
				if (section) {
					section.getElementsByTagName("h3")[0].onclick();
				}
				return false;
			}
		}
	}
	// Added functionality---page url with the appropriate section hash selects that section
	var url=document.location.href;
	if (DEBUG) {
		buglog("URL:"+url);
	}
	var fragment=url.match(/#([a-zA-z0-9]+)$/);
	if (fragment) {
		var findSection=fragment[1];
		if (DEBUG) {
			buglog("Fragment:"+findSection);
		}
		//#pagetitle should show the introductory text
		if (findSection=="pagetitle") {
			resetSections();
			return;
		}
		var section=document.getElementById(findSection);
		if (section) {
			if (DEBUG) {
				buglog("Sticking Section "+findSection);
			}
			section.getElementsByTagName("h3")[0].onclick();
		}
	}
}


function addStickEvents(sections) {
	// Simply adds event handlers to section headings
	for (var s=0; s<sections.length; s++) {
		var header=sections[s].getElementsByTagName("h3")[0];
		header.onclick=stickSection;
		header.style.cursor="pointer";
		// Make sections tab-able and stickie on focus for keyboard access
		sections[s].tabIndex=0;
		sections[s].onfocus=focusSection;
	}
}

function focusSection() {
	//Act like we clicked--i.e. sticky
	this.getElementsByTagName("h3")[0].onclick();
}

function resetSections() {	
	// Hides all sections, sets the onclick event on the header to reveal the section
	var wrapper=document.getElementById("maincontent");
	var sections=document.getElementsByClassName("section", wrapper);
	if (DEBUG) {
		buglog(sections.length+" sections");
	}
	document.getElementById("projintro").style.display="block";
	//remove any close button
	var closeButton=document.getElementById("sectionclose");
	if (closeButton) {
		var theSection=closeButton.parentNode;
		theSection.removeChild(closeButton);
		//theSection.getElementsByTagName("ul")[0].style.left="-1000px";
	}
	// Selecting a section removes the event handler for that section. This ensures everything is nicely in place and ready
	addStickEvents(sections);
	// Hide everything away
	for (var s=0; s<sections.length; s++) {
		removeClass("selected",sections[s]);
		//sections[s].getElementsByTagName("ul")[0].style.left="226px";
	}
	if (DEBUG) {
		buglog("Reset Sections");
	}
	// Reshow main text
	document.getElementById("projintro").style.display="block";
	var scrollPos=findPos(document.getElementById("projintro"))[1];
	window.scroll(0,scrollPos);
}

function stickSection() {
	// Sticky display a single section
	resetSections();
	// Hide the main text (useful for print purposes)
	document.getElementById("projintro").style.display="none";
	// We don't want clicking on the current section title to actually do anything
	this.onclick="";
	this.parentNode.onfocus="";
	this.style.cursor="default";
	// Then we show the appropriate section
	var thisSection=this.parentNode;
	addClass("selected",thisSection);
	var closeButton=document.createElement("span");
	closeButton.id="sectionclose";
	closeButton.onclick=resetSections;
	closeButton.title="Click to close section";
	closeButton.innerHTML="Close";
	document.getElementById("bttlink").parentNode.insertBefore(closeButton,document.getElementById("bttlink"));
	var url=document.location.href;
	var fragment=thisSection.id;
	var buildRegExp="#"+fragment+"$";
	var ourRegExp=new RegExp(buildRegExp);
	if (!url.match(ourRegExp)) {
		url=url.replace(/#.*$/,"");
		url+="#"+fragment;
		document.location.href=url;
		thisSection.focus();
	}
	thisSection.hideFocus=true; //for IE
	// scroll to position
	// thisSection.getElementsByTagName("ul")[0].scrollIntoView();
	var scrollPos=findPos(closeButton)[1];
	if (DEBUG) {
		buglog("Top: "+scrollPos);
	}
	window.scroll(0,scrollPos);
}

// Add our inititalistion to the window load event
var oldOnLoad;
if (window.onload) {
	oldOnLoad=window.onload;
}
else {
	oldOnLoad=function(){};
}
window.onload=function() {
	initProj();
	oldOnLoad();
}
