
function loadPage () {
	var argstring = window.location.search;
	if (argstring.charAt (0) != '?') {
		argstring="?main"
	}
	argstring = argstring.substring (1, argstring.length)
	openXML (argstring);
}

function loadScreenshot () {
	var argstring = window . location . search;
	if (argstring . charAt (0) != '?') {
		argstring="";
	}
	else {
		argstring = argstring . substring (1, argstring . length);
		argstring = unescape (argstring);
	}
	var xScreenshot = argstring;

	var column = document.getElementById("singlecolumn");
	var hScreenshotImage = document.createElement ("img");
	hScreenshotImage = document.getElementById("screenshot");
	hScreenshotImage.setAttribute("src", xScreenshot);
	hScreenshotImage.setAttribute("alt", "Full size screenshot");

	addBackLink (column);
	addFade(column);
}

var xTitle;

function openImage (screenshot) {

	xTitle = screenshot.nextSibling.innerHTML;
    var href = screenshot.getAttribute ("href");
    document.location = "screenshot.htm?" + href;
	return false;
}

function openXML(file) {
	if (window.XMLHttpRequest) {
		xmlDoc = new XMLHttpRequest();
		xmlDoc.onreadystatechange = function () {
			if (xmlDoc.readyState == 4) {
				xmlDoc = xmlDoc.responseXML;
				importXML();
			}
		};
		xmlDoc.open("GET", "xml/" + file + ".xml", true);
		xmlDoc.send(null);
		return;
	}
	if (document.implementation && document.implementation.createDocument) {
		xmlDoc = document.implementation.createDocument("", "", null);
		xmlDoc.onload = importXML;
	}
	else if (window.ActiveXObject) {
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.onreadystatechange = function () {
			if (xmlDoc.readyState == 4) importXML();
		};
 	}
	if (xmlDoc) {
		xmlDoc.load("xml/" + file + ".xml");
	}
	else {
		var error = document.getElementById("error");
		setText (error, "Please use a browser that supports importing XML, such as Firefox or Internet Explorer 6+. Sorry about this.");
	}
}

function importXML () {
	importTitle();
	importEntries ("maincolumn");
	importEntries ("sidecolumn");
	
	var loading = document.getElementById("loading");
	loading.parentNode.removeChild(loading);
}

function importTitle () {
	xSections = xmlDoc.getElementsByTagName("sections")[0];
	xRoot = xSections.getElementsByTagName("titlesection")[0];

	if (!xRoot) return;

	var xTitle = getTextForTag (xRoot, 'title');
	var xSubtitle = getTextForTag (xRoot, 'subtitle');

	var hTitle = document.getElementById ("title");
	var hSubtitle = document.getElementById ("section");

	setText (hTitle, xTitle);
	setText (hSubtitle, xSubtitle);

}

function importEntries (sectionName) {
	var hRoot = document.getElementById (sectionName);

	if(!hRoot) return;

	xSections = xmlDoc.getElementsByTagName("sections")[0];
	xRoot = xSections.getElementsByTagName(sectionName)[0];

	var entries = xRoot.getElementsByTagName('entry');

	for (j=0;j<entries.length;j++) {
		var entry = entries[j];
		var type = entry.getAttribute("type");

		var xTitle = getTextForTag (entry, 'title');
		var xScreenshot = getTextForTag (entry, 'screenshot');
		var xLink = getTextForTag (entry, 'link');
		var xText = getTextForTag (entry, 'text');

		var hEntry = document.createElement ('div');
		setClass(hEntry, "entry");

		if (xScreenshot) {
			var hScreenshot = document.createElement ('a');
			setClass(hScreenshot, "screenshot");

			if (xLink) {
				hScreenshot.setAttribute("href", xLink);
				hScreenshot.setAttribute("title", "View project details");
			}
			else {
				hScreenshot.setAttribute("href", xScreenshot);
				hScreenshot.setAttribute("title", "View full image");
				
				
				var agent = navigator.userAgent.toLowerCase();
				if (contains(agent,"opera") || contains(agent,"safari") || contains(agent,"msie")) {
					/* Could not get screenshot.htm to work with these browsers */
				}
				else {
					/* probably Firefox/Mozilla/Netscape */
					hScreenshot.setAttribute("onClick", "openImage(this); return false;");
				}
			}



			hEntry.appendChild(hScreenshot);

			var hScreenshotImage = document.createElement ("img");
			setClass(hScreenshotImage, "screenshot");
			hScreenshotImage.setAttribute("src", xScreenshot);
			hScreenshotImage.setAttribute("alt", "Screenshot");

			if (type == "minor") {
				setClass(hScreenshot, "screenshot minor");
			}

			hScreenshot.appendChild (hScreenshotImage);
		}

		var hTitle = document.createElement ('div');
		setClass(hTitle, "entrytitle");

		if (xLink) {
			setLink(hTitle, xLink, xTitle);
			hTitle.setAttribute("title", "View project details");
		}
		else {
			setText (hTitle, xTitle);
		}
		hEntry.appendChild(hTitle);


		var hText = document.createElement ('div');
		setClass(hText, "entrytext");
		setText(hText, xText);
		hEntry.appendChild(hText);

		hRoot.appendChild (hEntry);
	}


	addBackLink (hRoot);
	addFade(hRoot);

}

function contains (source, what) {
	return (source.indexOf(what) != -1);
}

function addFade (hRoot) {
	setDiv (hRoot, "clear");
	for (j=1; j<=5; j++) {
		setDiv (hRoot, "column-end-"+j);
	}
}

function addBackLink (element) {

	/* do not show if there is no history */	
	if (window.history.length <= 0) return;

	/* do not show in the main portfolio page */
	if (window.location.search.charAt (0) != '?' && window.location.search != "?main") return;

	var div = document.createElement("div");
	div.setAttribute ("class", "backlink");

	var link = document.createElement ("a");
	if (history.back) { /* internet explorer */
		link.setAttribute ("href", "javascript:history.back()");
	}
	else {
		link.setAttribute ("href", "javascript:window.back()");
	}
	
	link.innerHTML = "[Go back]";
	div.appendChild (link);
	element.appendChild (div);
}

function getTextForTag (parent, tag) {
	return getInnerXML (parent.getElementsByTagName(tag)[0]);
}

function setLink (element, href, text) {
	a = document.createElement('a');
	a.setAttribute("href", href);
	setText (a, text);
	element.appendChild(a);
	return a;
}

function setText (element, text) {
	if (element)
		element.innerHTML = text;
}

function setDiv (element, className) {
	var div=document.createElement('div')
	setClass(div, className);
	element.appendChild(div);
}

function setClass (element, className) {
	element.setAttribute("class", className); /* for Firefox */
	element.setAttribute("className", className); /* for IE */
}


/* Based on getInnerXml by Philip Perkins - http://techrepublic.com.com/5100-3513-5810495.html */
var ELEMENT_NODE                   = 1;
var ATTRIBUTE_NODE                 = 2;
var TEXT_NODE                      = 3;
var CDATA_SECTION_NODE             = 4;
function getXML(node) {
	if (!node) return "";

	if (node.nodeType == TEXT_NODE) {
		return node.nodeValue;
	}

    var s = "<" + node.nodeName;

    if (node.attributes) {
		for (var i=0; i<node.attributes.length; i++) {
			s += " " + node.attributes[i].name + "=\"" + node.attributes[i].value + "\"";
		}
    }

    if (node.hasChildNodes()) {
		s+= ">";
		for (var i = 0; i < node.childNodes.length; i++) {
			s += getXML(node.childNodes[i]);
		}
		s += "</" + node.nodeName + ">";
    }
    else {
        s += "/>";
    }
    return s;
}

function getInnerXML (node) {
	s="";
	if (!node) return;
    if (node.hasChildNodes()) {
		for (var i = 0; i < node.childNodes.length; i++) {
			s += getXML(node.childNodes[i]);
		}
    }
    return s;

}

