
/**
 * Add an event handler function to an object
 * 
 * @param {Object} The object
 * @param {String} The event name to listen for, eg "load", "unload", "mousedown"
 * @param {Function} The function to call on the event
 */
function addEventHandler( targetObject, eventType, handlerFunc ) {
	if( targetObject.addEventListener ) {
		targetObject.addEventListener( eventType, handlerFunc, true );
	} else if( targetObject.attachEvent ) {
		targetObject.attachEvent( 'on' + eventType, handlerFunc );
	}
}


/**
 * Request XML from the server by GET or POST
 * 
 * @param {String} Relative URL to call
 * @param {Function} Callback function to recieve data on success
 * @param {String} Type of request - GET or POST
 * @param {String} Data to send to the server
 */
function requestXml( url, callback, type, data ) {
	
	var client = new XMLHttpRequest();

	type = ( type ) ? type : 'GET';
	data = ( data ) ? encodeURI( data ) : null;
	url += ( ( url.indexOf( '?' ) != -1 ) ? '&' : '?' ) + '&rand=' + Math.random();
	
	if( typeof( callback ) == "function" ) {
		client.onreadystatechange = function() {
			if( client.readyState == 4 ) {
				callback( client.responseXML );
			}
		}
	}
	client.open( type, url, true );
	client.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );	
	client.send( data );
}

/**
 * Open a popup window with a fixed width and height
 * Places new windows in the centre of the screen
 *
 * @param {String} The URL
 * @param {Number} Window width
 * @param {Number} Window height
 */
function openPopupWindow( windowName, windowUrl, windowWidth, windowHeight ) {
	var windowTop = ( screen.height / 2 ) - ( windowHeight / 2 );
	var windowLeft = ( screen.width / 2 ) - ( windowWidth / 2 );
	var features = 'height=' + windowHeight + ',width=' + windowWidth + ',top=' + windowTop + ',left=' + windowLeft + ',scrollbars=yes,resizable=yes';
	return window.open( windowUrl, windowName, features );
}

/**
 * Open a new window which will resize to fit the image specified by windowUrl
 *
 * @param {String} windowUrl
 */
function openPhotoWindow( windowUrl, windowTitle ) {
	
	var htm = '<html>' +
	'<head>' +
	'<title>' + windowTitle + '</title>' +
	'<meta http-equiv="imagetoolbar" content="false" />' +
	'' +	
	'<style type="text/css">' +	
	'' +	
	'	body {' +	
	'		margin: auto;' +	
	'		padding: auto;' +	
	'		overflow: hidden;' +	
	'	}' +	
	'' +	
	'	p {' +	
	'		font: 11px Verdana,sans-serif;' + 
	'		position: absolute;' + 
	'		top: 10px;' +
	'		left: 10px;' +	
	'	}' +	
	'' +	
	'	#theDiv {' +	
	'		visibility: hidden; ' +	
	'		position: absolute; ' +	
	'		top: 0; ' +	
	'		left: 0;' +	
	'	}' +	
	'' +	
	'</style>' +	
	'' +	
	'<script type="text/javascript">' +
	'' +	
	'	function setSize() {' +	
	'		document.getElementById( "theDiv" ).style.visibility = "visible";' +
	'		var w = document.images[ "theImage" ].width+10;' +
	'		var h = document.images[ "theImage" ].height+28;' +
	'		window.resizeTo( w, h );' +
	'		window.moveTo( ( screen.width / 2 ) - ( w / 2 ), ( screen.height / 2 ) - ( h / 2 ) );' +
	'	}' +
	'' +
	'</script>' +
	'</head>' +
	'<body onload="setSize();">' +
	'<div id="theDiv"><a href="#" onclick="window.close();" title="Click to close window"><img src="' + windowUrl + '" border="0" id="theImage" alt="' + windowTitle + '" /></a></div>' +
	'</body>' +
	'</html>';	
	
	var photoWin = openPopupWindow( "photoWin", "", 150, 150 );
	photoWin.focus();
	photoWin.document.write( htm );
	photoWin.document.close();
	
}

function getElementsByClassName( needle ) {
	var my_array = document.getElementsByTagName( "*" );
	var retvalue = new Array();
	var i;
	
	for (i = 0; i < my_array.length; i++) {
		var c = " " + my_array[ i ].className + " ";
		if( c.indexOf(" " + needle + " ") != -1 ) {
			retvalue[ retvalue.length ] = my_array[ i ];
		}
	}
	return retvalue;
}
