// Scripts for Jonstout.net - Created 6/16/2009

function hideLayers() {
	document.getElementById("pHome").style.display = "none";
	document.getElementById("pExperience").style.display = "none";
	document.getElementById("pEducation").style.display = "none";
	document.getElementById("pPortfolio").style.display = "none";
	document.getElementById("pVideo").style.display = "none";
	document.getElementById("pAbout").style.display = "none";
	document.getElementById("pContact").style.display = "none";
}

function blankLinkClass() {
	document.getElementById("home").className = "";
	document.getElementById("experience").className = "";
	document.getElementById("education").className = "";
	document.getElementById("portfolio").className = "";
	document.getElementById("video").className = "";
	document.getElementById("about").className = "";
	document.getElementById("contact").className = "";
}
	
function loadLayer(layer, view) {
	hideLayers();
	blankLinkClass();
	switch (layer) {
		case 'home':
			document.getElementById("pHome").style.display = "block";
			document.getElementById("home").className = "current";
			break;
		case 'experience':
			document.getElementById("pExperience").style.display = "block";
			document.getElementById("experience").className = "current";
			loadExperience(view);
			break;
		case 'education':
			document.getElementById("pEducation").style.display = "block";
			document.getElementById("education").className = "current";
			loadEducation(view);
			break;
		case 'portfolio':
			document.getElementById("pPortfolio").style.display = "block";
			document.getElementById("portfolio").className = "current";
			break;
		case 'video':
			document.getElementById("pVideo").style.display = "block";
			document.getElementById("video").className = "current";
			break;
		case 'about':
			document.getElementById("pAbout").style.display = "block";
			document.getElementById("about").className = "current";
			break;
		case 'contact':
			document.getElementById("pContact").style.display = "block";
			document.getElementById("contact").className = "current";
			break;
		default:
			document.getElementById("pHome").style.display = "block";
			document.getElementById("home").className = "current";
			break;
	}
}

function loadXML(filename) {
	// loads an XML file into IE or Firefox
	var xmlDoc;
	if (window.ActiveXObject) {
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.validateOnParse = false;
	} else if (document.implementation && document.implementation.createDocument) {
		xmlDoc=document.implementation.createDocument("","",null);
	} else {
		alert("I'm sorry. Your browser cannot handle the XML/XSLT transformations necessary to view this portion of the site. If you see this message, please contact the webmaster.");
	}
	try {
		xmlDoc.async = false;
		xmlDoc.load(filename);
	} catch (e) {
		// if the above doesn't work, presume browser is either Safari or Chrome
		xmlDoc = false;
	}
	return (xmlDoc);
}

// global variables necessary for the below functions
var req;
var reqTwo;
var xmlContainer;

function loadXMLSafari(xmlURL, xslURL, theContainer) {
	// loads an XML file and an XSLT stylesheet into Safari and Chrome 
	req = new XMLHttpRequest();
	req.onreadystatechange = loadXMLSafariTwo;
	req.open("GET",xmlURL,false);
	
	reqTwo = new XMLHttpRequest();
	reqTwo.onreadystatechange = loadXMLSafariTwo;
	reqTwo.open("GET",xslURL,false);
	
	xmlContainer = theContainer;
	
	req.send();
	reqTwo.send();
}

function loadXMLSafariTwo() {
	// part two of the above function; the function must be split into two parts for the XMLHttpRequest to work
	if (req.readyState != 4 || reqTwo.readyState != 4) { // if both of the files aren't ready
		return;
	}
	
	// otherwise, get the XML file and the stylesheet, combine them, and put them on the page
	var xml = req.responseXML;
	var xsl = reqTwo.responseXML;
	xmlContainer.innerHTML = "";
	xsltProcessor = new XSLTProcessor();
	xsltProcessor.importStylesheet(xsl);
	myXML = xsltProcessor.transformToFragment(xml,document);
	xmlContainer.appendChild(myXML);
}

function loadExperience(view) {
	var myFile = "http://www.jonstout.net/exp.xml";
	var myStyle = new String();
	var myControl = document.getElementById("experience-control");
	var myContainer = document.getElementById("experience-container");

	// determine view or content filter
	if (!view || view == null || view == "") {
		// if no view is specified, select view from the control
		switch (myControl.selectedIndex) {
			case 0: // "Show All" selected
				myStyle = "exp-full.xsl";
				break;
			case 1: // "Show Summary" selected
				myStyle = "exp-summary.xsl";
				break;
			default:
				myStyle = "exp-full.xsl";
				break;
		}
	} else {
		// on the other hand, if view is defined, define the style and set the control to match the view
		switch (view) {
			case "all":
				myStyle = "exp-full.xsl";
				myControl.selectedIndex = 0;
				break;
			case "summary":
				myStyle = "exp-summary.xsl";
				myControl.selectedIndex = 1;
				break;
			default:
				myStyle = "exp-full.xsl";
				myControl.selectedIndex = 0;
				break;
		}
	}
	
	xml = loadXML(myFile);
	if (!xml) { // if the browser is Safari or Chrome, then push to loadXMLSafari function and exit.
		loadXMLSafari(myFile, myStyle, myContainer);
		return;
	}
	xsl = loadXML(myStyle);
	
	myContainer.innerHTML = ""; // blank the container
	if (window.ActiveXObject) { // code for IE 
		myXML = xml.transformNode(xsl);
		myContainer.innerHTML = myXML;
	} else if (document.implementation && document.implementation.createDocument) { // code for Firefox
		xsltProcessor=new XSLTProcessor();
		xsltProcessor.importStylesheet(xsl);
		myXML = xsltProcessor.transformToFragment(xml,document);
		myContainer.appendChild(myXML);
	} else {
		alert("We're sorry, but something has gone wrong in the loadExperience function. Please inform the webmaster.");
	}
	return;
}
	
function loadEducation(view) {
	var myFile = "edu.xml";
	var myStyle = new String();
	var myControl = document.getElementById("education-control");
	var myContainer = document.getElementById("education-container");

	// determine view or content filter
	if (!view || view == null || view == "") {
		// if no view is specified, select view from the control
		switch (myControl.selectedIndex) {
			case 0: // "Show All" selected
				myStyle = "edu-all.xsl";
				break;
			case 1: // "Web" selected
				myStyle = "edu-web.xsl";
				break;
			case 2: // "Video" selected
				myStyle = "edu-vid.xsl";
				break;
			case 3: // "Writing" selected
				myStyle = "edu-write.xsl";
				break;
			default:
				myStyle = "edu-all.xsl";
				break;
		}
	} else {
		// on the other hand, if view is defined, define the style and set the control to match the view
		switch (view) {
			case "all":
				myStyle = "edu-all.xsl";
				myControl.selectedIndex = 0;
				break;
			case "web":
				myStyle = "edu-web.xsl";
				myControl.selectedIndex = 1;
				break;
			case "video":
				myStyle = "edu-vid.xsl";
				myControl.selectedIndex = 2;
				break;
			case "writing":
				myStyle = "edu-write.xsl";
				myControl.selectedIndex = 3;
			default:
				myStyle = "edu-all.xsl";
				myControl.selectedIndex = 0;
				break;
		}
	}
	
	xml = loadXML(myFile);
	if (!xml) { // if the browser is Safari or Chrome, then push to loadXMLSafari function and exit.
		loadXMLSafari(myFile, myStyle, myContainer);
		return;
	}
	xsl = loadXML(myStyle);
	
	myContainer.innerHTML = ""; // blank the container
	if (window.ActiveXObject) { // code for IE 
		myXML = xml.transformNode(xsl);
		myContainer.innerHTML = myXML;
	} else if (document.implementation && document.implementation.createDocument) { // code for Firefox
		xsltProcessor=new XSLTProcessor();
		xsltProcessor.importStylesheet(xsl);
		myXML = xsltProcessor.transformToFragment(xml,document);
		myContainer.appendChild(myXML);
	} else {
		alert("We're sorry, but something has gone wrong in the loadExperience function. Please inform the webmaster.");
	}
	return;
}