// popup.js
// Copyright 2008, emlab
// krudd@uow.edu.au, 17/03/08

// Makes any links with the word "popup" in their "target name" open in a new window.
// Also takes the dimensions from the "target name" in the form "WWWxHHH".

function popUpLink()
{
	// grab any dimensions from target name
	var width = 0, height = 0;
	var result = /(\d+)[Xx](\d+)/.exec( this.target );
	if ( result != null ) {
		width = parseInt( result[ 1 ] );
		height = parseInt( result[ 2 ] );
	}

	// open window
	var size = ( width > 0 && height > 0 ) ? ( ',width=' + width + ',height=' + height ) : '';
	var popup = window.open( this.href, this.target, 'toolbar=no,locationbar=no,status=yes,scrollbars=yes,resizable=yes' + size );
	popup.window.focus();

	// grab any alt text and change title
	var img = this.getElementsByTagName('img');
	if ( img && img[0] && img[0].alt && img[0].alt.length )
		setTimeout( function() {
			popup.window.document.title = img[0].alt;
		}, 100 );

	return false;
}

function setupPopUps()
{
	var links = document.getElementsByTagName('a');
	for ( i = 0; i < links.length; i++ ) {
		if ( links[ i ].target.search( /popup/i ) > -1 ) {
			links[ i ].onclick = popUpLink;
			links[ i ].onkeypress = popUpLink;
		}
	}
}

window.onload = setupPopUps;
