/* -------------------------------------------------------------------------------------------------------------
   POPUP.JS
   -------------------------------------------------------------------------------------------------------------
	Description		:	All of the benefits of window.open with none of the drawbacks
	                  Specifically, if someone opens the same popup twice, things still work.
	Created by		:	Jim McGinley
	Last Modified	:	June 03, 2002 by Jim McGinley
	Last Fix       :  screenX of live window cannot be referenced if it's in a different domain
	Code found in	:	\\Webdev\WebDevRoot\2002\INTERNAL_PROJECTS\shared_code\javascript_popup
	Code tested in	:	PC  IE 4.72.3612.1713, 5.50.4134.0600, Netscape 4.08
							MAC IE 4.01, 5, Netscape 4.06
	Notes				:	Does NOT work in Netscape 3.X (Script SRC= not supported)
	Usage          :  <SCRIPT SRC = javascript_popup.js>
				         void openWin("jimdaily", "http://www.dailyradar.com", 400, 300, false)

	COMING IMPROVEMENTS
	1. If a popup is closed, and re-opened, it should re-appear in the same spot as the closed popup
	   To achieve this, keep meta array elements even after popup is closed.
		When popup closes, it needs to alert me to what its position was.
		Perhaps a timer to keep patrolling all open popups (if domains have not changed)
	2. Allow left, top, width and height to be specified as a percentage
								
	KNOWN BUGS
	1. Width & Height (in full screen mode too) are not accurate when "menubar, toolbar, location and directories" features are all yes.
	   i.e. "menubar=yes,toolbar=yes,location=yes,directories=yes" 
	2. In some cases, depending on the features, full screen will yield a popup that is vertically too big.
	   In other cases, depending on the features, full screen will not be wide enough.
	3. In MAC IE4, it does not center the popup. Nor does it allow you to position it.
	
	REMEMBER
	1. fullscreen=1; also seemed to do the trick (in an unrelated experiment)
		type=fullWindow,fullscreen is how you get netscape and ie to do a real fullscreen.
	   however, it ignores ALL your other settings i.e. you will get no menubar regardless of whether you want one.
		note: alt+f4 closes the window
	2. "1,2 , 3 , 4 ,5".split(\s*,\s*);	// Returns ["1,", "2", "3", "4", "5"]
	3. if then shortcut : words.is_required = (planguage == 1) ? " obligatoire.\n" : " is required.\n";
	4. window.open with NO FEATURES specified turns EVERYTHING on (scrollbars, resizeable etc.)
	   As soon as you specify 1 of the features (left, width, height), they all default to off.

	// determine whether the user wanted scrollbars or not
	if (typeof(pscrollbars) == "boolean") {
		scrollbars = pscrollbars;
	} else {
		hold = pscrollbars.toString().toLowerCase();
		if ((hold == "1") || (hold == "yes") || (hold == "scrollbars")) {scrollbars = true;}
	}
*/

var popups = new Array;

function popup(pname, pURL, pwidth, pheight, pfeature, pscreenx, pscreeny) {
//Pops up a window. If window is already popped up, it just sets focus to it.
	// 1. pname 	-> giving 2 popups the same name (including "") causes them to overwrite each other.
	//					-> giving 2 popups different names will cause 2 different popups
	// 2. pURL		-> URL you want to popup, relative URLS work.
	// 3. pwidth 4. pheight	-> Width of popup. Height of popup. Passing in 0, 0 causes a full-screen popup
	// 5. pfeature -> feature to override default feature.
	// 6. pscreenx 7. pscreeny -> Position of popup. If not passed in, centers the popup.
	// returns     -> pointer to window that was opened
	// Usage: openWin("jimdaily", "http://www.dailyradar.com", 400, 300, "scrollbars=yes", 100, 200)

	var newwidth=0, newheight=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";

	// search through the list of open popups to see if it's already open
	for (i = 0; i < popups.length ; i++) {
		if ((popups[i].window != null) && (!popups[i].window.closed)) {
			if (popups[i].name == pname) {
				// find current position of popup (in case we need to close and repopen popup)
				if (popups[i].url.substring(0, 7) != "http://") {
					// if we're on the same domain, re-use the CURRENT position of the pop-up window (not the saved position)
					// since user may have moved popup
					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 {
					// if we're in a different domain (indicated by the URL having an absolute as opposed to relative address),
					// can't even look at .screenX or .screenLeft, so default to memorized screenX and screenY
					screenx = popups[i].screenx;
					screeny = popups[i].screeny;				
				}
				if ((popups[i].width != pwidth) || (popups[i].height != pheight)) {
					// window exists, but it's a different width and height so can't resuse it.
					popups[i].window.close();
				} else {
					popupindex = i; // re-use the window
				}
			}
			if (screenx == 0) {screenx = popups[i].screenx + 25; screeny = popups[i].screeny + 25;} // cascade multiple open popups
		}
	}

	// in IE4, it's a security violation to touch a popup after it's been open (and points to a URL on a different site)
	// if all popups are on the same site as the page that called them, this code can be removed.
	if ((popupindex != -1) && (is_ie4)) {
		popups[popupindex].window.close(); popupindex = -1;
	}
	
	if (popupindex == -1) {
		// the requested window is NOT already open. open it, and add it to the list of popups.
		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(" ", "");}
		}

		// build the feature list with proper screenx and width
		if ((pwidth == 0) && (pheight == 0)) {
			// full screen popup
			newwidth = screen.availWidth; newheight = screen.availHeight;
			screenx = 0; screeny = 0;
		} else {
			newwidth = pwidth; newheight = pheight;
			// center the new window on the screen
			if ((typeof(pscreenx) == "number") && (typeof(pscreeny) == "number") && (pscreenx != 0) && (pscreeny != 0)) {
				screenx = pscreenx; screeny = pscreeny;
			} else if ((screenx == 0) && (screeny == 0)) {
				screenx = parseInt((screen.availWidth - newwidth) / 2); screeny = parseInt((screen.availHeight - newheight) / 2);
			}
			if (screenx < 0) screenx = 0; if (screeny < 0) screeny = 0;
		}
		feature += ",left=" + screenx + ",screenX=" + screenx + ",top=" + screeny + ",screenY=" + screeny + ",width=" + newwidth + ",height=" + newheight;

		newwindow = window.open(pURL, pname, feature);
		newwindow.focus();
		if (newwindow != null) {
			if (!((is_ie) && (version <= 4))) {newwindow.focus();}	// security violation in IE4 if URL is points to a different site

			// memorize the passed-in parameters for later reference
			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 = newwidth; popups[popupindex].height = newheight;
		}

	} else {
		// it's already open, so reuse the popup that's already out there (browser will NOT let us override feature, only URL).
		if (popups[popupindex].url != pURL) {	// are we changing URLs in currently open window?
		 	popups[popupindex].window.location = pURL;
			popups[popupindex].url = pURL;
		}
		popups[popupindex].window.focus();
	}

	return popups[popupindex].window;
}
