/*******************************************************************************************
 * popup
 * Written by Craig Francis
 * Look for links with a class like "popup" or "popup400x200" and turn it into pop-up link
 * where the resulting window is at the specified width (e.g. 400px) and height (e.g. 200px)
 *******************************************************************************************/

	var popup = new function () {

		//--------------------------------------------------
		// Do not allow older browsers to run this script

			if (!document.getElementById || !document.getElementsByTagName) {
				return;
			}

		//--------------------------------------------------
		// Initialisation function used to define the global
		// variables used in this script

			this.init = function () {

				//--------------------------------------------------
				// Regular expression to test the class names

					var popupRegExp = new RegExp('\\bpopup\\d+x\\d+\\b');

				//--------------------------------------------------
				// Loop through all links, and search for any which
				// use the 'popup' class, then add an 'onClick' event.

					var links = document.getElementsByTagName('a');

					for (var i = (links.length - 1); i >= 0; i--) {
						if (popupRegExp.test(links[i].className) || cssjs('check', links[i], 'popup')) {
							links[i].onclick = popup.launch;
							links[i].title = 'Opens in a new window';
						}
					}

			}

		//--------------------------------------------------
		// Function called when the link is used

			this.launch = function () {

				//--------------------------------------------------
				// Return the width and height

					var popupRegExp = new RegExp('\\bpopup(\\d+)x(\\d+)\\b');
					var popupDimentions = popupRegExp.exec(this.className);

					if (popupDimentions && popupDimentions.length == 3) {
						var width = popupDimentions[1];
						var height = popupDimentions[2];
					} else {
						var width = 0;
						var height = 0;
					}

				//--------------------------------------------------
				// Try to open the window

					if (width == 0 || height == 0) {
						var oWin = window.open (this.href);
					} else {
						var oWin = window.open (this.href, this.className, 'width=' + width + ', height=' + height + ', directories=no, menubar=no, resizable=yes, scrollbars=yes, status=yes, toolbar=no');
					}

				//--------------------------------------------------
				// Create a reference for the pop-up to access the
				// opener window - the try is done for Opera 8, which
				// believes this assignment is a security issue.

					try {
						oWin.opener = self;
					} catch (e) {
					}

				//--------------------------------------------------
				// If the pop-up was successfully created, then return
				// false (don't use normal href link), otherwise
				// return true (so the browser links as normal).

					if (oWin == null || typeof(oWin) == 'undefined') {
						return true;
					} else {
						oWin.focus();
						return false;
					}

			}

		//--------------------------------------------------
		// When the page has loaded, run the init function

			addLoadEvent (function() {
				popup.init();
			});

	}

