/* -------------------------------------------------------------------------------------------------------------
   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 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";

	// 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(" ", "");}
		}
		
		// tweak the passed in width & height depending on requested features - this is far from perfect
		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;

		// build the feature list with proper screenx and width
		if ((pwidth == 0) && (pheight == 0)) {
			// full screen popup
			width = screen.availWidth - deltawidth; height = screen.availHeight - deltaheight;
			screenx = 0; screeny = 0;
		} else {
			width = pwidth - deltawidth; height = pheight - deltaheight;
			// 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)) {
				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;
		}
		// if (!is_ie) {screeny -= 30;}
		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();}	// 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 = pwidth; popups[popupindex].height = pheight;
		}

	} 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;
}

/* -------------------------------------------------------------------------------------------------------------
   MENU: Code makes DIVS/SPAND safely visible/invisible. Used by the TopNav:Search:GeneralTopNavigationLevel2 and LeftNav:International:GeneralMapControl
   -------------------------------------------------------------------------------------------------------------
   This javascript is included by almost every template.
*/

var mold_spanid = "";
var mtimeoutid = 0;
var mcallback = "";

function span_change(pspanid, pon, pcallback) {
	var visibility_value = "";

	if (pcallback == null) { pcallback = ""; } // strongly type callback name

	// if a timer was already going (to close the hidden id), kill it
	if (mtimeoutid != 0) {	
		window.clearTimeout(mtimeoutid); // mouse event stops whatever timer was already going
		mtimeoutid = 0;
	}

	// if another menu is still showing, remove it immediatly (and then come back) - 1 level recursion
	if ((pon) && (mold_spanid != "")) {
		span_change(mold_spanid, false);
	}

	if (pon) {visibility_value = "visible";} else {visibility_value = "hidden";}
	window.document.getElementById(pspanid).style.visibility = visibility_value;
	
	/*
	if (!document.all && document.getElementById) {
		// Netscape 6 : it's worth noting the way you access SPANS/DIVS and the way you access images are TOTALLY different
		window.document.getElementById(pspanid).style.visibility = visibility_value;
	} else if (!document.all && document.layers) {
		// Netscape 4
		document.layers[pspanid].visibility = visibility_value;
	} else { // if (document.all) { 
		// IE 4+ (defaults to IE DOM implementation)
		document.all[pspanid].style.visibility = visibility_value;
	}
	*/

	if (pon) {
		mold_spanid = pspanid; // remember what's being turned on so it can be turned off
		mcallback = pcallback;
		if (mcallback != "") { eval( mcallback + "(true);"); }
	} else {
		mold_spanid = "";
		if (mcallback != "") { eval( mcallback + "(false);"); }
		mcallback = "";
	}	
		
}

function span_mouseover(pover) {	
	// start or restart a timer that will eventually close the open menu
	// if (mold_spanid == "") then someone has maually called span_change AGAIN to hide the DIV making the mouseover to close it unnecessary
	if (mold_spanid != "") {

		// the very first time the user mouseouts from the anchor/span that caused the span to display, there will be no timer to destroy
		if (mtimeoutid != 0) {		
			window.clearTimeout(mtimeoutid); // mouse event stops whatever timer was already going
			mtimeoutid = 0;
		}

		if (!pover) {
			// we're mouseout-ing, create a timer to hide the span
			mtimeoutid = window.setTimeout("span_change('" + mold_spanid + "', false);", 500);
		}
	}
}	

