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;
		if (document.getElementById("ModelWindow") != null) {
		   heightUsed += 15;
		}
	}
	else {
		height = window.innerHeight;
	}
	
	i_HelpFrame.height = height - heightUsed;
	document.getElementsByName("Help_Toc")[0].height = height - heightUsed;
}

// ----------------------------------------------------------------------------
// This routine find 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 find 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 perticular 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_strElementName)
{
    var nShift = 0;
    var nAdjustmentForMaps = 0;
    if (PWdsapp_bIsIE)
		if(document.location.pathname.indexOf("mapView.aspx")>0 || document.location.pathname.indexOf("navMap.aspx")>0 )
			nAdjustmentForMaps=4; //this 4 matches the nPos from getSize()

	var clElement = document.getElementById(i_strElementName);
	nShift = document.body.clientWidth - getSize(clElement, WIDTH_USEDUP)+nAdjustmentForMaps;
	clElement.style.posWidth = nShift;
	
	nShift = document.body.clientHeight - getSize(clElement, HEIGHT_USEDUP)+nAdjustmentForMaps;
	clElement.style.posHeight = nShift;
}

// ----------------------------------------------------------------------------
// 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;
	var nOrgSize;
    
    // 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.
	if (typeof(document.all['SpanTable']) != 'undefined') {
		if (typeof(ObjWdsForm.sRF_PanePosition) != 'undefined') {
			nPanePosition = parseInt(ObjWdsForm.sRF_PanePosition.value);
			if (nPanePosition < 0) {
				// Intially, the value is set to -1.  In that case use the value assigned to
				// the span from the style in the stylesheet.
				nPanePosition = document.all['SpanTable'].offsetWidth;
			}
		}
		nSize = nPanePosition + i_nUserShift;
		if (nSize > nScreenWidth/2)
			nSize = nScreenWidth/2;

		nOrgSize = document.all['SpanTable'].style.posWidth;
		document.all['SpanTable'].style.posWidth = nSize;

		nMaxSize = document.all['FolderPane1'].offsetWidth + document.all['FolderPane2'].offsetWidth;
		if (nMaxSize == nSavedSize) {
			nSize = nOrgSize;
			document.all['SpanTable'].style.posWidth = nSize;
		}
		else {
			if (document.all['SpanTable'].style.posWidth < nMaxSize && i_nUserShift != 0) {
				nSize = nMaxSize - 4;
				document.all['SpanTable'].style.posWidth = nSize;
			}
			nSavedSize = nMaxSize;
			ObjWdsForm.sRF_PanePosition.value = nSize;
		}
	}				
	document.all['ExplorerTable'].style.posWidth = 1;
				
	// Find the height and width of the report content pane.
	RepositionElement("SpanTableContents");

	// Folder content and report content will be the same size.
    nShift = document.all['SpanTableContents'].style.posHeight;
	if (typeof(document.all['SpanTable']) != 'undefined') {
		document.all['SpanTable'].style.posHeight = nShift;
	}
	if (typeof(document.all['ExplorerTable']) != 'undefined') {
		document.all['ExplorerTable'].style.posHeight = nShift;
	}
}

// ----------------------------------------------------------------------------
// 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 (document.layers) {
		document.layers['LayerToolbar'].pageX = window.pageXOffset+20;
		document.layers['LayerToolbar'].top = 0;
	} 
	else if (typeof(document.all) != 'undefined') {
		if(bLoad) {
			onObjectClick();
		}
		if (typeof(document.all['SpanModal']) != 'undefined') {
		    // Resize the model pages
			RepositionElement("SpanModal");
		}
		else if (typeof(document.all['SpanTableContents']) != 'undefined') {
		    // Resize the Report Folder and Advanced search pages
			RepositionSpanTable(0);
		}
		else {
		    // Resize the chart and map page.
			if (typeof(document.all['SpanTable']) != 'undefined') {
				RepositionElement("SpanTable");
			}
		}
		if (typeof(document.all['CoverTop']) != 'undefined') {
			resizeCover();
		}
	}
}
