﻿// Browser Window Size and Position
// copyright Stephen Chapman, 3rd Jan 2005, 8th Dec 2005
// you may copy these functions but please keep the copyright notice as well
function pageWidth()
{
	return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
}

function pageHeight()
{
	return window.innerHeight != null ? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null ? document.body.clientHeight : null;
}

function posLeft()
{
	return typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
}

function posTop()
{
	return typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
}

function posRight()
{
	return posLeft() + pageWidth();
}

function posBottom()
{
	return posTop() + pageHeight();
}

var popupName;
var popup;
var heading, sourceElement;

// adds the text defined by the "name" resource to the popup with id=OurPoup, and opens it next to the element with id=name.  It tries to open it within visible window boundaries.
function openPopup(name) {
	popupName = name;

//stop;

	popup = document.getElementById("OurPopup");
    if (popup == null)
        popup = document.getElementById("ctl00_OurPopup");
    if (popup == null)
        popup = document.getElementById("ctl00_ctl00_OurPopup");
    if (popup == null)
        return;
    popup.style.visibility = 'hidden';

	if (!name)
		return;

	sourceElement = document.getElementById(name);
	heading = sourceElement.innerHTML;

    var path;
    var index = sourceElement.href.indexOf('.aspx?');
    if (index != -1)
        path = sourceElement.href.substr(0, index) + "/" + sourceElement.href.substr(index + 6) + ".htm";
    else
	    path = "/definitions/" + heading + ".htm";
	str = "<p>Content unavailable: '" + path + "'!</p>"
	req = new XMLHttpRequest();
	req.open("GET", path, true);

	req.onreadystatechange = function () {
		if (req.readyState != 4)
			return;
		openPopup2(req);
	}

	req.send();
}

function openPopup2(req) {
	str = req.responseText;
	if (req.status != 200)
		str = "<p>Content not found.</p>"

	// we need to recurse up in the hierarchy in order to calculate the position offset from the root.
	var x = 0, y = 0, xbase = 0, ybase = 0;
	y = 4 + sourceElement.offsetHeight;
	var e = sourceElement;
	var sumBase = 0;
	do {
		// Since popup is absolute relative to page, we stop at page.
		// However, we keep track of the offset above this since the clippping clalculations below require the x + xbase value since that is window relative.
		if (e.className == "page")
		    sumBase = 1;

		x += e.offsetLeft;
		y += e.offsetTop;

		if (sumBase)
		{
			xbase += e.offsetLeft;
			ybase += e.offsetTop;
        }

        e = e.offsetParent;
	} while (e);

    var headingObject = document.getElementById("OurPopupHeading");
    if (headingObject == null)
        headingObject = document.getElementById("ctl00_OurPopupHeading");
    if (headingObject == null)
        headingObject = document.getElementById("ctl00_ctl00_OurPopupHeading");
    var contentObject = document.getElementById("OurPopupContent");
    if (contentObject == null)
        contentObject = document.getElementById("ctl00_OurPopupContent");
    if (contentObject == null)
        contentObject = document.getElementById("ctl00_ctl00_OurPopupContent");

    headingObject.innerHTML = heading;
    contentObject.innerHTML = str;

    //stop;

    popup.style.width = 500 + "px";
    if (popup.offsetHeight > 750 || str.indexOf("<img") != -1)
	    popup.style.width = 650 + "px";

	var windowRight = posRight();
	var windowLeft = posLeft();

	// if popup goes below the visible window, we pop it up at the top
	var windowBottom = posBottom();
	var windowTop = posTop();
	if (y + popup.offsetHeight > windowBottom) {
//alert("1: y=" + y + " h=" + popup.offsetHeight + " wb=" + windowBottom + " wt=" + windowTop);
		y -= popup.offsetHeight + (4 + sourceElement.offsetHeight) - 2;
	}
	var centered = 0;
	// if poup goes above the screen, then center it vertically on link
	if (y < windowTop) {
//alert("2: y=" + y + " h=" + popup.offsetHeight + " wb=" + windowBottom + " wt=" + windowTop);
		y += (popup.offsetHeight + sourceElement.offsetHeight) / 2;
		centered = 1;
	}
	// if popup still goes above or below the screen we pop it up with top being visible in top of window
	if (y < windowTop || (y + popup.offsetHeight > windowBottom > 150)) {
//alert("3: y=" + y + " h=" + popup.offsetHeight + " wb=" + windowBottom + " wt=" + windowTop);
		y = windowTop;
		centered = 1;
	}
	// if poup covers the link, try to move it either to the left or to the right so that the link is still visible and the poup is sill visible if possible.
	if (centered) {
		var right = windowRight - (x + sourceElement.offsetWidth + popup.offsetWidth);
		var left = (x - popup.offsetWidth) - windowLeft;

		if (right >= 0 || (left < 0 && right > left))
			x += sourceElement.offsetWidth;
		else
			x -= popup.offsetWidth;
	}
	//else {
		// make sure popup is within left/right boundaries
		if (x + popup.offsetWidth > windowRight)
			x = windowRight - popup.offsetWidth;
		if (x < windowLeft)
			x = windowLeft;
	//}

	// finally, set the poup position we calculated.
	popup.style.left = (x - xbase) + "px";
	popup.style.top = (y - ybase) + "px";

	// When we have name parameter, we are actually called by the link that is supposed to open this popup, so let's do it
	var ae = $find("PopupAE");
	if (ae == null)
	    ae = $find("ctl00_PopupAE");
	if (ae == null)
	    ae = $find("ctl00_ctl00_PopupAE");
	ae.OnClick();
	//popup.scrollIntoView();
}

function newWin() {
	return 1 ? false : true;
}

