/* 
-------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------
*/
//
// windowUtils.js
//


function GetWindowInfo(w, prefix)
{
    if (!prefix) prefix="";
    
    var depth=0;
    var w2 = w;
    while (w2 != w2.top)
    {
        depth++;
        w2 = w2.parent;
    }
    var s = "           ".substring(0,depth) + prefix + "Name:" + w.name + ", depth:" + depth + ", #frames=" + w.frames.length;
    for (var i=0; i<w.frames.length; i++)
    {
        s += "\n";
        s += "           ".substring(0,depth) + prefix + "frame[" + i + "]\n" + GetWindowInfo(w.frames[i], prefix);
    }
    return s;
}

function OpenWindow(url, target, features) 
{
	//alert("Entered OpenWindow()");
	
	var w = window.open(url, target, features);
	var wOpener = w.opener;
	if (wOpener != window)
	{
		//alert("Opener [" + wOpener.name + "] is not the current window.");
		
		// Close and reopen the window, so that my cookied, etc are in there.
		w.close();
		w = window.open(url, target, features);
		
		// Thoughts:  open another window, but name it something different (targetN).  Keep trying until 
		// we find one whose opener is us (existed or gets created).  This way, different "sessions" will
		// have their own "named" window.
	}

	//		n	e	meaning					action
	//		-	-	-------					------
	//		0	0	just created			make new
	//		0	1	exists, not new			do nothing
	//		1	0	was new, not anymore	no longer new, make already exists
	//		1	1	-
	//	
	
	var isNew = true;
	var alreadyExists = false;
	
	try 
	{
		isNew = new Boolean(w['_isNew']);
		alreadyExists = new Boolean(w['_alreadyExists']);
	}
	catch (e)
	{
		// Some windows don't allow you to set properties.
		// I'm seeing this with the window we're displaying PDF in.
		//alert("For window |" + w.name + "| properties not supported |" + e + "|");
		return w;
	}
	
	//alert("For window |" + w.name + "| (before) isNew=|" + isNew + "|  alreadyExists=|" + alreadyExists + "|");
	if ((! isNew.valueOf()) && (! alreadyExists.valueOf()))
	{
		w['_isNew'] = true;
		w['_alreadyExists'] = false;
	}
	else 
	{
		w['_isNew'] = false;
		w['_alreadyExists'] = true;
	}
	//alert("For window |" + w.name + "| (after) isNew=|" + w['_isNew'] + "|  alreadyExists=|" + w['_alreadyExists'] + "|");
	
	return w;
}

function IsNewWindow(w) { return IsWindowNew(w); }
function IsWindowNew(w) 
{
	//alert("Entered IsWindowNew()");
	
	if (! w) w = window;

	var isNew = false;
	if (w['_isNew'])
		isNew = true;
	//alert("In IsWindowNew()  for window |" + w.name + "| isNew = |" + isNew + "|");
	return isNew;
}



function SetWindowWidthBasedOnElement(elementId)
{
	SetWindowWidthBasedOnElementAndPadding(elementId, '30');
}

function SetWindowWidthBasedOnElementAndPadding(elementId, padding)
{
	var element = document.getElementById(elementId);
	if (! element)
		//alert("Element " + elementId + " could not be found in SetWindowWidthBasedOnElementAndPadding().");
	var elementWidth = element.offsetWidth;
	//alert("SetWindowWidthBasedOnElementAndPadding(): element width=|" + elementWidth + "| padding=|" + padding + "|");
	SetWindowWidth(Number(elementWidth) + Number(padding));	
}

function SetWindowHeightBasedOnElement(elementId)
{
	SetWindowHeightBasedOnElementAndPadding(elementId, '30');
}

function SetWindowHeightBasedOnElementAndPadding(elementId, padding)
{
	var element = document.getElementById(elementId);
	if (! element)
		alert("Element " + elementId + " could not be found in SetWindowHeightBasedOnElementAndPadding().");
	var elementHeight = element.offsetHeight;
	//alert("SetWindowHeightBasedOnElementAndPadding(): element height=|" + elementHeight + "| padding=|" + padding + "|");
	SetWindowHeight(Number(elementHeight) + Number(padding));	
}

function SetMinimalWindowWidthBasedOnElement(elementId)
{
	return SetMinimalWindowWidthBasedOnElementAndPadding(elementId, 30);
}

function SetMinimalWindowWidthBasedOnElementAndPadding(elementId, padding)
{
	var element = document.getElementById(elementId);
	if (! element)
		alert("Element " + elementId + " could not be found in SetMinimalWindowWidthBasedOnElementAndPadding().");
	var elementWidth = element.offsetWidth;
	//alert("SetMinimalWindowWidthBasedOnElementAndPadding(): element width=|" + elementWidth + "| padding=|" + padding + "|");
	return SetMinimalWindowWidth(Number(elementWidth) + Number(padding));	
}

function SetMinimalWindowHeightBasedOnElement(elementId)
{
	return SetMinimalWindowHeightBasedOnElementAndPadding(elementId, 30);
}

function SetMinimalWindowHeightBasedOnElementAndPadding(elementId, padding)
{
	var element = document.getElementById(elementId);
	if (! element)
		alert("Element " + elementId + " could not be found in SetMinimalWindowHeightBasedOnElementAndPadding().");
	var elementHeight = element.offsetHeight;
	//alert("SetMinimalWindowHeightBasedOnElementAndPadding(): element height=|" + elementHeight + "| padding=|" + padding + "|");
	return SetMinimalWindowHeight(Number(elementHeight) + Number(padding));	
}



// +++ Set window minimal width and height +++

function SetMinimalWindowWidth(minimalWidth) 
{
	var currentWidth = GetWindowWidth();
	//alert("current width=|" + currentWidth + "|");
	if (currentWidth < minimalWidth)
	{
		SetWindowWidth(minimalWidth);
		return true;
	}
	return false;
}

function SetMinimalWindowHeight(minimalHeight)
{
	var currentHeight = GetWindowHeight();
	//alert("current height=|" + currentHeight + "|");
	if (currentHeight < minimalHeight)
	{
		SetWindowHeight(minimalHeight);
		return true;
	}
	return false;

}

// --- Set window minimal width and height ---



// +++ Set window width and height +++

function SetWindowWidth(width) 
{
	var screenWidth = screen.availWidth;
	
	//alert("For window |" + window.name + "|  width=|" + GetWindowWidth() + "| Setting width to |" + width + "| screenWidth=|" + screenWidth + "|");
	
	// Protect against it being too big.
	var maxWidth = screenWidth - 29;	// 25 for right side, 4 for left side.
	if (width >= maxWidth)
		width = maxWidth;
		
	var windowLeft = 0;
	if (window.screenLeft)
		windowLeft = window.screenLeft;		// IE only


	if (window.innerWidth)
		window.innerWidth = width
	else 
	{
		var currentWidth = GetWindowWidth();
		var delta = width - currentWidth;
		//alert("Adjusting width by " + delta + " to " + width + ".");
		if (delta != 0)
		{
			// if this makes the part of the window extend off the screen, we need to move the window
			// in some.  Otherwise, it looks like IE (in some situations) will not allow the window to 
			// extend off the screen.
			if (windowLeft + width > screenWidth)
			{
				//alert("Moving window in some first");
				window.moveBy(-delta, 0)
			}
			
			window.resizeBy(delta, 0);
		}
	}

	//alert("window width is now |" + GetWindowWidth() + "|");
}

function SetWindowHeight(height)
{
	var screenHeight = screen.availHeight;

	//alert("For window |" + window.name + "|   height=|" + GetWindowHeight() + "| Setting height to |" + height + "| screenHeight=|" + screenHeight + "|");
	
	// Protect against it being too big.
	var maxHeight = screenHeight - 60;	// 30 for top bar, and 30 for bottom bar.
	if (height >= maxHeight)
		height = maxHeight;
		
	var windowTop = 0;
	if (window.screenTop)
		windowTop = window.screenTop;			// IE only


	if (window.innerHeight)
		window.innerHeight = height
	else 
	{
		var currentHeight = GetWindowHeight();
		var delta = height - currentHeight;
		//alert("Adjusting height by " + delta + " to " + height + ".");
		if (delta != 0)
		{
			// if this makes the part of the window extend off the screen, we need to move the window
			// in some.  Otherwise, it looks like IE (in some situations) will not allow the window to 
			// extend off the screen.
			if (windowTop + height > screenHeight)
			{
				//alert("Moving window up some first");
				window.moveBy(0, -delta)
			}

			window.resizeBy(0, delta);
		}
	}

	//alert("window height is now |" + GetWindowHeight() + "| maxHeight=|" + maxHeight + "|");
}

// --- Set window width and height ---



// +++ Get window width and height +++

function GetWindowWidth(win)
{
	var _fixedWindowWidth;
	if (!win) win = window;
		
	try
	{
	if(_fixedWindowWidth)
		return _fixedWindowWidth;
	}
	catch(e) {}

	if (win.innerWidth)
		return win.innerWidth;

	if (win.document) 
	{
		if (win.document.body)
			return win.document.body.clientWidth;
	}
	
	return 100;		// Can't determine width yet.
}


function GetWindowHeight(win)
{
	var _fixedWindowHeight;
	if (!win) win = window;

	try
	{
	    if(_fixedWindowHeight)
		    return _fixedWindowHeight;
	}
	catch(e) {}

	if (win.innerHeight)
		return win.innerHeight;
		
	if (win.document && win.document.body)
		return win.document.body.clientHeight;
		
	return 100;		// Can't determine height yet.
}


// --- Get window width and height ---


function MoveWindowToCenterOfScreen(win) 
{
	//alert("Entered MoveWindowToCenterOfScreen() for window |" + win.name + "|");
	
	if (! win) win = window;
	
	var windowWidth = GetWindowWidth(win);
	var windowHeight = GetWindowHeight(win);
	
	var screenWidth = screen.availWidth - 29;
	var screenHeight = screen.availHeight - 60;
	
	
	var x = ((.5 * screenWidth) - (.5 * windowWidth));
	var y = ((.5 * screenHeight) - (.5 * windowHeight));
	
	//alert("MoveWindowToCenterOfScreen(): screenWidth=" + screenWidth + " (" + (.5 * screenWidth) + ") windowWidth=" + windowWidth + " (" + (.5 * windowWidth) + ") x=" + x + ".");
	//alert("MoveWindowToCenterOfScreen(): screenHeight=" + screenHeight + " (" + (.5 * screenHeight) + ") windowHeight=" + windowHeight + " (" + (.5 * windowHeight) + ") y=" + y + ".");

	win.moveTo(x, y);
	
} // MoveWindowToCenterOfScreen


function MoveWindowOntoScreen(win)
{
	if (! win) win = window;
		
	//alert("Entered MoveTheWindowOntoScreen() for window |" + win.name + "|");

	var moved = false;
	
	var windowWidth = GetWindowWidth(win);
	var windowHeight = GetWindowHeight(win);
	
	var screenWidth = screen.availWidth;
	var screenHeight = screen.availHeight;
	
	var windowLeft = 0;
	if (win.screenLeft)
		windowLeft = win.screenLeft;		// IE only
	var windowTop = 0;
	if (win.screenTop)
		windowTop = win.screenTop;			// IE only

	var windowRight = windowLeft + windowWidth;
	var windowBottom = windowTop + windowHeight;
	
	var minLeft = 4;
	var maxRight = screenWidth - 25;
	

	//alert("MoveTheWindowOntoScreen(" + win.name + ") : windowLeft=|" + windowLeft + "| minLeft=|" + minLeft + "| windowRight=|" + windowRight + "| maxRight=|" + maxRight + "| screenWidth=|" + screenWidth + "|");
	
	if (windowLeft < minLeft)
	{
		// move the window to the right.
		var moveBy = minLeft - windowLeft;
		//alert("MoveTheWindowOntoScreen(" + win.name + ") : moveBy(right)=|" + moveBy + "|");
		win.moveBy(moveBy, 0)
		moved = true;
	}
	else if (windowRight > maxRight)
	{
		// move the window to the left;
		var moveBy = windowRight - maxRight;
		if (windowLeft - moveBy < minLeft) 
			moveBy = windowLeft - minLeft;
			
		//alert("MoveTheWindowOntoScreen(" + win.name + ") : moveBy(left)=|" + moveBy + "|");
		win.moveBy(-moveBy, 0);
		moved = true;
	}
	
	var minTop = 30;
	var maxBottom = screenHeight - 30;
	
	//alert("MoveTheWindowOntoScreen(" + win.name + ") : windowTop=|" + windowTop + "| minTop=|" + minTop + "| windowBottom=|" + windowBottom + "| maxBottom=|" + maxBottom + "| screenHeight=|" + screenHeight + "|");

	if (windowTop < minTop) 
	{
		// move the window down.
		var moveBy = minTop - windowTop;
		win.moveBy(0, moveBy);
		moved = true;
	}
	else if (windowBottom > maxBottom)
	{
		// move the window up
		var moveBy = windowBottom-maxBottom;
		if (windowTop - moveBy < minTop)
			moveBy = windowTop - minTop;
			
		//alert("MoveTheWindowOntoScreen(" + win.name + ") : moveBy=|" + moveBy + "|");
		win.moveBy(0, -moveBy);
		moved = true;
		
	}
	
	return moved;
		
} // MoveTheWindowOntoScreen()


