/**
@description  Adds the function refferrence passed as a pararmeter to the onload event
@param  String:function to be called
@return  Void
*/
function addLoadListener(fn)
{
  if (typeof window.addEventListener != 'undefined')
  {
    window.addEventListener('load', fn, false);
  }
  else if (typeof document.addEventListener != 'undefined')
  {
    document.addEventListener('load', fn, false);
  }
  else if (typeof window.attachEvent != 'undefined')
  {
    window.attachEvent('onload', fn);
  }
  else
  {
    var oldfn = window.onload;
    if (typeof window.onload != 'function')
    {
      window.onload = fn;
    }
    else
    {
      window.onload = function()
      {
        oldfn();
        fn();
      };
    }
  }
}



/**
@description  Sets the target element with the supplied class
@param  String: id of the target element
@param  String: class name
@return  Void
*/
function setElementClass(obj, classText) 
{ 
	if(identity = document.getElementById(obj))
	{
		identity.className = classText;
	}
}


function externalAndPopUpLinks()
{
	var defW = 640;
	var defH = 480;
	/* todo - check for missing or invalid width and height settings and set a default value for the popup*/
	var popupTitle = 'popup';
	var scrW = screen.availWidth;
    var scrH = screen.availHeight;
    var anchors = document.getElementsByTagName("a"); 
    var theAnchor, i, target, relIndex, relSplit, linkDest; 
    for (i=0;i<anchors.length;i++)
	{
		theAnchor = anchors[i];
		/* does this anchor have a rel attribute set? */
		if( theAnchor.getAttribute("href") && theAnchor.getAttribute("rel") != null)
		{
			linkDest = theAnchor.getAttribute("href"); 
			relIndex = theAnchor.getAttribute("rel"); 
			relSplit = relIndex.split(" ");
			switch(relSplit[0]) 
			{
				case 'external' :
					applyBlankTarget(theAnchor);
				break;
				case 'popup' :
					/* if popup parameters missing fill in with defaults */
					if(relSplit[1]==null){relSplit[1]='popup';}
					if(relSplit[2]==null){relSplit[2]=defW;}
					if(relSplit[3]==null){relSplit[3]=defH;}
					theAnchor.setAttribute("rel" , relSplit[0] + ' ' + relSplit[1] + ' ' + relSplit[2] + ' ' + relSplit[3]);
					theAnchor.title+=relSplit[1];
					theAnchor.title+=' (popup that opens in new window)';
					popupW=relSplit[2];
					popupH=relSplit[3];
					theAnchor.title+=' ' + popupW + 'x' + popupH;
					theAnchor.onclick=function() {newWin=window.open(this.href,this.rel.split(" ")[1],'location=no,menubar=no,status=no,resizable=yes,toolbar=no,scrollbars=yes,width=' + this.rel.split(" ")[2] + ',height=' + this.rel.split(" ")[3]);if(window.focus){newWin.focus()} return false;}
				break;
			}
		}
	}
}

function applyBlankTarget(elementRef)
{
	// Put a span after the a and put the external bg image in it
	var externalSpan = document.createElement('span');
	var spanTextNode = document.createTextNode(' ');
	externalSpan.appendChild(spanTextNode);
	externalSpan.className = 'external';
	elementRef.parentNode.insertBefore(externalSpan, elementRef.nextSibling);
	
	// Set to open the page in a new window
	elementRef.title +=' (Opens in a new window)';
	elementRef.target = '_blank';
}

