//defaultProxy should be the production URL for the proxy so that if all else fails, it will work in production
var defaultProxy = "https://www.aps.com/main/account/MngAcct/resize.html";
var previousHeight = 0;
var currentHeight = 0;

/************************************************************************
 * Continuously polls the page for height changes.
 ***********************************************************************/
function initialize()
{
	window.setInterval(check, 100); // time in MS
}

/************************************************************************
 * Checks the height, and, if changed, resizes the IFrame.
 ***********************************************************************/
function check()
{
	currentHeight = getBodyHeight().toString();
	if (previousHeight == currentHeight) 
	{
		return;
	}
	else
	{
		resizeMe();
		previousHeight = currentHeight;
	}
}

/************************************************************************
 * Posts a "resize me" message to the top level window in the browser, 
 * either through the HTML-5 based "postMessage" interface for cross-
 * domain messaging, or through a hidden IFrame on the same domain as 
 * the top level window. This posts a "resize me" message.
 ***********************************************************************/
function resizeMe() 
{
	var proxyURL = getProxyUrl();
	
	// If we are using Opera Browser, or other browsers
	// that support the HTML 5 "postMessage" interface
	// for cross-domain interfacing, use it.  Otherwise
	// use the older-hack-proxy.
	if (document.postMessage)
	{
		top.window.document.postMessage(height);
	}
	else
	{
		// Remove any existing resizing proxy iframe
		var tempIFrame=document.getElementById('resizeFrame');
		if (tempIFrame != null)
		{
			document.body.removeChild(tempIFrame);
		}

		// Add the new one
		tempIFrame=document.createElement('iframe');
		tempIFrame.setAttribute('id','resizeFrame');
		tempIFrame.src = proxyURL + "#" + currentHeight;
		tempIFrame.style.visibility='hidden';
		tempIFrame.style.border='0px';
		tempIFrame.style.width='0px';
		tempIFrame.style.height='0px';
			
		document.body.appendChild(tempIFrame);
    }
}

/************************************************************************
 * Returns the vertical height of the body of this document.
 ***********************************************************************/
function getBodyHeight() 
{
	/***************************************************************************
	* There are MANY inconsistencies among browsers here
	* For more info, take a look at http://www.quirksmode.org/dom/w3c_cssom.html
	* One known issue with the current solution is that FireFox never contracts; 
	* height remains equal to the tallest page visited
	****************************************************************************/
	
	if (document.body.scrollHeight
		&& (document.body.scrollHeight != 0))
	{
		return document.body.scrollHeight;
	}

	return document.documentElement.offsetHeight;
}

/************************************************************************
 * Parses the proxy url from the given URL, which should be indicated
 * via the "#proxy=" hash string.  If not found on the url hash, we
 * check the cookie.  If not found in the cookie, we use the hard-coded
 * default.
 ***********************************************************************/
function getProxyUrl()
{
	var url = unescape(window.location.href);
	var identifier = "#proxy=";
	var indexOfIdentifier = url.indexOf(identifier);
	if (indexOfIdentifier != -1)
	{
		var proxy = url.substring(indexOfIdentifier + identifier.length);
		saveProxyUrl("APSProxy", proxy);
		return proxy;
	}
	else
	{
		// Check for the proxy cookie
		var proxy = getCookie("APSProxy");
		if (proxy.length > 0)
		{
		
			return proxy;
		}
	}

	// Return default only after checking the parameter and cookie versions (last resort).
	return defaultProxy;
}

/************************************************************************
 * Saves the proxy url to a cookie.  An attempt is made to save the cookie
 * to the parent domain, if possible.  Otherwise, the fully qualified
 * subdomain is used.  This cookie is used so we do not have to re-attach
 * the "#proxy=" hash to each link/form action in the pages linked to
 * from this one.
 ***********************************************************************/
function saveProxyUrl(name, value)
{
	// Cookie format: name=value;expiration;domain;path;secure
	document.cookie = name + "=" + escape(value) + ";;domain=" + getDomain() + ";path=/";
}

/************************************************************************
 * Gets the cookie value with the given name.
 ***********************************************************************/
function getCookie(name) 
{
	var search = name + "=";
	var returnvalue = "";

	if (document.cookie.length > 0) 
	{
		offset = document.cookie.indexOf(search);
    
		// if cookie exists
    	if (offset != -1) 
		{ 
      			offset += search.length;

      			// set index of beginning of value
      			end = document.cookie.indexOf(";", offset);

      			// set index of end of cookie value
      			if (end == -1)
			{ 
				end = document.cookie.length;
			}
      
			returnvalue = unescape(document.cookie.substring(offset, end));
      	}
   	}
  	return returnvalue;
}

/************************************************************************
 * Attempts to retrieve the parent domain, if possible, otherwise returns
 * the empty string, which represents the current fully-qualified domain.
 ***********************************************************************/
function getDomain()
{
	var domain = window.location.hostname;
	if (domain != null || domain.length() > 0)
	{
		var domainPieces = domain.split('.');
		if (domainPieces.length >= 2)
		{
			return "." + domainPieces[domainPieces.length - 2] + "." + domainPieces[domainPieces.length - 1];
		}
	}

	// Return empty string for the fully qualified domain
	return "";
}

/************************************************************************
 * Adds a on-load event to the body document, while still preserving
 * any original function that was set to execute on the body on-load event.
 ***********************************************************************/
function addLoadEvent(func) 
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function') 
	{
		window.onload = func;
	} 
  	else 
  	{
		window.onload = function() 
		{
	  		if (oldonload) 
	  		{
				oldonload();
	  		}
	  		func();
		}
  	}
}

// Resize the top level window for this content
addLoadEvent(initialize);