/*
**
** IMPORTANT: this is the file that OPENS the popup, not a file for the popup itself.
** i.e., any page opening a popup must include this file, though the poped-up page itself doesn't have to.
**
*/


/*

Overview
========
Allows another webpage ("Child") to be displayed as a window within the current
web page ("Parent") in an IFrame.  Parent is grayed out (overlayed with #666 at
60% transparency), and Child is centered ala lightbox.  Clicking the grayed-out
Parent will cause the child window to close.  In addition, a close button
(calling [Parent].popupClose()) may also be implemented.



Invokation
==========
A popup can be displayed using the popup function.  This function takes 3
parameters: URL, width, height.  If width or height are omitted, default
values (970, 552) will be used.

e.g.
<a href="glossary.html" onclick="return popup(this.href);">Glossary</a>           <!-- recommended usage, crawler/ada compliant -->
<a href="glossary.html" onclick="return popup(this.href, 640, 480);">Glossary</a> <!-- recommended usage, crawler/ada compliant -->

<a href="javascript:void(popup('glossary.html'));">Glossary</a>
<a href="javascript:void(popup('glossary.html', 640, 480));">Glossary</a>



Assumptions
===========
- Parent may scroll vertically
- Parent will NOT scroll horizontally
- Child will NOT scroll either vertically or horizontally (see Design
  Considerations)
- Child is smaller (narrower and shorter) than the current viewport



Design Considerations
=====================
The IFrame has scrolling disabled, so Child must be designed to fit content
within that area.  If scrolling is required, it is up to Child to do so (i.e.
use a Div with a scroll bar, or something like that).

The IFrame has no border.  If a border is required, it is up to Child (i.e. a
use a background graphic with a border).

The IFrame has no drop-shadow.  Tell Creative to deal with it.



Prerequisites
=============
Parent must include /docroot/js/popup.js and /docroot/js/lib/jquery-1.3.2.js.
Child has no prerequisites.

It is recommended, but not required that Child call
"window.parent.popupOpenCallback()" onload.  This will enable bookmarking and
recentering.



Bookmarking
===========
When a popup is opened, the hash of Parent is updated to include the URL of
the popup, prefixed with "#popup-".  For example, if  /en/home.jsp  were to
open  /en/learning-center.html,  the address bar would change to read
/en/home.jsp#popup-/en/learning-center.html.

If Child navigates to a different page, Parent should notice and update the URL
again.  To continue the above example, if the user navigates from
/en/learning-center.html to http://www.google.com/, Parent's URL will change
from /en/home.jsp#popup-/en/learning-center.html to
/en/home.jsp#popup-http://www.google.com/.

When Parent first loads, if the URL contains #popup-, the popup will be opened
to that URL.  This means that if the user bookmarks
/en/home.jsp#popup-/en/learning-center.html and then revisits that page,
/en/home.jsp will open, and then /en/learning-center.html will be opened in the
popup (i.e. if the user bookmarks a page with an open popup, the bookmark
location is stored too and will also be restored).

If there is a more detailed state for Child which should be remembered, i.e.
which article is opened in the Learning Center popup, it is up to Child to
store that state in its own URL and deal with that when it gets reloaded.

NOTES:
1. bookmarking will only work after Child has called
   "window.parent.popupOpenCallback()".

2. fully qualified URLs (URLs containing ":") will NOT be restored onload
   (security... to prevent anyone from sending a link like
   http://homeloans.bankofamerica.com/en/home.jsp#popup-http://www.google.ca/search?q=Bank+of+America+sucks)

3. updating Parent's URL can take up to a second (frequency of
   [Parent].popupInterval() is 1000ms)



Recentering
===========
When initially opened, Child is centered both horizontally and vertically
within the current viewport.

If Parent is resized, Child will be immediately recentered.  This *may*
occur while Parent is being resized, or if not then immediately after mouse up
(browser dependent).

If Parent scrolls vertically, Child will be recentered automatically.  This is
done using a JQuery animation ("kinda bouncy").

Recentering will only work after Child has called
"window.parent.popupOpenCallback()".



Close Button
============
It is recommended that Child have a Close Button in the upper right.  This can
be done with an anchor which calls "window.parent.popupClose()".

e.g.
<a href="javascript:void(window.parent.popupClose())">close</a>

 */
var g_cpopupHostname = (window.location + "").substr(0, (window.location + "").indexOf(window.location.pathname));
var g_opopupContainer = null;
var g_opopupBackground = null;
var g_opopupWindow = null;
var g_opopupWindowBg = null;
var g_opopupCloser = null;
var g_opopupInterval = null;
var g_nfadeMs = 300;
var g_npopupHeight = 1;
var g_npopupWidth = 1;
var g_npopupLeft = 0;
var g_npopupTop = 0;



function popup(p_curl, p_nwidth, p_nheight) {
	/*
	 * example:
	 *
	 * 	<a href="glossary.html" onclick="return popup(this.href);">Glossary</a>
	 */

	//default values for parameters
	if (!p_curl) p_curl = "http://www.organic.com";
	if (!p_nwidth)  g_npopupWidth = 970; else g_npopupWidth = p_nwidth;
	if (!p_nheight) g_npopupHeight = 552; else g_npopupHeight = p_nheight;

	function createElement(p_ctype, p_oparent) {
		var oret = document.createElement(p_ctype);
		oret.style.position = "absolute";
		oret.style.top = oret.style.left = oret.style.margin = oret.style.padding = "0px";
		if (p_oparent)
			p_oparent.appendChild(oret);
		return oret;
	} //end inner function

	if (g_opopupContainer == null) {

		g_opopupContainer = createElement("span", document.body);
		g_opopupContainer.id = "g_opopupContainerId";
		g_opopupContainer.style.display = "none";
		g_opopupContainer.style.zIndex = "999";

		g_opopupBackground = createElement("span", g_opopupContainer);
		g_opopupBackground.style.display = "block";
		g_opopupBackground.style.backgroundColor = "#666";
		if ((navigator.appName + "").indexOf("Microsoft") >= 0) g_opopupBackground.style.filter = "alpha(opacity=60)";
		else g_opopupBackground.style.opacity = "0.60";

		g_opopupWindowBg = createElement("div");
		g_opopupWindowBg.id = "g_opopupWindowBgId";
		g_opopupWindowBg.border = 0;
		g_opopupWindowBg.frameBorder = 0;
		g_opopupWindowBg.style.backgroundColor = "white";
		//g_opopupWindowBg.style.backgroundImage = "url(" + contextPath + "/docroot/images/popup_background.gif)";
		g_opopupWindowBg.style.display = "block";
		g_opopupWindowBg.style.overflowX = "hidden";
		g_opopupWindowBg.scrolling = "no";
		g_opopupContainer.appendChild(g_opopupWindowBg);

		g_opopupCloser = createElement("a", g_opopupContainer);
		g_opopupCloser.href = "javascript:popupClose();";
		g_opopupCloser.title = "";
		g_opopupCloser.innerHTML = "&nbsp;";
		g_opopupCloser.style.color = "white";
		g_opopupCloser.style.textDecoration = "none";

		g_opopupWindow = createElement("iframe");
		g_opopupWindow.id = "g_opopupWindowId";
		g_opopupWindow.border = 0;
		g_opopupWindow.frameBorder = 0;
		g_opopupWindow.style.display = "block";
		g_opopupWindow.style.overflowX = "hidden";
		g_opopupWindow.scrolling = "no";
		g_opopupContainer.appendChild(g_opopupWindow);

		createElement("span", g_opopupContainer);
	} //end if

	g_opopupWindow.src = p_curl;
	popupResize();
	$("#g_opopupWindowId").css("display", "none");
	$("#g_opopupWindowBgId").css("display", "none");
	$("#g_opopupContainerId").css("opacity", 0.01).css("display", "block");

	$("#g_opopupContainerId").animate( { opacity:1 }, g_nfadeMs, "", function() {
		$("#g_opopupWindowBgId").css("display", "block").css("opacity", 0.01).animate( { opacity:1 }, g_nfadeMs );
		$("#g_opopupWindowId").css("display", "block").css("opacity", 0.01).animate( { opacity:1 }, g_nfadeMs );
	} );
	return false;
} //end function

function popupOutside(p_curl, p_nwidth, p_nheight) {
	// for learning center
	if(top != self) {
           window.parent.popup(p_curl, p_nwidth, p_nheight) ;
           return;
     } else {
     	window.popup(p_curl, p_nwidth, p_nheight) ;
        return;
     }
}

function popupClose() {
	$("#g_opopupWindowBgId").animate( { opacity:0.01 }, g_nfadeMs);
	$("#g_opopupWindowId").animate( { opacity:0.01 }, g_nfadeMs, "", function() {
		$("#g_opopupWindowId").css("display", "none");
		$("#g_opopupWindowBgId").css("display", "none");
		$("#g_opopupContainerId").animate( { opacity:0.01 }, g_nfadeMs, "", function() {
			var opage = document.getElementById("page");

			$("#g_opopupContainerId").css("display", "none");
			if (opage != null) {
				opage.style.height = "";
				//TN: this overflow setting causes flash player to restart swf
				//opage.style.overflow = "";
			} //end if

			//remove g_opopupContainerId from document.body
			document.body.removeChild(g_opopupContainer);
			g_opopupContainer = null;
		});
	});
	if (g_opopupInterval != null) {
		window.clearInterval(g_opopupInterval);
		g_opopupInterval = null;
		window.location.hash = "#closePopup";
	} //end if
} //end function



function popupOpenCallback() {
	if (g_opopupInterval == null)
		g_opopupInterval = window.setInterval(popupInterval, 1000);
} //end function



function popupInterval() {
	var opopupWindow = g_opopupWindow.contentWindow;
	var csrc = opopupWindow.location + "";
	var chash;
	var chashU;
	var chashCurr;
	var chashCurrU;

	if (csrc.length > g_cpopupHostname.length)
		if (csrc.substr(0, g_cpopupHostname.length) == g_cpopupHostname)
			csrc = csrc.substr(g_cpopupHostname.length);
	chash = "popup-" + escape(csrc);
	chashU = "popup-" + csrc;
	chashCurr = window.location.hash;
	chashCurrU = unescape(chashCurr);
	if (chash != chashCurr && "#" + chash != chashCurr &&
		chash != chashCurrU && "#" + chash != chashCurrU &&
		chashU != chashCurr && "#" + chashU != chashCurr
	)
		window.location.hash = chash;
} //end function



function popupResize() {
	var npageHeight;
	var npageWidth;
	var opage = document.getElementById("page");
	var nxoffset = (("" + navigator.userAgent).indexOf("MSIE") < 0) ? 24 : 0;
	var ndocHeight;
	var nscrollBarw;

	if (window.innerHeight) npageHeight = window.innerHeight;
	else if (document.documentElement) npageHeight = document.documentElement.clientHeight;
	else npageHeight = document.body.clientHeight;

	if (opage != null) {
		opage.style.height = npageHeight + "px";
		//TN: this overflow setting causes flash player to restart swf
		//opage.style.overflow = "hidden";
	} //end if

	if (window.innerWidth) npageWidth = window.innerWidth;
	else npageWidth = document.body.clientWidth;

	npageHeight = Math.max(npageHeight, g_npopupHeight);
	npageWidth = Math.max(npageWidth, g_npopupWidth);
	g_npopupTop = Math.max(0, (npageHeight - g_npopupHeight) >> 1);
	g_npopupLeft = Math.max(15, (npageWidth - g_npopupWidth - nxoffset) >> 1);

	if (window.scrollMaxY)
		if (window.scrollMaxY > 0) nscrollBarw = 24;
		else nscrollBarw = 0;
	else nscrollBarw = 0;

	g_opopupCloser.style.width =
	g_opopupContainer.style.width =
	g_opopupBackground.style.width =
		Math.max(document.body.scrollWidth - 1, npageWidth - nscrollBarw - 1, 16, 1000) + "px"

	ndocHeight = $(document).height();
	g_opopupCloser.style.height =
	g_opopupContainer.style.height =
	g_opopupBackground.style.height =
		Math.max(npageHeight, ndocHeight, document.body.scrollHeight - 1, document.body.clientHeight - 1, 16) + "px";

	g_opopupWindowBg.style.left =
	g_opopupWindow.style.left =
		g_npopupLeft + "px";

	$(g_opopupWindowBg).stop();
	$(g_opopupWindow).stop();
	g_opopupWindow.style.top =
	g_opopupWindowBg.style.top =
		($(document).scrollTop() + g_npopupTop) + "px";

	g_opopupWindow.style.height =
	g_opopupWindowBg.style.height =
		g_npopupHeight + "px";

	g_opopupWindow.style.width =
	g_opopupWindowBg.style.width =
		g_npopupWidth + "px";

} //end function



function popupInit() {
	var chash = window.location.hash;
	if (chash.length > 1)
		if (chash.charAt(0) == "#")
			chash = chash.substr(1);
	if (chash.length > 6)
		if (chash.substr(0, 6) == "popup-") {
			chash = unescape(chash.substr(6));
			if (chash.indexOf(':') < 0)
				popup(chash);
		} //end if
} //end function



$(document).bind("ready", function() {
	window.setTimeout(popupInit, 100);
});



$(window).bind("scroll", function() {
	var ntop;
	if (g_opopupInterval != null)
		if (g_npopupTop != 0 && g_npopupLeft >= 16) {
			$(g_opopupWindowBg).stop();
			$(g_opopupWindow).stop();
			ntop = $(document).scrollTop() + g_npopupTop;
			$(g_opopupWindowBg).animate({
		        top: ntop + "px"
			}, 500 );
			$(g_opopupWindow).animate({
		        top: ntop + "px"
			}, 500 );
		}
});



$(window).bind("resize", function() {
	if (g_opopupInterval != null) popupResize();
});

$(window).bind("focus", function() {
	if (g_opopupInterval != null) g_opopupWindow.contentWindow.focus();
});

$(document.body).bind("activate", function() {
	if (g_opopupInterval != null) g_opopupWindow.contentWindow.focus();
});