// JavaScript Document

function checkObjects()
{
	// grab all links and loop through them checking for anchors that need special functions
	var as = document.getElementsByTagName("a");
	for(var i=0;i<as.length;i++)
	{
		//get the link's href and see if the link is external to the current site
		if(as[i].href.indexOf(window.location.hostname) == -1 )
		{
			as[i].target="_blank";

			// don't add external class to email addresses, class=blockLink, class=info (for "more info" buttons on affiliates page, links inside h3 tags,)
			if((as[i].href.indexOf("mailto:") == -1) && (as[i].className.indexOf("blockLink") == -1) && (as[i].parentNode.className.indexOf("info") == -1) && (as[i].parentNode.nodeName != "H3") && (as[i].className.indexOf("mapLink") == -1) && (as[i].className.indexOf("eventsFeedRss") == -1))
			{
				if(as[i].firstChild)
				{
					if(!/img/i.test(as[i].firstChild.nodeName))
					{
						//if anchor does not contain current location then add the .external class and set target to _blank
						as[i].className=as[i].className+" external";
					}
				}
			}
		}
		
		//Popup Windows. Rel attribute for anchor must be set to "popup" 
		if ((as[i].getAttribute("rel")) && (as[i].getAttribute("rel").indexOf("popup") != -1))
		{
			// anchor tag must look like this: <a href="foo.bar" rel="pupup,window name,height,width">Link</a>
			as[i].onclick=function(){return(popup(this));};
		}

		//Close Window. Rel attribute for anchor must be set to "close"
		if (as[i].getAttribute("rel") == "close")
		{
			// close popup window buttons. This script will put the text into the anchor if the browser can use the script. If not, then there will be not button visible.
			as[i].innerHTML = "Close Window";
			as[i].className=as[i].className+ "close";
			as[i].onclick=function(){return(closePopup(this));};
		}
	}
}



function popup(anchorTag)
{
	var rel = anchorTag.getAttribute("rel").split(",");
	var windowAttributes = "height=" + rel[2] + ",width=" + rel[3] + ",menubar,resizable,scrollbars,";
	var windowName = rel[1];
	window.open(anchorTag.href,windowName,windowAttributes);
	return false;
}

function closePopup(windowName)
{
	window.close(windowName);
	alert(window.parent.name);
}

//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
    	window.onload = func;
	} 
	else 
	{
		window.onload = function(){oldonload();func();}
	}
}
addLoadEvent(checkObjects);