var isIE, isOpera, isGecko, isSafari, isNN4, isProbaseCompatible;

/**
 * function def(v)
 * Determines if v is defined.
 * @param v mixed any variable
 * @return Returns true if the variable is defined.
 */
function def(v)
{
	return ((typeof v) != "undefined");
}

/**
 * function get(id)
 * Shorthand for 'document.getElementById'
 * @param id int an element id
 * @return the element
 */
function get(id)
{
	return document.getElementById(id);
}

/**
 * function r(e)
 * Has the same purpose as the ProBase r() function: dump the contents of e, for debug purposes.
 * The function will dump to an alert for simple variables, and will dump objects (and arrays) to a new window.
 * @param e mixed any variable
 */
function r(e)
{
	if ((typeof e) == 'object') {
		// place object attributes in array
		var r = new Array(), i = 0;
		for(a in e) r[i++] = a;
		// sort array
		r.sort();
		// show sorted
		var s = "<table border='1'>";
		for (i=0; i < r.length; i++) {
			s += "<tr><td>" + r[i] + "</td><td>" + e[r[i]] + "</td></tr> ";
		}
		s += "</table>";
		window.open().document.write(s + "<a href='javascript:window.close()'>close</a>");
	} else {
		alert(e);
	}
}

/**
 * function pbInit()
 * Contains ProBase initialization code, like the initialization of the isX variables. Is executed onload.
 */
function pbInit()
{
	isW3C = def(document.getElementById);
	isOpera = navigator.userAgent.indexOf("Opera") != -1;
	isIE = def(document.all) && !isOpera;
	isNN4 = def(document.layers);
	isGecko = !isNN4 && (navigator.userAgent.indexOf("Gecko") != -1);
	isSafari = navigator.userAgent.indexOf("Safari") != -1;
	isProbaseCompatible = isW3C;
}

/**
 * function pbGetNewURI(relLink)
 * Has the same purpose as the ProBase function $Nav->getNewURI()
 * If relLink is defined, it returns the full path to the page. If no relLink is given, the base URI is returned, without a trailing /.
 * @param relLink The relative link to a page (for instance: 'library/document/3'). Not required.
 */
function pbGetNewURI(relLink)
{
	return PbLib.getNewURI(relLink);
}

/**
 * function pbStopBubble(event)
 * Stops event from bubbling towards the window. Use this function if you want the browser to execute only the handler for the topmost element;
 * and skip the lower elements.
 * Note: the function has no effect in KHTML browsers.
 * @param event The bubbling event.
 */
function pbStopBubble(event)
{
	if (def(event) && event != null) {
		event.cancelBubble=true;
		if (event.stopPropagation) {
			event.stopPropagation();
		}
	}
}
var resizedWindowToContent = false;

/**
 * function resizeWindowToContent(height, width)
 * Resizes the window to the content of that window.
 * @param bool height Whether to resize the height
 * @param bool width Whether to resize the width
 */
function resizeWindowToContent(resizeHeight, resizeWidth)
{
	if (resizedWindowToContent) {
		return true;
	}
	resizedWindowToContent = true;

	if (typeof resizeHeight == 'undefined') {
		var resizeHeight	= true;
	}
	if (typeof resizeWidth == 'undefined') {
		var resizeWidth		= false;
	}

	var doc			= window.document;

	if (!doc.body) {
		return false;
	}

	// Set the available width and height of the screen
	var availWidth	= screen.availWidth ? screen.availWidth : 1024;
	var availHeight	= screen.availHeight ? screen.availHeight : 768;
	var availLeft	= screen.availLeft ? screen.availLeft : 0;
	var availTop	= screen.availTop ? screen.availTop : 0;

	// Positionning offset
	var posOffX = (availWidth / 2) + availLeft;
	var posOffY = (availHeight / 2) + availTop;

	// Find out the size of the chrome
	var currentWidth	= window.document.body.offsetWidth;
	var currentHeight	= window.document.body.offsetHeight;
	window.moveTo(posOffX - (currentWidth / 2), posOffY - (currentHeight / 2));
	window.resizeTo(currentWidth, currentHeight);
	var diffX			= currentWidth - window.document.body.offsetWidth;
	var diffY			= currentHeight - window.document.body.offsetHeight;

	// Initially set target width height to restore the original size or 50% of the available width and height
	var targetWidth		= resizeWidth ? 0.5 * (availWidth - availLeft) : currentWidth + diffX;
	var targetHeight	= resizeHeight ? 0.5 * (availHeight - availTop) : currentHeight + diffY;
	window.moveTo(posOffX - (targetWidth / 2), posOffY - (targetHeight / 2));
	window.resizeTo(targetWidth, targetHeight);

	//window.moveTo(0, 0);
	if (resizeWidth) {
		var curScroll = 0;
		if (document.all) {
			do {
				curScroll += 200;
				window.scrollTo(curScroll, 0);
			} while (Math.max(document.body.scrollLeft, document.body.parentNode.scrollLeft) >= curScroll);
			targetWidth += Math.max(document.body.scrollLeft, document.body.parentNode.scrollLeft);
		} else {
			do {
				curScroll += 200;
				window.scrollTo(curScroll, 0);
			} while (window.scrollX >= curScroll);
			targetWidth += window.scrollX;
		}
		window.scrollTo(0,0);
		targetWidth = Math.min(availWidth - availLeft, targetWidth + 1);
		window.moveTo((availWidth - targetWidth) / 2 + availLeft, (availHeight - targetHeight) / 2 + availTop);
		window.resizeTo(targetWidth, targetHeight);
	}
	if (resizeHeight) {
		var curScroll = 0;
		if (document.all) {
			do {
				curScroll += 200;
				window.scrollTo(0, curScroll);
			} while (Math.max(document.body.scrollTop, document.body.parentNode.scrollTop) >= curScroll);
			targetHeight += Math.max(document.body.scrollTop, document.body.parentNode.scrollTop);
		} else {
			do {
				curScroll += 200;
				window.scrollTo(0, curScroll);
			} while (window.scrollY >= curScroll);
			targetHeight += window.scrollY;
		}
		window.scrollTo(0,0);
		targetHeight = Math.min(availHeight - availTop, targetHeight + 1);
	}

	window.moveTo((availWidth - targetWidth) / 2 + availLeft, (availHeight - targetHeight) / 2 + availTop);
	window.resizeTo(targetWidth, targetHeight);
}

Event.observe(window, 'load', pbInit);