/*******************************************************************************************
 * popup
 * Written by Craig Francis
 * Look for links with a class like "popup400x200" and turn it into popup link where the
 * resulting window is at the specified width (e.g. 400px) and height 200px (any height)
 *******************************************************************************************/

//--------------------------------------------------
// Function called when a popup link is used

	function popupLaunch () {

		//--------------------------------------------------
		// 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 popup to access the
		// opener window.

			oWin.opener = self;

		//--------------------------------------------------
		// If the popup was sucessfully created, then return
		// false (dont 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 finished loading, then add the
// relevant JS for 'popup' links

	addLoadEvent (function() {
		if (document.getElementsByTagName) {

			//--------------------------------------------------
			// Return a reference to all links on the page

				var links = document.getElementsByTagName('a');
				var linksLength = links.length;

			//--------------------------------------------------
			// Regular expression to test popup 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' function.

				for (var i=0; i<linksLength; i++) {
					if (popupRegExp.test(links[i].className)) {
						links[i].onclick = popupLaunch;
					}
					if (cssjs('check', links[i], 'popup')) {
						links[i].onclick = popupLaunch;
					}
				}

		}
	});
