/*		externallinks.js
		by Paul Novitski - www.juniperwebcraft.com
		February 2007

This script converts all absolute URL hyperlinks to open in a new window.
19 April 2007 - also any link with the class "external"
*/

//=========================
// load script when document loads
//=========================
var loadExtLinks = window.onload;

window.onload = function()
{
		if (loadExtLinks) loadExtLinks();

		// check for DOM-awareness
		if (!document.getElementById) return;
		if (!document.getElementsByTagName) return;

	jsExtLinks();
}


//=========================
function jsExtLinks()
//=========================
{
	//alert("function jsExtLinks()");

	var sPrompt = "(Opens in a new window)";

	var aLinks = document.getElementsByTagName("A");
	
	//alert("aLinks.length = " + aLinks.length);
	
	for (var iLink = 0; iLink < aLinks.length; iLink++)
	{
		var bExternal = false;
		var bPopup = false;
		
		var sClass = aLinks[iLink].className;		
		//var sClass = aLinks[iLink].getAttribute("class");
			if (sClass && sClass == 'popup')
			{
				bPopup = true;
			}
			else if (sClass && sClass == 'external')
			{
				bExternal = true;
			}
			else
			{		
				var sHref = aLinks[iLink].getAttribute("href");
				//alert(iLink + ": " + sHref);
				
					if (!sHref) continue;
					if (sHref == "") continue;

					if (sHref.substring(0, 4) == "http") bExternal = true;
					
					// don't open new window for any page in this website family
					if (sHref.indexOf("//dot-font.com") >= 0
					 || sHref.indexOf("//www.dot-font.com") >= 0
					 || sHref.indexOf("//johndberry.com") >= 0
					 || sHref.indexOf("//www.johndberry.com") >= 0
					) bExternal = false;

			}
		
			if (!bExternal && !bPopup) continue;
		
			if (bExternal)
			{
				var sClickFunction = jsExtLinksClick;
			}
			else if (bPopup)
			{
				var sClickFunction = jsPopupLinksClick;
			}
			else
			{
				continue;
			}
		
		aLinks[iLink].onclick = sClickFunction;

		var sTitle = aLinks[iLink].getAttribute("title");
		
			if (!sTitle) sTitle = '';
			if (sTitle.indexOf("new window") >= 0) continue;

			if (sTitle == "")
			{
				sTitle = sPrompt;
			}
			else
			{
				sTitle = sTitle + " " + sPrompt;
			}
		
		aLinks[iLink].setAttribute("title", sTitle);
		//alert("title: " + iLink + ": " + sTitle);
	}
}


//=========================
function jsExtLinksClick(evt)
//=========================
{
	// cancel event-bubbling
		if (evt) { event = evt; }
	event.cancelBubble = true;

	var sHref = this.getAttribute("href");

	window.open (sHref, "_blank");	

	return false;
}



//=========================
function jsPopupLinksClick(evt)
//=========================
{
	// cancel event-bubbling
		if (evt) { event = evt; }
	event.cancelBubble = true;
	
	// see if it's an image
	var rCheckImage = /\.(jpg|jpeg|gif|png)$/i;
	var rGetDims = /-(\d+)x(\d+)\.(jpg|jpeg|gif|png)$/i;
	
	var sPopupHref = 'popupImage.php?img=';
	
	var sHref = this.getAttribute("href");

	var bIsImage = rCheckImage.test(sHref);
	
	//alert((bIsImage) ? 'An image' : 'Not an image');

		if (bIsImage)
		{
			var aMatches = rGetDims.exec(sHref);
			//alert(aMatches.join('\n'));
			sHref = sPopupHref + encodeURIComponent(sHref);
			var sWindowName = "Image Pop-up";
			var sFeatures = "menubar=no,toolbar=no,directories=no,personalbar=no,scrollbars=no";
				if (aMatches) sFeatures += ",width=" + aMatches[1] + ",height=" + aMatches[2];
		}
		else
		{
			var sWindowName = "New Window";
			var sFeatures = '';
		}

	window.open(sHref, sWindowName, sFeatures);

	return false;
}


