var OFFSET_TOP = 1;
var OFFSET_LEFT = 2;
var BOTTOM_HEIGHT = 3;
var RIGHT_WIDTH = 4;
var WIDTH_USEDUP = 5;
var HEIGHT_USEDUP = 6;

var nSavedSize = 0;

function resizeCover()
{
	var cover = document.getElementById("cover");
	var coverTop = document.getElementById("CoverTop");
   if (PWdsapp_bIsIE) {
      cover.style.posWidth = document.body.clientWidth;
	   cover.style.posHeight = document.body.clientHeight;
	   cover.style.posTop = 0;
	   coverTop.style.posWidth = document.body.clientWidth;
	   coverTop.style.posHeight = document.body.clientHeight;
	   if (bFullScreen) {
		   coverTop.style.posTop = 0;
		}
	   else {
		   coverTop.style.posTop = Math.max(document.body.clientHeight/2 - 50, 0);
		}
   }
   else {
      cover.style.width = window.innerWidth;
	   cover.style.height = window.innerHeight;
	   cover.style.top = 0;
	   coverTop.style.width = window.innerWidth;
	   coverTop.style.height = window.innerHeight;
	   if (bFullScreen) {
		   coverTop.style.top = 0;
		}
	   else {
		   coverTop.style.top = Math.max(window.innerHeight/2 - 50, 0);
		}
   }  
}

// gets the offset position of the element relative to the screen.
function GetOffSet(i_clElement, i_nDirection)
{  
    var nOffSet = 0;
    var clElement = i_clElement;
    
    switch (i_nDirection) {
		case OFFSET_TOP:
			while (clElement != null && clElement.tagName.toUpperCase() != "BODY") {
				nOffSet += clElement.offsetTop;
				clElement = clElement.offsetParent;
			}
			break;
		case OFFSET_LEFT:
			while (clElement != null && clElement.tagName.toUpperCase() != "BODY") {
				nOffSet += clElement.offsetLeft;
				clElement = clElement.offsetParent;
			}
			break;
		case BOTTOM_HEIGHT:
			if (clElement != null) {
				nOffSet = clElement.offsetHeight;
			}
			break;	
		case RIGHT_WIDTH:
			if (clElement != null) {
				nOffSet = clElement.offsetWidth;
			}
		break;
	}
    
    // If the nOffSet is equal to 1 then it is just the control placed in it then the size 
    // should be equal to 0.
    if (nOffSet == 1) nOffSet=0;
    
	return nOffSet;
}

// Find the height and width of a help page that can be resized
function RepositionSpanHelp(i_HelpFrame)
{
	var height = 0;
	var heightUsed = 0;
	var clElement = document.getElementById('helpPage');
	
	heightUsed += getSize(clElement, HEIGHT_USEDUP) + document.getElementById('HelpTabTac').offsetHeight - 4;
   if (PWdsapp_bIsIE){
		height = document.body.clientHeight;
	}
	else {
		height = window.innerHeight;
	}
	
	i_HelpFrame.height = height - heightUsed;
	document.getElementsByName("Help_Toc")[0].height = height - heightUsed;
}

// This routine finds the height of the bottom cell.
function getBottomHeight()
{
	var nPos = 0;
	var clElement;

	if (document.getElementById("cell5") != null) {
		clElement = document.getElementById("cell5");
		nPos = GetOffSet(clElement, BOTTOM_HEIGHT);
	}

	return nPos;					
}

// This routine finds the width of the right hand side (RHS) cell.
function getRHSWidth()
{
    var nPos = 0;
	var clElement;
	
    if (document.getElementById("cell6") != null) {
		clElement = document.getElementById("cell6");
		nPos = GetOffSet(clElement, RIGHT_WIDTH);
	}
	return nPos;					
}

// This routine finds the height and width of a particular element.
function getSize(i_clElement, i_nDirection)
{
   var nPos = 4;
	var clElement = i_clElement;
		
	if (i_nDirection == HEIGHT_USEDUP) {
		nPos += GetOffSet(clElement, OFFSET_TOP) + getBottomHeight(); 
    } else {
        nPos += GetOffSet(clElement, OFFSET_LEFT) + getRHSWidth();
    }
	return nPos;					
}

// Resize the element according to the element name.
function RepositionElement(i_clElement)
{
	var nShift = 0;
	var nAdjustment = 0;
	var nWindowWidth;
	var nWindowHeight;
	
	if (PWdsapp_bIsIE) {
		if (document.location.pathname.indexOf("mapView.aspx")>0 || document.location.pathname.indexOf("navMap.aspx") > 0) {
			nAdjustment=4; //this 4 matches the nPos from getSize()
		}
		
		if (document.location.pathname.toLowerCase().indexOf("summary.aspx")>0) {
			nAdjustment=-10;
		}
		
		if (document.location.pathname.indexOf("PrintOptions.aspx")>0) {
			nAdjustment=-10;
		}
	}

	if (window.innerWidth == undefined) {
		// IE
		nWindowWidth = document.body.clientWidth;
		nWindowHeight = document.body.clientHeight;
	}
	else {
		// NN
		nWindowWidth = window.innerWidth;
		nWindowHeight = window.innerHeight;
	}

	nShift = nWindowWidth - getSize(i_clElement, WIDTH_USEDUP) + nAdjustment;
	if(nShift<0) nShift=0;
	i_clElement.style.width = nShift + "px";
	
	nShift = nWindowHeight - getSize(i_clElement, HEIGHT_USEDUP) + nAdjustment;
	if(nShift<0) nShift=0;
	i_clElement.style.height = nShift + "px";
}

// ExplorerTable - Entire table (SpanTable + SpanTableContents)
// SpanTable - Folder content (folder structure is displayed)
// FolderPane1 - FolderName on the explorer header
// FolderPane2 - X mark where "showFolder click" on the explorer header
// SpanTableContents - Content pane ( Where the report name are displyed)
function RepositionSpanTable(i_nUserShift)
{
	var nPanePosition;
	var nScreenWidth = window.screen.availWidth;
	var nShift;
	var nSize = 0;
	var nMaxSize = 0;
	var nOrgSize;
	var clExplorerTable = null;
	var clFolderPane1 = null;
	var clFolderPane2 = null;
	var clSpanTable = null;
	var clSpanTableContents = null;
    
	// Find the size of the Folder content pane (FolderPane1 + FolderPane2) and remember the size 
	// in the form field sRF_PanePosition, initial default size is as defined in the stylesheet.
	clSpanTable = document.getElementById("SpanTable");
	if (clSpanTable != null) {
		if (typeof(ObjWdsForm.sRF_PanePosition) != "undefined") {
			nPanePosition = parseInt(ObjWdsForm.sRF_PanePosition.value);
			if (nPanePosition < 0) {
				// Intially, sRF_PanePosition, which holds the width of the folder pane, is set to -1.
				// In that case, use the value assigned to the span by the style in the stylesheet.
				nPanePosition = clSpanTable.offsetWidth;
			}
		}
		nSize = nPanePosition + i_nUserShift;
		if (nSize > nScreenWidth/2) {
			nSize = nScreenWidth/2;
		}

		nOrgSize = clSpanTable.offsetWidth;
		if (nSize >= 0) {
			clSpanTable.style.width = nSize + "px";
		}

		clFolderPane1 = document.getElementById("FolderPane1");
		clFolderPane2 = document.getElementById("FolderPane2");
		if (clFolderPane1 != null && clFolderPane2 != null) {
			nMaxSize = clFolderPane1.offsetWidth + clFolderPane2.offsetWidth;
		}
		if (nMaxSize == nSavedSize) {
			nSize = nOrgSize;
			if (nSize >= 0) {
				clSpanTable.style.width = nSize + "px";
			}
		}
		else {
			if (clSpanTable.offsetWidth < nMaxSize && i_nUserShift != 0) {
				nSize = nMaxSize - 4;
				if (nSize >= 0) {
					clSpanTable.style.width = nSize + "px";
				}
			}
			nSavedSize = nMaxSize;
			ObjWdsForm.sRF_PanePosition.value = nSize;
		}
	}
	
	clExplorerTable = document.getElementById("ExplorerTable");
	if (clExplorerTable != null) {
		clExplorerTable.style.width = "1px";
	}
				
	// Find the height and width of the report content pane.
	clSpanTableContents = document.getElementById("SpanTableContents");
	if (clSpanTableContents != null) {
		RepositionElement(clSpanTableContents);
	}

	// Folder content and report content will be the same height.
	nShift = clSpanTableContents.offsetHeight;
	if (clSpanTable != null) {
		clSpanTable.style.height = nShift + "px";
	}
	if (clExplorerTable != null) {
		clExplorerTable.style.height = nShift + "px";
	}
}

// This routine is called from every single page to resized to its Maximum
function onLayoutResize(bLoad)
{
	// This routine does not resize the TableView or the dimview page, it is resized in its own page.
	var bInHelp = document.getElementsByName("Help_Body");
	if (bInHelp.length == 1) {
	    // resize the help pages	
	    RepositionSpanHelp(bInHelp[0]);
	}
	if (bLoad) {
		onObjectClick();
	}
	var clSpanModal = document.getElementById("SpanModal");
	if (clSpanModal != null) {
		// Resize the modal pages
		RepositionElement(clSpanModal);
	}
	else {
		var clSpanTableContents = document.getElementById("SpanTableContents");
		if (clSpanTableContents != null) {
			// Resize the Report Folder and Advanced search pages
			RepositionSpanTable(0);
		}
		else {
			// Resize the chart and map page.
			var clSpanTable = document.getElementById("SpanTable");
			if (clSpanTable != null) {
				RepositionElement(clSpanTable);
			}
		}
	}
	var clCover = document.getElementById("CoverTop");
	if (clCover != null) {
		resizeCover();
	}
}

/* These functions are used in the online publisher feature. */
function Publisher_ResizeSpan(span1, nHeight, nWidth)
{
	if (PWdsapp_bIsIE) 
	{
		span1.style.posHeight = nHeight;
		span1.style.posWidth = nWidth;
	}
	else 
	{
		span1.style.overflow = "auto";
		span1.style.height = nHeight;
		span1.style.width = nWidth;
	}
}

function Publisher_GetAbsPos(i_clElement, i_strPos) 
{
	var nPos = 0;
	while (i_clElement != null) 
	{
		nPos += i_clElement["offset" + i_strPos];
		i_clElement = i_clElement.offsetParent;
	}
	return nPos;
}

function Publisher_GetAvailableHeight(span1)
{
	if (PWdsapp_bIsIE) 
		return document.body.clientHeight - GetOffSet(span1,1);
	else 
		return window.innerHeight - GetOffSet(span1,1);
}

function Publisher_GetAvailableWidth(span1)
{
	if (PWdsapp_bIsIE) 
		return document.body.clientWidth - Publisher_GetAbsPos(span1, "Left");
	else 
		return window.innerWidth - Publisher_GetAbsPos(span1, "Left");
}

