// a rel="external"
function externalLinks() {
	if (!document.getElementsByTagName) return;
		var anchors = document.getElementsByTagName('a');
		for (var i=0; i < anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute('href') && anchor.getAttribute('rel') == 'external') anchor.target = '_blank';
	}
}

// IE Opacity fix for png background's
function alphaBackgrounds(){
	var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
	var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);
	for (i=0; i<document.all.length; i++){
		var bg = document.all[i].currentStyle.backgroundImage;
		if (itsAllGood && bg){
			if (bg.match(/\.png/i) != null){
				var mypng = bg.substring(5,bg.length-2);
				document.all[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+mypng+"', sizingMethod='scale')";
				document.all[i].style.backgroundImage = "url('/images/interface/blank.gif')";
			}
		}
	}
}

// Images disabled, but javascript still enabled?
function checkImages() {
	if (document.getElementById) {
		var x = document.getElementById('image-check').offsetWidth;
		if (x < 1) {
			// Disable the stylesheet
			var css = document.getElementById('css');
			document.getElementsByTagName('head')[0].removeChild(css);

			// Force a repaint, damn you IE
			window.resizeBy(1,0);
			window.resizeBy(-1,0);
		}
	}
}

// Execute the command if key pressed is action key (space|return|enter)
// e.g. onclick="myFunction();" onkeyup="actionClick(this.onclick);"
function whichKey(e){
	var code;
    if(!e) var e=window.event;
    if(e.keyCode){code=e.keyCode;}
    else if(e.which){code=e.which;}
	action = (code==13||code==32) ? true : false;
}
function actionClick(args){if(action && args)eval(args)}

// Default values in input boxes
function defaultVals(){
	var inputs = document.getElementsByTagName('input');
	var txtareas = document.getElementsByTagName('textarea');
	var obj;

	for(var i=0;i<inputs.length;i++){
		if((obj=inputs[i]) && obj.getAttribute('type')=='text' && obj.className.match(/userinput/gi)){
			obj.onfocus = function(){if(this.value=='*') this.value='';}
			obj.onblur = function(){if(!this.value.replace(/\s+/,'')) this.value='*';}
		}
	}
	for(i=0;i<txtareas.length;i++){
		if((obj=txtareas[i]) && obj.className.match(/userinput/gi)){
			obj.onfocus = function(){if(this.value=='*') this.value='';}
			obj.onblur = function(){if(!this.value.replace(/\s+/,'')) this.value='*';}
		}
	}
}

// document.getElementById
function $(id){
	return document.getElementById(id);
}

// Assign styles to an object
function setStyle(obj,styles){
	if(obj.style.setAttribute){
		obj.style.setAttribute('cssText',styles,0);
	}
	else{
		obj.setAttribute('style',styles)
	}
}

// Corners on trail links
function pixel(){
	if($('breadcrumbs')){
		var a = $('breadcrumbs').getElementsByTagName('a');
		if(a.length){
			var TL = [];
			var TR = [];
			var BL = [];
			var BR = [];

			for(var i=0;i<=a.length;i++){
				if(a[i]){

					TL[i] = document.createElement('img');
					TL[i].setAttribute('src','/images/general/TL.gif');
					setStyle(TL[i],'position:absolute;top:0px;left:0px;width:2px;height:2px;');

					TR[i] = document.createElement('img');
					TR[i].setAttribute('src','/images/general/TR.gif');
					setStyle(TR[i],'position:absolute;top:0px;left:'+(a[i].offsetWidth-2)+'px;width:2px;height:2px;');

					BL[i] = document.createElement('img');
					BL[i].setAttribute('src','/images/general/BL.gif');
					setStyle(BL[i],'position:absolute;top:'+(a[i].offsetHeight-2)+'px;left:0px;width:2px;height:2px;');

					BR[i] = document.createElement('img');
					BR[i].setAttribute('src','/images/general/BR.gif');
					setStyle(BR[i],'position:absolute;top:'+(a[i].offsetHeight-2)+'px;left:'+(a[i].offsetWidth-2)+'px;width:2px;height:2px;');

					setStyle(a[i],'position:relative');

					a[i].style.position = 'relative';
					a[i].appendChild(TL[i]); 
					a[i].appendChild(TR[i]); 
					a[i].appendChild(BL[i]); 
					a[i].appendChild(BR[i]);
				}
			}
		}
	}
}

document.onkeydown = function(){
	whichKey;
}
window.onload = function(){
	externalLinks();
	checkImages();
	defaultVals();
	footnoteLinks('content','content');
	if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
		//alphaBackgrounds();
	}
	pixel();
}