/*
	Javascript code written by PSLWeb.co.uk for Hayley Edwards (www.rovingchef.net)
	Copyright ©2009 PSLWeb.co.uk - All Rights Reserved.
*/
/*
	Function:     addImg
	Called From:  init
	Description:  Adds an image to the DOM. This image will overlay the relative heading element. The
					image ALT text is taken from the heading text and adjustments made to the styling
					of both the image and the heading.
	Inputs:       obj - Object: the heading element (h1 or h2).
				  fileIdfr - String: the calling page filename (extension omitted).
				  relativePath - String: if the calling page not in the root directory then this
				  		will be "../" the set the correct path to the image file.
	Outputs:      None.
	Side-effects: Alters the DOM and (if this is the homepage) initiates the slideshow.
*/
function addImg(obj, fileIdfr, relativePath) {
	var id = (obj.id) ? obj.id : "heading"; // If heading ID defined use it, else default to "heading".
	var s = { heading: 1, signature: 0.8, foodchain: 0.7 }; // Font Size adjustments
	var h = { heading: 44, signature: 29, foodchain: 23 }; // Height adjustments in pixels
	var imgName = (id == "heading") ? fileIdfr : id; // Build up our image file name.
	var i = new Image();
	i.src = relativePath + "images/misc/" + imgName + "_heading.gif"; // Set image source
	i.height = h[id]; // Set image height (ensure spacing consistent)
	i.alt = obj.childNodes[0].nodeValue; // Set the image ALT text to the same as the Heading text
	obj.style.height = h[id] + "px"; // Re-style Heading
	obj.style.marginBottom = "0.4em";
	obj.style.fontSize = s[id] + "em";
	obj.appendChild(i); // Add the image to the DOM
}

/*
	Function:     init
	Called From:  All pages using header images
	Description:  Overlays an image a header element (h1, h2) where the header tag has a class
					of 'heading'. The image file used depends on whether the id of the header is
					'heading' in which case the image filename is derived from the page filename,
					e.g. page "about.htm" yields image name "about_heading.gif", or whether the id of
					the header is something else, in which case that name is used, e.g. id="signature"
					yields image name "signature_heading.gif". Finally, if this is the homepage then
					the slideshow is started.
	Inputs:       None.
	Outputs:      None.
	Side-effects: Alters the DOM and (if this is the homepage) initiates the slideshow.
*/
function init() {
	// Find filename (omit extension)
	var url = document.location.toString();
	var lastSlash = url.lastIndexOf("/");
	var lastDot = url.lastIndexOf(".");
	// If lastDot found get filename from URL, otherwise default to 'index'
	var fileIdfr = (lastDot > lastSlash) ? url.substring(lastSlash + 1, lastDot) : "index";
	var relativePath = (fileIdfr == "index") ? "" : "../";
	var h1 = document.getElementsByTagName("h1"); // Examine all h1 tags
	for (var i = 0; i < h1.length; i++)
		if (h1[i].className == "heading")
			addImg(h1[i], fileIdfr, relativePath);
	var h2 = document.getElementsByTagName("h2"); // Examine all h2 tags
	for (var i = 0; i < h2.length; i++)
		if (h2[i].className == "heading")
			addImg(h2[i], fileIdfr, relativePath);
	if (fileIdfr == "index") // If this is the homepage, start the slideshow
		show = new Slideshow(24, 7, 4, 5);
}

// Attach Window event onLoad
if (window.addEventListener)
	window.addEventListener("load", init, false);
else
	if (window.attachEvent)
		window.attachEvent("onload", init);
	else
		window.onload = init;