	function viewPhoto (photoURL, photoText)
	{
		window.location.href = "photo-viewer.htm?" + photoURL + "?" + photoText;
	}
	
	/* Get the photo caption to be used on the 'photo viewer' page.  This is set the text from the
	   <span> element below (ie a child of) the anchor <a> tag.  (Ignore the first <span> element if
	   it has class="mount" - this is a positioning element wrapping the image.)  If no text found, it
	   should be set to the "alt" value from the first <img> below the anchor tag instead.  */
	function getPhotoCaption(anchor)
	{
		var spanList = anchor.getElementsByTagName("span");
		if (spanList.length > 0)
		{
			var photoText = "";
			var spanElement = spanList[0];
			if (spanElement.className == "mount")
			{
				spanElement = spanList[1];
			}
			var spanChildren = spanElement.childNodes;
			for (var c = 0; c < spanChildren.length; c++)
			{
				photoText = photoText + getPhotoCaptionComponent(spanChildren.item(c));
			}
			return photoText;
		}
		var imgList = anchor.getElementsByTagName("img");
		if (imgList.length > 0)
		{
			var photoText = imgList[0].getAttribute("alt");
			if (photoText != null)
			{
				return photoText;
			}
		}
		return "";
	}
	
	/* Convert a single HTML node into a text value for use as (part of) the photo caption
	   on the 'photo viewer' page.  This function is called recursively to convert, for
	   example, one <span> element inside another.  */
	function getPhotoCaptionComponent(captionNode)
	{
		if (captionNode.nodeType == 3) /* IE doesn't recognise Node.TEXT_NODE */
		{
			return captionNode.nodeValue;
		}
		
		if (captionNode.nodeType == 1) /* IE doesn't recognise Node.ELEMENT_NODE */
		{
			var captionText = "<" + captionNode.nodeName.toLowerCase();
			var attribs = captionNode.attributes;
			for (var a = 0; a < attribs.length; a++)
			{
				if (attribs.item(a).nodeName.toLowerCase() == "class")  /* older Opera browsers return attribute names in upper case */
				{
					captionText = captionText + " " + attribs.item(a).nodeName.toLowerCase() + "=\\\"" + attribs.item(a).nodeValue + "\\\"";
				}
			}
			captionText = captionText + ">";
			var children = captionNode.childNodes;
			for (var c = 0; c < children.length; c++)
			{
				captionText = captionText + getPhotoCaptionComponent(children.item(c));
			}
			var captionText = captionText + "</" + captionNode.nodeName.toLowerCase() + ">";
			return captionText;
		}
	}

	function popupMail(name,domain,suffixcd)
	{
		var at = "@";
		var mailto = "mailto:";
		var suffix;
		if (suffixcd == "au")
			suffix = ".ac.uk";
		else if (suffixcd == "c")
			suffix = ".com";
		else if (suffixcd == "cu")
			suffix = ".co.uk";
		else if (suffixcd == "gu")
			suffix = ".gov.uk";
		else if (suffixcd == "n")
			suffix = ".net";
		else if (suffixcd == "o")
			suffix = ".org";
		else if (suffixcd == "ou")
			suffix = ".org.uk";
		window.location = mailto + name + at + domain + suffix;
	}

	function popupText(txt)
	{
		textWindow = window.open("","txt","width=300,height=300,scrollbars");
		windowHead = "<HEAD><TITLE>The SOC - Photo Notes</TITLE><LINK type='text/css' rel='stylesheet' href='main.css'></HEAD>";
		windowBody = "<BODY>" + txt + "</BODY>";
		textWindow.document.write("<HTML>" + windowHead + windowBody + "</HTML>");
		textWindow.document.close();
		textWindow.focus();
	}

	function preparePopups()
	{
		if (!document.getElementsByTagName) {
			return;
		}
		var anchors = document.getElementsByTagName("a");
		for (var i = 0; i < anchors.length; i++)
		{
			var anchor = anchors[i];
			var rel = anchor.getAttribute("rel");
			if (rel)
			{
				var relVals = rel.split("|");
				if (relVals[0] == "photo" || relVals[0] == "photo2")
				{
					var href = anchor.getAttribute("href");
					var spanList = anchor.getElementsByTagName("span");
					var photoText = getPhotoCaption(anchor);
					anchor.setAttribute('href', 'javascript:viewPhoto("'+href+'","'+photoText+'")');
				}
				else if (relVals[0] == "external" || relVals[0] == "doc")
				{
					anchor.setAttribute("target", "_blank"); 
				}
				else if (relVals[0] == "email")
				{
					anchor.setAttribute("href", "javascript:popupMail('"+relVals[1]+"','"+relVals[2]+"','"+relVals[3]+"')");
				}
				else if (relVals[0] == "text")
				{
					anchor.setAttribute("href", "javascript:popupText('"+relVals[1]+"')");
				}
			}
		}
	}
	
	function XMLtoString(elem)
	{	
		var serialized;		
		try
		{
			// XMLSerializer exists in current Mozilla browsers
			serializer = new XMLSerializer();
			serialized = serializer.serializeToString(elem);
		} 
		catch (e)
		{
			// Internet Explorer has a different approach to serializing XML
			serialized = elem.xml;
		}		
		return serialized;
	}
	
	window.onload = preparePopups;