////////////////////
// Copyright (c) 2009, Perceptive Automation, LLC. All rights reserved.
// http://www.perceptiveautomation.com
//
// Redistribution of this source file, its binary forms, and images are not allowed.
// We are working on our open source license terms, but at this point the source files,
// its binary forms, and all images cannot be redistributed without written permission
// from both Perceptive Automation, LLC. and Andy Turner, HighEarthOrbit software.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
//
// IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
//
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
////////////////////

function getDivRefFromID(divID, fromDoc) {
	if (!fromDoc) {
		fromDoc = document;
	}
	if (fromDoc.layers && fromDoc.layers[divID]) {
		return fromDoc.layers[divID];
	}
	if (fromDoc.getElementById && fromDoc.getElementById(divID)) {
		return fromDoc.getElementById(divID);
	}
	if (fromDoc.all && fromDoc.all[divID]) {
		return fromDoc.all[divID];
	}
	return fromDoc[divID];
}

function resizeWinToDiv(divID) {
	var divRef = getDivRefFromID(divID);
	if (!divRef) {
		return;
	}

	// Because not all browsers support specifying the resize amount of
	// the inner contents (divID), we resize 230 larger than that to make
	// sure the scroll bars disappear. We can then calculate a good resize
	// value using the outter dims and the client (inner) dims.
	var objWidth = divRef.clip ? divRef.clip.width : divRef.offsetWidth;
	var objHeight = divRef.clip ? divRef.clip.height : divRef.offsetHeight;

	var curWidth = objWidth + 230;
	var curHeight = objHeight + 230;
	window.resizeTo(curWidth, curHeight);

	var docRef = window.document.documentElement;
	var bodyRef = window.document.body;
	var winBorderWidth = 0, winBorderHeight = 0;

	if (window.innerWidth) {
		winBorderWidth = curWidth - window.innerWidth;
		winBorderHeight = curHeight - window.innerHeight;
	}
	else if (docRef && docRef.clientWidth) {
		winBorderWidth = curWidth - docRef.clientWidth;
		winBorderHeight = curHeight - docRef.clientHeight;
	}
	else if (bodyRef && bodyRef.clientWidth) {
		winBorderWidth = curWidth - bodyRef.clientWidth;
		winBorderHeight = curHeight - bodyRef.clientHeight;
	}
	// For Opera 6 we need to remove 16 pixels for the scrollbar that doesn't
	// show (grrrr).
	if (window.opera && !document.childNodes) {
		winBorderWidth -= 16;
	}
	window.resizeTo(objWidth + winBorderWidth, objHeight + winBorderHeight);
}
