/***********************************************************************\
*                            POPUP SECTIONS                             *
\***********************************************************************/

if (!top.GLOBALFRAMES)	{ top.GLOBALFRAMES = new createGlobalFramesObject(); }

// ------------------------------------------------------------------- \\

function createGlobalFramesObject() {
	this.Expandos = 0;
	this.Expando = new Array();
	this.ExpandoPadding = "0px";
	this.ExpandoMargin = "2px";
	this.Popups = 0;
	this.Popup = new Array();
	this.PopupDepthLimit = 1;		// -1 = infinite nesting
}

// ------------------------------------------------------------------- \\



// ----- user numeric array indexes to make array code compatible with earlier browsers. ----- \\
// ----- probably wasted effort, since some part of the DOM is bound to be incompatible. ----- \\
POPUP_OBJ	= 0;
CONTAIN_OBJ	= 1;
SHADOW_OBJ	= 2;
CONTENT_OBJ	= 3;
POPUP_IDX	= 4;
POPUP_VISIBLE	= 5;

if ((window.name.substring(0,7) != "popup__") && (window.name.substring(0,9) != "_expando_") && (window.name != "HiddenPopup")) {
	top.globalPopupList = null;
}

function MarkPopup () {
	top.globalMouseX = event.x;
	top.globalMouseY = event.y;
}

function Popup ( targetURL ) {
	if (window.feedback_push) { feedback_push( "POP", targetURL ); }

	var fixedURL = FixURL(targetURL);
	if (fixedURL) { MakePopup(fixedURL); }
}

function FixURL( targetURL ) {
	myloc = self.location.toString();
	if (myloc.indexOf("::")>=0) {
	// ----- This is a CHM file ----- \\
		if (targetURL.indexOf("::")>=0) {
		// ----- You are calling a popup in a CHM file ----- \\
			t1 = myloc.substring(0,myloc.indexOf("::"));
			t2 = t1.substring(0,t1.lastIndexOf("\\")+1);
			newURL = t2 + targetURL;
		} else {
		// ----- Just a regular popup call ----- \\
			newURL = targetURL;
		}
	} else {
		// ----- this is a file on the hard drive ----- \\
		if (window.TOPIC_UPDATEPATH) {
			// ----- This is a patched file ----- \\
			TOPIC_EXTPATH = TOPIC_EXTPATH.replace(/\//g,"\\");
			if (targetURL.indexOf("::")>=0) {
				// ----- You already know the name of the CHM file ----- \\
				newURL = ("ms-its:" + TOPIC_EXTPATH + targetURL);
			} else {
				// ----- find the subfolder and make sure that "chmFile" is defined ----- \\
				try {
					t = myloc.substr(myloc.indexOf(TOPIC_UPDATEPATH)+TOPIC_UPDATEPATH.length,myloc.length);
					t = t.substr(0,t.lastIndexOf("/")+1);
					newURL = ("ms-its:" + TOPIC_EXTPATH + CHMFILE + t + targetURL);
				}
				catch (e) {
					newURL = "";
				}
			}
		} else {
			// ----- don't know anything about the environment ----- \\
			newURL = targetURL;
		}
	}
	return newURL;
}

function MakePopup ( targetURL ) {
	if (top.GLOBALFRAMES.PopupDepthLimit == 0) {	open(targetURL,"_self"); return; }
	if (top.globalPopupList == null) { top.globalPopupList = new Array(); }

	var hPopup = null;
	var tLen = top.globalPopupList.length;
	if (top.GLOBALFRAMES.PopupDepthLimit == tLen) {
		top.globalPopupList[tLen-1][POPUP_OBJ].location = targetURL;
		hPopup = top.globalPopupList[tLen-1][POPUP_OBJ];
		FinishPopup(tLen-1);
	} else {
		for (var i=0; i<top.globalPopupList.length; i++) {
			// ----- the window is already defined; maybe it's empty ----- \\
			if (!top.globalPopupList[i][POPUP_VISIBLE]) {
				top.globalPopupList[i][POPUP_OBJ].location = targetURL;
				hPopup = top.globalPopupList[i][POPUP_OBJ];
				FinishPopup(i);
				i = top.globalPopupList.length+1;
			}
		}
	}
	if (hPopup == null) {
		if (top.globalPopupList.length == 0) {
			document.body.onclick = ParentClosePopups;
//			PETE:	Why is this code even here? If there's no "globalPopupList",
//				then there are no _expando_ frames. Seems simple. Test.
//			for (var j=1; j <= top.GLOBAL_EXPANDO_FRAMES; j++) {
//				document.frames["_expando_"+j].document.body.onclick = ParentClosePopups;
//			}
		}
		hPopup = CreatePopup(targetURL);
	}
}

function CreatePopup ( targetURL ) {
	// ----- Need to create a new Popup Window ------ \\

	var idx = top.globalPopupList.length;

	var t = "";
	t += "<div id=\"contain"+idx+"\" style=\"visibility:hidden;\">";
	t += "<div id=\"shadow_"+idx+"\" style=";
	t +=   "\"position:absolute; top:0; left:0; background-color:#C0C0C0;\"></div>";
	t += "<div id=\"content"+idx+"\" style=";
	t +=   "\"position:absolute; top:0; left:0; background-color:#FFFFFF;";
	t +=   " border:1px #000000 outset;\">";
	t += "<iframe name=\"popup__"+idx+"\" src=\"" + targetURL + "\" frameborder=\"0\" scrolling=\"auto\"></iframe>";
	t += "</div></div>";
	document.body.insertAdjacentHTML("beforeEnd", t);

	var popupObjArray = new Array();
	popupObjArray[POPUP_OBJ] = eval("popup__"+idx);
	popupObjArray[CONTAIN_OBJ] = eval("contain"+idx);
	popupObjArray[SHADOW_OBJ] = eval("shadow_"+idx);
	popupObjArray[CONTENT_OBJ] = eval("content"+idx);
	popupObjArray[POPUP_IDX] = idx;
	popupObjArray[POPUP_VISIBLE] = false;

	top.globalPopupList[idx] = popupObjArray;

	popupObjArray[POPUP_OBJ].document.location = targetURL;
	FinishPopup(idx);

	return popupObjArray[POPUP_OBJ];
}

function FinishPopup ( idx ) {
	popupObj = top.globalPopupList[idx][POPUP_OBJ];
	if ( (popupObj.document.readyState == "complete") &&
	     (popupObj.document.body != null) ) {
		// ----- page is finished loading; polish it off ----- \\

		fixPopupTopicLinks(popupObj.document);
//		popupObj.document.body.onclick = ClosePopups;
		popupObj.document.onclick = ClosePopups;

		ResizePopup(idx);

		top.globalPopupList[idx][CONTAIN_OBJ].style.visibility = "visible";
		top.globalPopupList[idx][POPUP_VISIBLE] = true;

		MovePopup(idx);

	} else {
		setTimeout("FinishPopup("+idx+")", 1000);
	}
}

function MovePopup ( idx ) {
	popupObj = top.globalPopupList[idx][POPUP_OBJ];
	contentObj = top.globalPopupList[idx][CONTENT_OBJ];
	shadowObj = top.globalPopupList[idx][SHADOW_OBJ];

	var nWidth = popupObj.document.body.offsetWidth;
	var nHeight = popupObj.document.body.offsetHeight;
	var parentWidth = document.body.clientWidth - 6;
	var parentHeight = document.body.clientHeight - 16;

	// ----- where would we like the popup? Centered beneath the mouse ----- \\
	var nLeft = top.globalMouseX - (nWidth/2);
	var nTop = top.globalMouseY + document.body.scrollTop;

	// ----- now bring it in to fit in the parent window properly ----- \\
	if ( (top.globalMouseX + (nWidth/2)) > parentWidth ) {
		nLeft = parentWidth - nWidth;
	}
	if ( top.globalMouseX < (nWidth/2) ) {
		nLeft = 2;
	}
	if ( (top.globalMouseY + nHeight ) > parentHeight ) {
		nTop = parentHeight - nHeight + document.body.scrollTop;
	}
	if ( nTop < 0 ) { nTop = 0; }

	contentObj.style.left = nLeft;
	contentObj.style.top = nTop;

	// ----- update shadow info ----- \\
	shadowObj.style.width = contentObj.style.width;
	shadowObj.style.height = contentObj.style.height;
	shadowObj.style.left = nLeft + 6;
	shadowObj.style.top = nTop + 6;
}

function ResizePopup ( idx ) {
   // ----- Error Trap this whole routine ----- \\
   try {
	popupObj = top.globalPopupList[idx][POPUP_OBJ];

	parentWidth = document.body.clientWidth;
	parentHeight = document.body.clientHeight;
	maxWidthRatio = 0.8;
	maxHeightRatio = 0.8;
	maxWidth = parentWidth * maxWidthRatio;
	maxHeight = parentHeight * maxHeightRatio;
	parentSlope = parentHeight/parentWidth;
	idealSlope = 0.65;

	// ----- start popup size at the max size, constrained by the ideal ratio ----- \\
	var popX = maxWidth;
	var popY = maxHeight;
	if (parentSlope > idealSlope) {
		popY = popX * idealSlope;
	} else {
		popX = popY / idealSlope;
	}

	// ----- test shrinking the Y to the minimum and see where that gets us ----- \\
	// ----- (resize once to SET the scrollHeight, once to USE the scrollHeight) ----- \\
	popupObj.resizeTo(popX, popupObj.document.body.scrollHeight);
	popupObj.resizeTo(popX, popupObj.document.body.scrollHeight);
	var minY = popupObj.document.body.scrollHeight;

	if (minY > popY) {
		// ----- content doesn't fit the maximum "ideal ratio" window, ----- \\
		// ----- so let's dissolve ideal ratio constraint and try again ----- \\
		popupObj.resizeTo(maxWidth, popupObj.document.body.scrollHeight);
		popupObj.resizeTo(maxWidth, popupObj.document.body.scrollHeight);
		var minY = popupObj.document.body.scrollHeight;

		if (maxHeight > minY) {
			// ----- there's still room to maneuver; ----- \\
			// ----- let's try the parent ratio ----- \\
			popupObj.resizeTo(maxWidth, maxHeight);
//			qsortShrinkPopup( popupObj, 0, 0 );

			// ----- since we know scroll bars are unnecessary, turn them off ----- \\
			// ----- in case another popup DIV is added to the bottom of it ----- \\
			popupObj.document.body.scroll = 'no';
		} else {
			// ----- scroll bars are inevitable; make the window fit ----- \\
			popupObj.resizeTo(maxWidth, maxHeight);
		}
	} else {
		// ----- content does fit within the "ideal ratio", ----- \\
		// ----- now how small can we make it before scroll bars appear ----- \\
		// ----- first, reset it to fit the ideal ratio once again ----- \\
		popupObj.resizeTo(popX,popY);
//		qsortShrinkPopup ( popupObj, 0, 0 );

		// ----- since we know scroll bars are unnecessary, turn them off ----- \\
		// ----- in case another popup DIV is added to the bottom of it ----- \\
		popupObj.document.body.scroll = 'no';
	}

   }
   catch (e) {
	// ----- do nothing about it now ----- \\
//	alert("ResizePopup error.");
//	alert(e.name + ": "+e.message);
   }
}

function qsortShrinkPopup ( popupObj, constrainX, constrainY ) {
	// ----- this function will shrink the popupObj window towards ----- \\
	// ----- (constrainX, constrainY) using a binary qsort search ----- \\
	// ----- if called initially with 0,0 as the constraint, then ----- \\
	// ----- the aspect ratio of the window will be preserved. ----- \\

	var nowX = popupObj.document.body.clientWidth;
	var nowY = popupObj.document.body.clientHeight;

	var tryX = nowX - ((nowX - constrainX) / 2);
	var tryY = nowY - ((nowY - constrainY) / 2);
	if ( (Math.abs(nowX - constrainX) < 16) || (Math.abs(nowY - constrainY) < 16) ) {
		// ----- too small a difference; time to escape ----- \\
		return;
	}

	popupObj.resizeTo(tryX, tryY);

	if (popupObj.document.body.scrollHeight > popupObj.document.body.clientHeight) {
		// ----- (tryX,tryY) is too small; reset window and make "tryX/Y" the constraint ----- \\
		popupObj.resizeTo(nowX, nowY);
		qsortShrinkPopup( popupObj, tryX, tryY );
	} else {
		// ----- (tryX, tryY) is still legal; keep going ----- \\
		qsortShrinkPopup( popupObj, constrainX, constrainY );
	}
}


function fixPopupTopicLinks ( targetContainer ) {
	var anchors = targetContainer.all.tags("A");
	for (var i=0; i<anchors.length; i++) {
		var t = anchors[i].href.toLowerCase();
		if (t.indexOf("javascript:") == -1) {
			if (anchors[i].target == "") {
				anchors[i].target = "_parent";
			}
		}
	}
}

function ClosePopups() {
	subPops = top.globalPopupList.length + 1;
	for (var i=0; i<top.globalPopupList.length; i++) {
		var hPopup = top.globalPopupList[i][POPUP_OBJ];
		if ((hPopup.event != null) && (hPopup.event.type == "click")) {
			// ----- Found the popup with the event in it ----- \\

			var myElement = hPopup.event.srcElement;
			while ((myElement.tagName != "BODY") && (myElement.tagName != "A") && (myElement.parentElement)) {
				myElement = myElement.parentElement;
			}

			if (myElement.tagName != "A") {
				// ----- If it's not a link, vanish the current window ----- \\
				top.globalPopupList[i][CONTAIN_OBJ].style.visibility = "hidden";
				top.globalPopupList[i][POPUP_VISIBLE] = false;
				subPops = i+1;
			} else {
				// ----- Otherwise, vanish a subwindow ----- \\
				if (i+1 < top.globalPopupList.length) {
					top.globalPopupList[i+1][CONTAIN_OBJ].style.visibility = "hidden";
					top.globalPopupList[i+1][POPUP_VISIBLE] = false;
					subPops = i+2;
				}
			}
			// ----- any sub-(sub-)popups can be eliminated; their objects will die ----- \\

			if (subPops<top.globalPopupList.length) {
				top.globalPopupList.length = subPops;
			}

			// ----- end loop early ----- \\
			i = top.globalPopupList.length;
		}
	}
}

function ParentClosePopups() {
	if ( top.globalPopupList.length > 0 ) {
		// ----- Vanish the primary popup window and truncate popup object list ----- \\
		top.globalPopupList[0][CONTAIN_OBJ].style.visibility = "hidden";
		top.globalPopupList[0][POPUP_VISIBLE] = false;
		top.globalPopupList.length = 1;
	}
}
