// JavaScript Document for opening centered browser windows

function openWindow(URL)
{
	//preset variables
	var w = 800; //width
	var h = 600; //height
	
	var winName = 'portfolio'; //don't use spaces here - blows up IE
	
	features = 'menubar=no,scrollbars=yes,resizable=no';
	
	// if screen.dimension is true (available),
	// then calculate the position using that value, else use a fixed value
	xPos = (screen.width) ? (screen.width-w)/2 : 50;
	yPos = (screen.height) ? (screen.height-h)/2 : 50;
 
	// assemble all window settings from calculations and parameters
	settings = 'height='+h+',width='+w+',top='+yPos+',left='+xPos+','+features;
 
	// open the window and bring it to front
	thisWin=window.open(URL,winName,settings);
	
	if(window.focus)
	{
		thisWin.focus();
	}
}

function getAnchors()
{
	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName("a");
	var swapAnchors = new Array();
	var counter = 0;

	// loop through all anchor tags
	for (var i=0; i<anchors.length; i++)
	{
		var anchor = anchors[i];

		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "newWindow"))
		{
			swapAnchors[counter] = anchor; //store all anchors that pass above if test into new array
			
			swapAnchors[counter].onclick = function () {openWindow(this.href); return false;}
			++counter;
		}
	}
}


//
// 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(getAnchors);	// run initSwap onLoad