/* POPUP.JS : <SCRIPT SRC = popup.js>
   comments removed to decrease load time.
	commented code avaiable from www.wideframe.com.
*/

var popups = new Array;

function popup(pname, pURL, pwidth, pheight, pfeature, pscreenx, pscreeny) {
	var width = 0, height = 0;
	var deltawidth= 0, deltaheight = 0;
	var screenx = 0, screeny = 0;
	var i = 0, popupindex = -1, newwindow = null;

	var is_ie  = ((navigator.appVersion.toLowerCase().indexOf('msie')) != -1);
	var is_mac = (navigator.userAgent.toLowerCase().indexOf("mac") != -1);																										
	var version = parseFloat(navigator.appVersion);
	var is_ie4  = ((is_ie) && (version == 4) && (navigator.userAgent.toLowerCase().indexOf("msie 4")!=-1));
	
	var feature = "scrollbars=no,menubar=no,toolbar=no,location=no,directories=no,status=no,resizable=no";

	for (i = 0; i < popups.length ; i++) {
		if ((popups[i].window != null) && (!popups[i].window.closed)) {
			if (popups[i].name == pname) {
				if (popups[i].url.substring(0, 7) != "http://") {
					screenx = (popups[i].window.screenX) ? popups[i].window.screenX : popups[i].window.screenLeft;
					screeny = (popups[i].window.screenY) ? popups[i].window.screenY : popups[i].window.screenTop - 22;
				} else {
					screenx = popups[i].screenx;
					screeny = popups[i].screeny;				
				}
				if ((popups[i].width != pwidth) || (popups[i].height != pheight)) {
					popups[i].window.close();
				} else {
					popupindex = i;
				}
			}
			if (screenx == 0) {screenx = popups[i].screenx + 25; screeny = popups[i].screeny + 25;}
		}
	}

	if ((popupindex != -1) && (is_ie4)) {
		popups[popupindex].window.close(); popupindex = -1;
	}
	
	if (popupindex == -1) {
		popupindex = popups.length;

		// configure the feature of the window (including scrollbars)
		if (pfeature) {
			if (typeof(pfeature) != "string") {alert("BUG:Popup.js was passed an invalid feature. It MUST be a string."); return false;}
			if (pfeature.toString() != "") {feature = pfeature.toString().replace(" ", "");}
		}
		
		deltawidth = 10;
		deltawidth += (((feature.search("resizable=yes")) != -1) && (is_ie)) ? 2 : 0;
		
		deltaheight = 29;
		deltaheight += (((feature.search("resizable=yes")) != -1) && (is_ie)) ? 2 : 0;
		deltaheight += (((feature.search("menubar=yes")) != -1) && (is_ie)) ? 48 : 0;
		deltaheight += (((feature.search("status=yes")) != -1) && (is_ie)) ? 20 : 0;

		if ((pwidth == 0) && (pheight == 0)) {
			width = screen.availWidth - deltawidth; height = screen.availHeight - deltaheight;
			screenx = 0; screeny = 0;
		} else {
			width = pwidth - deltawidth; height = pheight - deltaheight;
			if ((typeof(pscreenx) == "number") && (typeof(pscreeny) == "number") && (pscreenx != 0) && (pscreeny != 0)) {
				screenx = pscreenx; screeny = pscreeny;
			} else if ((screenx == 0) && (screeny == 0)) {
				if ((is_ie) && (is_mac)) {pwidth -= 15;}
				screenx = parseInt((screen.availWidth - (pwidth + deltawidth)) / 2); screeny = parseInt((screen.availHeight - (pheight + deltaheight)) / 2);
			}
			if (screenx < 0) screenx = 0; if (screeny < 0) screeny = 0;
		}
		feature += ",left=" + screenx + ",screenX=" + screenx + ",top=" + screeny + ",screenY=" + screeny + ",width=" + width + ",height=" + height;

		newwindow = window.open(pURL, pname, feature);
		newwindow.focus();
		if (newwindow != null) {
			if (!((is_ie) && (version <= 4))) {newwindow.focus();}
			
			popups[popupindex] = new Object();
			popups[popupindex].window = newwindow;
			popups[popupindex].name = pname;
			popups[popupindex].url = pURL;
			popups[popupindex].screenx = screenx; popups[popupindex].screeny = screeny;
			popups[popupindex].width = pwidth; popups[popupindex].height = pheight;
		}

	} else {
		if (popups[popupindex].url != pURL) {
		 	popups[popupindex].window.location = pURL;
			popups[popupindex].url = pURL;
		}
		popups[popupindex].window.focus();
	}

	return popups[popupindex].window;
}
