function FrameSetNav (sBodyFrame, sFrameName)
{
	window.GetNavURI = FrameSetNav_GetNavURI;
	window.GetNavURIPage = FrameSetNav_GetNavURIPage;
	window.GoToNavURI = FrameSetNav_GoToNavURI;
	window.GoToNavURIPage = FrameSetNav_GoToNavURIPage;
	window.GetMainNavURIPage = FrameSetNav_GetMainNavURIPage;
	window.GoToMainNavURIPage = FrameSetNav_GoToMainNavURIPage;
	
	window._sBodyFrame = sBodyFrame.toLowerCase();
	window._sFrameName = sFrameName.toLowerCase();
	window.onload = FrameSetNav_FrameSetPageLoader;
	window._sLoadedWindow = null;
}

function FrameSetNav_FrameSetPageLoader()
{
	var sNamedArgs = window.location.search.substr(1);
	if (sNamedArgs.length != null && sNamedArgs.length > 0)
		{
		var sNewLocation = "";
		var asNamedArgsNoBodyArg = new Array();
		var asNamedArgs = sNamedArgs.split(/&/);
		for (var nNamedArg = 0; nNamedArg < asNamedArgs.length; nNamedArg++)
			{
			var sNamedArg = asNamedArgs[nNamedArg];
			var asParamAndArg = sNamedArg.split(/=/);
			if (asParamAndArg[0].toLowerCase() == window._sBodyFrame)
				sNewLocation = asParamAndArg[1];
			else
				asNamedArgsNoBodyArg.push(sNamedArg)
			}
		if (sNewLocation.length > 0)
			{
			var sNamedArgsNoBodyArg = asNamedArgsNoBodyArg.join("&");
			var sPath = window.location.pathname;
			var sNewPath = sPath.substr(0, sPath.lastIndexOf("/") + 1);
			var sNewURI = sNewPath + sNewLocation;
			if (sNamedArgsNoBodyArg.length > 0)
				sNewURI += "?" + sNamedArgsNoBodyArg;
			var winBody = window.frames[window._sBodyFrame];
			winBody.location = sNewURI;
			window._sLoadedWindow = FrameSetNav_CanonicalizeURI(sNewLocation);
			if (sNamedArgsNoBodyArg.length > 0)
				window._sLoadedWindow += "?" + sNamedArgsNoBodyArg;
			}
		}
}

function FrameSetNav_CanonicalizeURI(sURI)
{
	var sFramePath = window.location.pathname.substr(0, window.location.pathname.lastIndexOf("/"));
	var sURIPath = sURI;
	while (sURIPath.substr(0, 3) == "../")
		{
		sURIPath = sURIPath.substr(3);
		sFramePath = sFramePath.substr(0, sFramePath.lastIndexOf("/"));
		}
	return sFramePath + "/" + sURIPath;
}

function FrameSetNav_GetNavURIPage(page)
{
	var sNewFrameSetPath;
	var sNewPage = page.replace("?", "&");
	
	if (window.top.location.pathname.indexOf(window._sFrameName) > -1)
	{
		sNewFrameSetPath = window.top.location.pathname + "?" + window._sBodyFrame + "=" + sNewPage;
	}
	else
	{
		var sPath = window.top.location.pathname;
		var asPathParts = sPath.split(/\//);
		if (asPathParts.length > 1)
		{
			asPathParts.pop();
		}	
		asPathParts.push(window._sFrameName);
		sNewFrameSetPath = "/" + asPathParts.join("/") + "?" + window._sBodyFrame + "=" + sNewPage;
	}
	
	return sNewFrameSetPath;
}

function FrameSetNav_GetMainNavURIPage(tWindow)
{
	var i = tWindow.location.pathname.lastIndexOf("/");
	var iLen = tWindow.location.pathname.length;
	
	var search = "";
	
	if (tWindow.location.search.length > 0)
	{
		search = "&" + tWindow.location.search.substr(1, tWindow.location.search.length);
	}
	
	var sNewFrameSetPath = tWindow.location.pathname.substring(0, i + 1) + window._sFrameName + "?" + window._sBodyFrame + "=" + tWindow.location.pathname.substring(i + 1, iLen) + search;
	
	return sNewFrameSetPath;
}

function FrameSetNav_GoToMainNavURIPage(tWindow)
{
	var sNewFrameSetPath = FrameSetNav_GetMainNavURIPage(tWindow);
	
	window.top.location.href = sNewFrameSetPath;
}

function FrameSetNav_GetNavURI(framedWindow)
{
	// returns a path to the resource in the main window, relative to the frameset page
	var sFrameSetPath = window.top.location.pathname;
	var sFramedPath = framedWindow.location.pathname;
	var asFrameSetPathParts = sFrameSetPath.split(/\//);
	asFrameSetPathParts.pop();
	var asFramedPathParts = sFramedPath.split(/\//);
	var sResourceName = asFramedPathParts.pop();
	var cDepth = Math.max(asFrameSetPathParts.length, asFramedPathParts.length);
	var sNewPath = "";
	var bDifferentPaths = false;
	for (nDepth = 0; nDepth < cDepth; nDepth++)
		{
		if (nDepth < asFrameSetPathParts.length)
			{
			if (nDepth < asFramedPathParts.length)
				bDifferentPaths = bDifferentPaths || (asFrameSetPathParts[nDepth] != asFramedPathParts[nDepth]);
			else
				bDifferentPaths = true;
			if (bDifferentPaths)
				sNewPath = "../" + sNewPath;
			}
		else
			bDifferentPaths = true;
		if (nDepth < asFramedPathParts.length)
			if (bDifferentPaths)
				sNewPath = sNewPath + asFramedPathParts[nDepth] + "/";
		}
	var sNewFrameSetPath = window.top.location.pathname + "?" + window._sBodyFrame + "=" + sNewPath + sResourceName;
	var sArgs = framedWindow.location.search;
	if (sArgs.length > 0)
		sNewFrameSetPath += "&" + sArgs.substr(1);
	return sNewFrameSetPath;
}

function FrameSetNav_GoToNavURI(framedWindow, useSecure)
{
	var sNewFrameSetPath = FrameSetNav_GetNavURI(framedWindow);
	
	var oldPathname = window.top.location.pathname;
	
	var newHref = window.top.location.protocol + "//" + window.top.location.host + sNewFrameSetPath;
	
	if (useSecure == true)
	{
		newHref = newHref.replace("http:", "https:");
	}
	else
	{
		newHref = newHref.replace("https:", "http:");
	}
	
	window.top.location.href = newHref;
}

function FrameSetNav_GoToNavURIPage(page, useSecure)
{
	var sNewFrameSetPath = FrameSetNav_GetNavURIPage(page);

	var newPathName = window.top.location.host + sNewFrameSetPath;
	newPathName = newPathName.replace("//", "/");
	
	var newHref = window.top.location.protocol + "//" + newPathName;
	
	if (useSecure == true)
	{
		newHref = newHref.replace("http:", "https:");
	}
	else
	{
		newHref = newHref.replace("https:", "http:");
	}
	
	window.top.location.href = newHref;
}

