
/*
	Authors:		Dan Nye & Jeff Home, Coedit Limited - http://www.coedit.co.uk/
	Description:	Coedit Limited - Markup enhancing JavaScript
	Copyright:		Copyright 2006 - Coedit Limited - http://www.coedit.co.uk/

	Not to be reproduced without permission of the authors.
*/

function getElementsByClassName(classToFind, baseElement) {
	if (arguments.length == 1) {
		baseElement = document;		// Assume whole document if no base element passed in
	} else if (!baseElement) {
		return([]);					// If base element is passed in and doesn't exist, return empty array
	}	

	var tempElements = baseElement.getElementsByTagName('*');

	// getElementsByTagName('*') doesn't work in IE 5.0/Win or IE 5.5/Win... so use this as a backup
	if (!tempElements.length && baseElement.all) tempElements = baseElement.all;

	var matchingElements = [];
	for (var loop=0; loop<tempElements.length; loop++) {
		if ((' ' + tempElements[loop].className + ' ').indexOf(' ' + classToFind + ' ') != -1) {
			matchingElements[matchingElements.length] = tempElements[loop];
		}
	}
	return(matchingElements);
}

/* IE hack to support abbr element - from http://www.sovavsiti.cz/css/abbr.html */
/* Not needed for IE/Mac */
/*
	function styleAbbr() {
		var oldBodyText = document.body.innerHTML;
		var reg = /<ABBR([^>]*)>([^<]*)<\/ABBR>/g;
		var newBodyText = oldBodyText.replace(reg, '<abbr $1><span class=\"abbr\" $1>$2<\/span><\/abbr>');
		document.body.innerHTML = newBodyText;
	}
*/

/* Returns an array of 'tagName' elements that are descendants of element 'baseEl', and that (optionally) match type 'type' */
	function getElements(baseEl, tagName, type) {
		var elements = [];
		var tempEls = baseEl.getElementsByTagName(tagName);
		if (typeof(type) == 'string') type = type.toLowerCase();
		for (var loop=0; loop<tempEls.length; loop++)
			if (typeof(type) == 'undefined' || (typeof(type) != 'undefined' && type == tempEls[loop].type.toString().toLowerCase()))
				elements[elements.length] = tempEls[loop];
		return(elements);
	}


/* IE hack to support the "focus" pseudo-class */
/* Not needed for IE/Mac */
	function patchFocus() {
		var inputs = getElements(document, 'select');
		inputs = inputs.concat(getElements(document, 'textarea'));
		inputs = inputs.concat(getElements(document, 'input', 'text'));
		for (var loop=0; loop<inputs.length; loop++) {
			inputs[loop].onfocus = function() {
				this.className += ' focused';
			};
			inputs[loop].onblur = function() {
				this.className = this.className.replace(' focused', '');
			};
		}
	}


/* Patch thumnbail navigation hovers so that they work in IE */
/*
	function patchHovers() {
		var subNavThumbs = document.getElementById('subNav').getElementsByTagName('li');
		for (var loop=0; loop<subNavThumbs.length; loop++) {
			var thumbnail = subNavThumbs[loop].getElementsByTagName('span')[0];
			thumbnail.onmouseover = function() {
				this.style.backgroundImage = this.currentStyle.backgroundImage.replace('.jpg', '_ro.jpg');
			}
			thumbnail.onmouseout = function() {
				this.style.backgroundImage = this.currentStyle.backgroundImage.replace('_ro.jpg', '.jpg');
			}
		}
	}
*/


/* Set any "back to top" links to scroll instead of adding an ugly bookmark to the end of the URL */
	function patchBackToTopLinks() {
		if (window.scrollTo && (typeof(window.scrollTo) == 'function' || typeof(window.scrollTo) == 'object')) {
			var anchors = document.getElementsByTagName('a');
			for (var loop=0; loop<anchors.length; loop++)
				if (anchors[loop].href.indexOf('#backToTop') != -1)
					anchors[loop].onclick = function() {
						window.scrollTo(0, 0);
						return(false);
					};
		}
	}



/* Menu-related JavaScript */
/*
	var statStr = '';
	var hideSubMenuTimer = menuSelected = menuShown = null;
	var levelOneLIs = [];
	var menuDelay = 1000;
	var fadeAmount = 5;

	function patchMenuRollover() {
		for (var loop=0; loop<levelOneLIs.length; loop++) {
			levelOneLIs[loop].onmouseover = mouseOverLevelOneMenu;
			levelOneLIs[loop].onmouseout = mouseOutLevelOneMenu;
		}
	}

	function mouseOverLevelOneMenu() {
		if (hideSubMenuTimer != null) {
			clearTimeout(hideSubMenuTimer);
			actualHideSubMenu(menuShown.id);
		}
		menuShown = this;
		this.className += ' showSubMenu';
		if (menuSelected != null && menuSelected != this) menuSelected.className += ' doNotShowSubMenu';
	}

	function mouseOutLevelOneMenu() {
		if (menuSelected != null && menuSelected != this) {
			menuSelected.className = menuSelected.className.replace(' doNotShowSubMenu', '');
		}
		if (hideSubMenuTimer != null) clearTimeout(hideSubMenuTimer);
		hideSubMenuTimer = setTimeout('actualHideSubMenu(\'' + this.id + '\')', menuDelay);
	}

	function actualHideSubMenu(whichMenu) {
		hideSubMenuTimer = null;
		if (menuShown.id == whichMenu) menuShown = null;
		var LI = document.getElementById(whichMenu);
		LI.className = LI.className.replace(' showSubMenu', '');
	}
*/


/* Onload code */
	if (typeof window.onload == 'function') {
		var _tempOnload = window.onload;
		window.onload = function() {
			_tempOnload();
			enhanceMarkup();
		}
	} else {
		window.onload = function () {
			enhanceMarkup();
		};
	}

	function enhanceMarkup() {
		// For IE/Win, add things that aren't provided by default
		// Note: ieWin is NOT set to true for IE 7. This is not an issue, as it now supports min-width, etc
		if (typeof(ieWin) == 'boolean' && !ieMac) {
			//styleAbbr();		// Add support for abbr styling without having to upgrade to XHTML for the namespace fix - NO LONGER NEEDED. DONE SERVE-SIDE
			patchFocus();		// Add support for ":focus" pseudo class on input elements
			//if (document.getElementById('subNav')) patchHovers();		// Add support for hovering over non-anchor elements in the thumnbail navigation section

			// Set up min width fixes

			// IE 5.x require a different sized minimum on the footer element
			var majorVersion = -1;
			if ((tempPos = navigator.appVersion.indexOf('MSIE ')) != -1) majorVersion = parseInt(navigator.appVersion.substr(tempPos + 5), 10);
			if (majorVersion == 5) {
				document.getElementById('footer').style.behavior = 'url(/assets/behaviours/ieMinWidthFix.htc?minWidth=880px&normalWidth=55em)';
			} else {
				document.getElementById('footer').style.behavior = 'url(/assets/behaviours/ieMinWidthFix.htc?minWidth=856px&normalWidth=53em)';
			}

			document.getElementById('centreContent').style.behavior = 'url(/assets/behaviours/ieMinWidthFix.htc?minWidth=880px&normalWidth=55em)';
			document.getElementById('contentOuter').style.behavior = 'url(/assets/behaviours/ieMinWidthFix.htc?minWidth=880px&normalWidth=55em)';
			document.getElementById('navigation').style.behavior = 'url(/assets/behaviours/ieMinWidthFix.htc?minWidth=880px&normalWidth=55em)';
			document.getElementById('content').style.behavior = 'url(/assets/behaviours/ieMinWidthFix.htc?minWidth=600px&normalWidth=37.5em)';
			if (document.getElementById('sideBoxOuter'))
				document.getElementById('sideBoxOuter').style.behavior = 'url(/assets/behaviours/ieMinWidthFix.htc?minWidth=192px&normalWidth=12em)';
		}

/*
		if (!ieMac) {
			// Patch thumbnails to show relevant "page" of information
			// If this is run on IE/Mac, the navigation vanishes! Sorry IE/Mac users - you get the non-sexy version of the page
			if (document.getElementById('subNav')) patchPortfolioThumbnails();
		}
*/
		// Patch back-to-top functionality
		patchBackToTopLinks();
/*
		// Patch menu rollovers
		var LIs = document.getElementById('navigation').getElementsByTagName('li');
		for (var loop=0; loop<LIs.length; loop++) {
			if ((' ' + LIs[loop].className + ' ').indexOf(' levelOne ') != -1) {
				if (typeof(LIs[loop].id) == 'undefined' || LIs[loop].id == '') LIs[loop].id = 'levelOneMenuItemID' + loop;
				levelOneLIs[levelOneLIs.length] = LIs[loop];
				if ((' ' + LIs[loop].className + ' ').indexOf(' selected ') != -1) menuSelected = LIs[loop];
			}
		}
		patchMenuRollover();
*/
	}
