var Barclays = {}
	Barclays.Common = {
		/*	Original code developed by Robert Nyman, http://www.robertnyman.com	Code/licensing: http://code.google.com/p/getelementsbyclassname/ */
		getElementsByClassName: function (oElm, strTag, strClassName){
				if (document.getElementsByClassName) {
						getElementsByClassName = function (oElm, strTag, strClassName) {
								oElm = oElm || document;
								var elements = oElm.getElementsByClassName(strClassName),
										nodeName = (strTag && (strTag !== "*"))? new RegExp("\\b" + strTag + "\\b", "i") : null,
										returnElements = [],
										current;
								for(var i=0, il=elements.length; i<il; i+=1){
										current = elements[i];
										if(!nodeName || nodeName.test(current.nodeName)) {
												returnElements.push(current);
										}
								}
								return returnElements;
						};
				}
				else if (document.evaluate) {
						getElementsByClassName = function (oElm, strTag, strClassName) {
								strTag = strTag || "*";
								oElm = oElm || document;
								var classesToCheck = "",
										xhtmlNamespace = "http://www.w3.org/1999/xhtml",
										namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
										returnElements = [],
										elements,
										node;           
										classesToCheck += "[contains(concat(' ', @class, ' '), ' " + strClassName + " ')]";
		 
								try     {
										elements = document.evaluate(".//" + strTag + classesToCheck, oElm, namespaceResolver, 0, null);
								}
								catch (e) {
										elements = document.evaluate(".//" + strTag + classesToCheck, oElm, null, 0, null);
								}
								while ((node = elements.iterateNext())) {
										returnElements.push(node);
								}
								return returnElements;
						};
				}
				else {
						getElementsByClassName = function (oElm, strTag, strClassName) {
								strTag = strTag || "*";
								oElm = oElm || document;
								var classes = strClassName.split(" "),
										classesToCheck = [],
										elements = (strTag === "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTag),
										current,
										returnElements = [],
										match;
								for(var k=0, kl=classes.length; k<kl; k+=1){
										classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
								}
								for(var l=0, ll=elements.length; l<ll; l+=1){
										current = elements[l];
										match = false;
										for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
												match = classesToCheck[m].test(current.className);
												if (!match) {
														break;
												}
										}
										if (match) {
												returnElements.push(current);
										}
								}
								return returnElements;
		 
						};
				}
				return getElementsByClassName(oElm, strTag, strClassName);
		},

		/*
		setupResizing: function() {	
			var resizeTimeoutId;    //timer is needed to get around IE6 firing multiple resize events on window.resize
			window.onresize = function(event) {			
					 window.clearTimeout(resizeTimeoutId);
					 resizeTimeoutId = window.setTimeout("BT.windowsResizeActions()", 1);
			}
		},
		*/
		
		//Accordion Functions
		 setupExpandableAreas: function() {
			//find all expandable areas on page
			expandableAreas=this.getElementsByClassName(document, "div","expandableContent");
			for (var i=0;i<expandableAreas.length;i++)	{
				expandableAreas[i].id = "expandableArea_"+i;
				var oEl=document.getElementById(expandableAreas[i].id);
				//collapse all expandable areas - unless wrapper has class defaultToOpen
				if (oEl.className.indexOf("defaultToOpen")==-1)	{
					if (oEl.className.indexOf("expandableContentCollapsed")==-1)	{
						oEl.className+= " expandableContentCollapsed";
					}
				}
				//if shortdesc has radio/checkbox form elements and answer also has form elements then open on 
				//if shortdesc has radio form element, close the other related radio groups' answer
				if(getElementsByClassName(expandableAreas[i],"div","question").length>0){
					var thisquestion=this.getElementsByClassName(expandableAreas[i],"div","question")[0];
					// add closure if class withClosure is given
					if (thisquestion.className.indexOf("withClosure")!=-1)	{
						var closure='<div class="closureButton"><a href="#"><span></span></a></div>';
						thisquestion.innerHTML=closure+thisquestion.innerHTML;
					}
					var questionInputs=thisquestion.getElementsByTagName("input");
					var answerInputs=this.getElementsByClassName(expandableAreas[i],"div","answer")[0].getElementsByTagName("input");
					for(var j=0;j<questionInputs.length;j++){
						if(answerInputs.length==0){continue;}
						if(questionInputs[j].type=="radio" || questionInputs[j].type=="checkbox"){
							questionInputs[j].iteration=i;
							
							function collapseUnselected(e){
								if(!e){var e=window.event;}
								var elem=(e.currentTarget)?e.currentTarget:e.srcElement;
								if(elem.nodeType==3){elem=elem.parentNode;}
								var expandableArea=document.getElementById("expandableArea_"+elem.iteration);
								if(expandableArea.className.indexOf("expandableContentCollapsed")!=-1){
									expandableArea.className=expandableArea.className.replace("expandableContentCollapsed","");
								}
								elem.checked=true;
								if(elem.type=="radio"){
									var radiogroup=elem.name;
									//go through all expandable areas, get their question input, if it matches radiogroup then add the collapse			
									for(var k=0;k<expandableAreas.length;k++){
										var sdInput=this.getElementsByClassName(expandableAreas[k],"div","question")[0].getElementsByTagName("input")[0];
										if(sdInput.name==radiogroup){
											if (sdInput.checked)	{
												if(expandableAreas[k].className.indexOf("expandableContentCollapsed")==-1){
													expandableAreas[k].className+=" expandableContentCollapsed";
												}
											}
										}
									}
								}
							}
							
							questionInputs[j].addEventListener ? (questionInputs[j].addEventListener("click",collapseUnselected,false)) : (questionInputs[j].attachEvent("onclick",collapseUnselected));
						}
					}
				}
				//remove link from anchor, assisgn id
				if (getElementsByClassName(expandableAreas[i], 'div', 'question')[0]) {
					 if (expandableAreaAnchor = getElementsByClassName(expandableAreas[i], 'div', 'question')[0].getElementsByTagName("a")[0])	{
						expandableAreaAnchor.id = "expandableAreaAnchor_"+i;
						expandableAreaAnchor.href="#";
						expandableAreaAnchor.onclick = function () {
							elementId = this.id.split("_").reverse()[0]; //puts the element id number at the front of array
							Barclays.Common.openOrCloseExpandableContent("expandableArea_" + elementId );
							return false;
						}
					}
				}
				if (getElementsByClassName(expandableAreas[i], 'div', 'question')[0]) {
					 if (exp2Parent=getElementsByClassName(expandableAreas[i], 'div', 'closureButton')[0])	{
						if (expandableAreaAnchor2 = getElementsByClassName(expandableAreas[i], 'div', 'question')[0].getElementsByTagName("a")[1])	{
							expandableAreaAnchor2 = getElementsByClassName(expandableAreas[i], 'div', 'question')[0].getElementsByTagName("a")[1];
							expandableAreaAnchor2.id = "expandableAreaAnchor2_"+i;
							expandableAreaAnchor2.href="#";
							expandableAreaAnchor2.onclick = function () {
								elementId = this.id.split("_").reverse()[0]; //puts the element id number at the front of array
								Barclays.Common.openOrCloseExpandableContent("expandableArea_" + elementId );
								return false;
								
							}
						}
					}
				}
			}
		},

		openOrCloseExpandableContent: function(id) {
			var oEl=document.getElementById(id);
			if (oEl.className.indexOf("expandableContent")!=-1)	{
				oEl.className=oEl.className.replace(" defaultToOpen","");
				if (oEl.className.indexOf("expandableContentCollapsed")==-1) 	{
					oEl.className += " expandableContentCollapsed";
				} else {
					oEl.className=oEl.className.replace(" expandableContentCollapsed","");
				}
			}
		}
	}; 

	Barclays.Custom = {
		init: function(){
			if (window.initialised){return;}
			window.initialised=true;
			//setupExpandableAreas();
			this.filterSearch();
			this.clearFields();
			this.externalLinks();
			this.positionElements();
			Barclays.Common.setupExpandableAreas();
		},
		
		filterSearch: function(){
			var showFilter = Barclays.Common.getElementsByClassName(document, "span", "showFilter");
			var filterContainer = Barclays.Common.getElementsByClassName(document, "ul", "searchFilter");
			list = filterContainer[0];
			if(filterContainer.length > 0){
				list.className = "searchFilter hide";
				var oLink = document.createElement('a');
				oLink.id =  'showFilterLink';
				oLink.href = '#'; // Make the link focusable for keyboard users
				oLink.appendChild(document.createTextNode('Filter by topic'));
				oLink.onclick = function() {
					if(list.className == 'searchFilter hide'){
						//list.setAttribute("className", "searchFilter show");
						list.className = "searchFilter show";
					//	list.className.replace("searchFilter hide","searchFilter show");
						document.getElementById('showFilterLink').innerHTML = 'Close filter';
					}
					else{
						//list.style.display = 'none';
						list.className = "searchFilter hide";					
						document.getElementById('showFilterLink').innerHTML = 'Filter by topic';
					}
				}
				showFilter[0].appendChild(oLink);
			}
			return;
		},
		
		clearFields: function(){
			var inp = document.getElementsByTagName('input');
			for(var i = 0; i < inp.length; i++) {
				if(inp[i].type == 'text') {
					var temp = inp[i].className;
					inp[i].value = inp[i].className.split('//')[1];
					inp[i].onfocus = function() {
						if(this.value == this.className.split('//')[1]) {
							this.value = '';
						}
						else {
							return false;
						}
					}
					inp[i].onblur = function() {
						if(this.value == '') {
							this.value = this.className.split('//')[1];
						} else {
							return false;
						}
					}
				}
			}
			return;
		},
		
		
		getWindowSize: function () {
			var thewidth = 0;
			//Non-IE
			if(typeof(window.innerWidth)=='number') {
				thewidth = window.innerWidth;
			} 
			//IE 6+ in 'standards compliant mode'
			else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
				thewidth = document.documentElement.clientWidth;
			} 
			//IE 4 compatible
			else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
				thewidth = document.body.clientWidth;
			}
			return thewidth;
		},
		
		 positionElements: function() {
			if (!document.getElementsByTagName) return false;
			if (!document.getElementById) return false;
			if (!document.getElementById("relatedContent")) return false;
			var screenwidth = 0;
			var thediv = document.getElementById("relatedContent");
			var maindiv = document.getElementById("pageWrapper");
			this.switchClass(thediv);
			this.setWidth(maindiv);
			window.onresize = function() {
				Barclays.Custom.switchClass(thediv);
				Barclays.Custom.setWidth(maindiv);
				//Barclays.Custom.resizeFlashCarousel();
			}
		},
		
		setWidth: function(element){
			screenwidth = this.getWindowSize();
			if (screenwidth > 880) {
				this.changeClass(element,"fullWidth");
			}
			else {
				this.changeClass(element,"reducedWidth");
			}
		},
		
		switchClass: function(element) {
			screenwidth = this.getWindowSize();
			if (screenwidth > 880) {
				this.changeClass(element,"hasWidth");
			}
			else {
				this.changeClass(element,"hasNoWidth");
			}
		},
		
		changeClass: function(element,value) {
			if (value == "") {
				element.className = "";
			}
			else {
				if (!element.className) {
					element.className = value;
				} 
				else {
		//			element.className+= " ";
					element.className= value;
				}
			}
		},

		externalLinks: function (){
			var anchorTags = document.getElementsByTagName('a');
			var len = anchorTags.length;
			
			for(var i=0; i<len; i++){
				if("external" == anchorTags[i].getAttribute('rel')){
					anchorTags[i].setAttribute('target','_blank');
				}
			}
		}

	}; 


/*
 * SETUP DOM READY AND LOAD EVENTS HERE
*/
window.onload = function(){
	Barclays.Custom.init();
}
