
/*
	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', '');
			};
		}
	}


/* 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);
					};
		}
	}


/* Stops IE giving the "Click here to active this..." message (part of the Eolas patent ruling) for "Satay"-embedded Flash objects */
	function patchEolasPatent() {
		var objects = document.getElementsByTagName('object');
		for (var loop=0; loop<objects.length; loop++) {
			var object = objects[loop];
			if (object.type == 'application/x-shockwave-flash') {
				var _type = object.type;
				var _data = object.data;
				var _width = object.width;
				var _height = object.height;
				var _noFlash = object.innerHTML;
				var _html = '';
				_html += '<object type="application/x-shockwave-flash" data="' + _data + '" width="' + _width + '" height="' + _height + '">';
				_html += '<param name="movie" value="' + _data + '" />';
				_html += _noFlash;
				_html += '</object>';
				object.parentNode.innerHTML = _html;
			}
		}
	}


/* Onload code */
	if (window.addEventListener) {
		window.addEventListener('load', enhanceMarkup, false);
	} else if (window.attachEvent) {
		window.attachEvent('onload', enhanceMarkup);
	} else {
		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 (ieWin === true && !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
			patchEolasPatent();	// Bypass the Eolas patent malarky

			// 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/ieMinFix.htc?minWidth=880px&normalWidth=55em)';
			} else {
				document.getElementById('footer').style.behavior = 'url(/assets/behaviours/ieMinFix.htc?minWidth=856px&normalWidth=53em)';
			}

			// Min-width fixes for IE
			document.getElementById('centreContent').style.behavior = 'url(/assets/behaviours/ieMinFix.htc?minWidth=880px&normalWidth=55em)';
			document.getElementById('contentOuter').style.behavior = 'url(/assets/behaviours/ieMinFix.htc?minWidth=880px&normalWidth=55em)';
			document.getElementById('navigation').style.behavior = 'url(/assets/behaviours/ieMinFix.htc?minWidth=880px&normalWidth=55em)';
			document.getElementById('content').style.behavior = 'url(/assets/behaviours/ieMinFix.htc?minWidth=600px&normalWidth=37.5em)';
			if (el = document.getElementById('sideBoxOuter')) el.style.behavior = 'url(/assets/behaviours/ieMinFix.htc?minWidth=192px&normalWidth=12em)';
			if (el = document.getElementById('subNav')) el.style.behavior = 'url(/assets/behaviours/ieMinFix.htc?minWidth=600px&normalWidth=37.5em)';
			if (el = document.getElementById('banners')) el.style.behavior = 'url(/assets/behaviours/ieMinFix.htc?minWidth=600px&normalWidth=37.5em)';

			// Min-height fixes for IE
			if (el = document.getElementById('quickFind')) el.style.behavior = 'url(/assets/behaviours/ieMinFix.htc?minHeight=88px&normalHeight=5.5em)';
		}

		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 non-browser-specific stuff
		patchBackToTopLinks();
		try {
			if (patchQuickFindSelectBoxes) patchQuickFindSelectBoxes();	// Add onchange events to the quick-find select elements
		} catch(e) {};
	}
