//*** This code is copyright 2002-2003 by Gavin Kistner and Refinery; www.refinery.com
//*** It is covered under the license viewable at http://phrogz.net/JS/_ReuseLicense.txt
function addClass(obj,cName){removeClass(obj,cName); return obj && (obj.className+=(obj.className.length>0?' ':'')+cName); }
function removeClass(obj,cName){ return obj && (obj.className=obj.className.replace(new RegExp("^"+cName+"\\b\\s*|\\s*\\b"+cName+"\\b",'g'),'')); }
function hasClass(obj,cName){ return (!obj || !obj.className)?false:(new RegExp("\\b"+cName+"\\b")).test(obj.className) }

function addRel(obj,cName){removeRel(obj,cName); return obj && (obj.rel+=(obj.rel.length>0?' ':'')+cName); }
function removeRel(obj,cName){ return obj && (obj.rel=obj.rel.replace(new RegExp("^"+cName+"\\b\\s*|\\s*\\b"+cName+"\\b",'g'),'')); }
function hasRel(obj,cName){ return (!obj || !obj.rel)?false:(new RegExp("\\b"+cName+"\\b")).test(obj.rel) }

// trim
function trim(value) {
	//var re = new RegExp("^(\s*)([\W\w]*)(\b\s*$)");	return value.replace(re, '$2');
	return value.replace(/^\s+|\s+$/g, '');
}
function remove_whitespace(value) {
	return value.replace(/\s+/g, '');
}
function normalise_website_string(value) {
	if(value.substring(0,7).toLowerCase()!='http://') value = 'http://'+value;
	return value;
}

// element check
function elementCheck(id) {
	if(!document.getElementById && !document.getElementById(id)) return false;
	return document.getElementById(id);
}

// getElementStyle
function getElementStyle(elemID, IEStyleProp, CSSStyleProp) {
	var elem = document.getElementById(elemID);
	if(elem.currentStyle) {
		return elem.currentStyle[IEStyleProp];
	}
	else if(window.getComputedStyle) {
		var compStyle = window.getComputedStyle(elem, '');
		return compStyle.getPropertyValue(CSSStyleProp);
	}
	return '';
}

function create_list_item(text,url,class_name) {
	var el_li = document.createElement('li');
	var el_a = document.createElement('a');
	var el_a_text_node = document.createTextNode(text);
	
	el_a.href = url;
	el_a.appendChild(el_a_text_node);
	el_li.appendChild(el_a);
	
	if(class_name) {
		if(class_name instanceof Array) for(var i=0; i<class_name.length; i++) addClass(el_li,class_name[i]);
		else addClass(el_li,class_name);
	}
	
	return el_li;
}

// getFragment - #this-bit
function getFragment(el) {
	if(el==location) return location.href.split('#')[1];
	if(!el.tagName && !(/a/i.test(el.tagName))) return false;
	var hash = el.hash.substr(1);
	if(!hash) return false;
	return hash;
}

// removeChildren like el.innerHTML = '';
function removeChildren(el) {
	while(el.firstChild) el.removeChild(el.firstChild);
}

// insertAfter
function insertAfter(newElement,targetElement) {
	var parent = targetElement.parentNode;
	if(parent.lastChild == targetElement) {
		parent.appendChild(newElement);
	} else {
		parent.insertBefore(newElement,targetElement.nextSibling);
	}
}

// getWindowSize
function getWindowSize() {
	var size_array = new Object();
	var myWidth = 0, myHeight = 0;
	if(typeof( window.innerWidth )=='number') {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	}
	else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} 
	else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	size_array['width'] = myWidth;
	size_array['height'] = myHeight;
	return size_array;
}

// traverse - traverse(element,'fieldset','parent');
function traverse(el,elMatch,direction) {
	var elMatch = elMatch.toLowerCase();
	var direction = direction.toLowerCase();
	
	while(el.nodeType != 1 || !el.tagName || elMatch != el.tagName.toLowerCase()) {
		switch (direction) {
			case 'parent':
				if(!el.parentNode) return false;
				el = el.parentNode;
				break;
			case 'previous':
				if(!el.previousSibling) return false;
				el = el.previousSibling;
			case 'next':
				if(!el.nextSibling) return false;
				el = el.nextSibling;
		}
	}
	return el;
}

var target_class = 'target_x';
function x_target_setup() {
	if(!document.createElement || !document.getElementsByTagName || !document.getElementById) return false;
	
	if(window.watch) window.watch('location',x_target);
	frag = x_target();
	// loop to check whether the current hash has changed, if so change based upon hash
	window.setInterval(function(a,b) {
		var check_hash = getFragment(location);
		if(check_hash && frag!=check_hash) frag = x_target();
	}, 100);
}
function x_target() {
	var frag = getFragment(location);
	var el_array = getElementsByClassName(document.body,'*',target_class);
	for(var i=0; i<el_array.length; i++) removeClass(el_array[i],target_class);
	
	var el_array = getElementsByClassName(document.body,'a','back-to-top');
	for(var i=0; i<el_array.length; i++) el_array[i].parentNode.removeChild(el_array[i]);
	
	var module_faq_set = getElementsByClassName(document.body,'div','module-faqs');

	if(frag && module_faq_set!='') {
		var frag_el = document.getElementById(frag);
		if(frag_el) {
			addClass(frag_el,target_class);
			new_a = document.createElement('a'); new_a.href = '#container';	addClass(new_a,'back-to-top');
			new_a_text = document.createTextNode('Back to top');
			new_space = document.createTextNode(' ');
			new_a.appendChild(new_a_text);
			frag_el.appendChild(new_space);
			frag_el.appendChild(new_a);
		}
	}
	return frag;
}
addEvent(window,'load',x_target_setup);