// Taken from John Goodman's book.
// Removed support for NN4.

// Glabl variables

var isCSS, isW3C, isIE4, isIE6CSS;
var fudgeFactor = {top:-1, left:-1};

function initDHTMLAPI()
{
	if (document.images)
	{
		isCSS = (document.body && document.body.style) ? true : false;
		isW3C = (isCSS && document.getElementById) ? true : false;
		isIE4 = (isCSS && document.all) ? true : false;
		isIE6CSS = (document.compatMode && document.compatMode.indexOf("CSS1") >= 0) ? true : false;
	}
}

// sets event handler to initialize API (if onload defined somewhere else, it
// needs to call the init function)

//window.onload = initDHTMLAPI();

// Convert object name string or object
// reference inot a valid element object reference

function getObject(obj)
{
	var theObj = {};

	if (typeof(obj) == "string")
	{
		if (isW3C)
		{
			theObj = document.getElementById(obj);
		}
		else if (isIE4)
		{
			theObj = document.all(obj);
		}
	}
	else
	{
		// pass through object reference
		theObj = obj;
	}

	return theObj;
}


// Convert object name string or object reference
// into a valid style reference

function getObjectStyle(obj)
{
	var theObj = getObject(obj);

	if (theObj && isCSS)
	{
		theObj = theObj.style;
	}

	return theObj;
}


// Position an object at a specific pixel coordinate
function shiftTo(obj, x, y) 
{
	var theObj = getObjectStyle(obj);

	if (theObj)
	{
		if (isCSS)
		{
			// equalize incorrect numerical value type
			var units = (typeof(theObj.left) == "string") ? "px" : 0;
			theObj.left = x + units;
			theObj.top = y + units;
		}
	}
}


// Move an object by x and/or y pixels

function shiftBy(obj, deltaX, deltaY)
{
	var theObj = getObjectStyle(obj);

	if (theObj)
	{
		if (isCSS)
		{
			// equalize incorrect numeric value type
			var units = (typeof(theObj.left) == "string") ? "px" : 0;
			theObj.left = getObjectLeft(obj) + deltaX + units;
			theObj.top = getObjectTop(obj) + deltaY + units;
		}
	}
}


// Set the z-order of an object

function setZIndex(obj, zOrder)
{
	var theObj = getObjectStyle(obj);

	if (theObj)
	{
		theObj.zIndex = zOrder;
	}
}


// Set the background color of an object

function setBGColor(obj, color)
{
	var theObj = getObjectStyle(obj);

	if (theObj)
	{
		if (isCSS)
		{
			theObj.backgroundColor = color;
		}
	}
}


// Set the visibility of an object to visible

function show(obj)
{
	o = getObject(obj);
	var theObj = getObjectStyle(obj);
	var doFilter = false;

	if (theObj)
	{
		// Note: a bug in IE on Windows causes script to fail unless
		// the filter is defined in the style sheet
		//if (o.filters && o.filters.item("DXImageTransform.Microsoft.Fade"))
			//doFilter = true;

		//if (doFilter)
			//o.filters.item("DXImageTransform.Microsoft.Fade").apply();

		theObj.visibility = "visible";

		//if (doFilter)
			//o.filters.item("DXImageTransform.Microsoft.Fade").play();
	}
}


// Set the visibility of an object to hidden

function hide(obj)
{
	var theObj = getObjectStyle(obj);

	if (theObj)
	{
		theObj.visibility = "hidden";
	}
}


// Retrieve the x coordinate of a positionable object

function getObjectLeft(obj)
{
	var elem = getObject(obj);

	var result = 0;

	if (document.defaultView)
	{
		var style = document.defaultView;
		var cssDecl = style.getComputedStyle(elem, "");

		if (parseInt(cssDecl.getPropertyValue("left")))
			result = cssDecl.getPropertyValue("left");
		else if (elem.offsetParent)
			result += elem.offsetLeft + getObjectLeft(elem.offsetParent);
	}
	else if (elem.currentStyle)
	{
	//debug(obj + ": left: " + elem.currentStyle.left);
		if (parseInt(elem.currentStyle.left) || elem.currentStyle.left == '')
		{
			if (elem.currentStyle.left != '')
				result = elem.currentStyle.left;
		}
		else if (elem.offsetParent)
			result += elem.offsetLeft + getObjectLeft(elem.offsetParent);
	}
	else if(elem.style)
	{
		if (parseInt(elem.style.left))
			result = elem.style.left;
		else if (elem.offsetParent)
			result += elem.offsetLeft + getObjectLeft(elem.offsetParent);
	}

	return(parseInt(result));
}


//  Retrieve the y corrdinate of a positionable object

function getObjectTop(obj)
{
	var elem = getObject(obj);

	var result = 0;

	if (document.defaultView)
	{
		var style = document.defaultView;
		var cssDecl = style.getComputedStyle(elem, "");

		if (parseInt(cssDecl.getPropertyValue("top")))
			result = cssDecl.getPropertyValue("top");
		else if (elem.offsetParent)
			result += elem.offsetTop + getObjectTop(elem.offsetParent);
	}
	else if (elem.currentStyle)
	{
	//debug(obj + ": top: " + elem.currentStyle.top);
		if (parseInt(elem.currentStyle.top) || elem.currentStyle.top == '')
		{
			if (elem.currentStyle.left != '')
				result = elem.currentStyle.top;
		}
		else if (elem.offsetParent)
			result += elem.offsetTop + getObjectTop(elem.offsetParent);
	}
	else if(elem.style)
	{
		if (parseInt(elem.style.top))
			result = elem.style.top;
		else if (elem.offsetParent)
			result += elem.offsetTop + getObjectTop(elem.offsetParent);
	}

	return parseInt(result);
}


// Retrieve the rendered width of an element

function getObjectWidth(obj)
{
	var elem = getObject(obj);

	var result = 0;

	if (elem.offsetWidth)
	{
	//debug(obj + ": offsetWidth: " + elem.offsetWidth + ", scrollWidth: " + elem.scrollWidth);
		if (elem.scrollWidth && (elem.offsetWidth != elem.scrollWidth))
		{
			result = elem.scrollWidth;
		}
		else
		{
			result = elem.offsetWidth;
		}
	}
	else if (elem.clip && elem.clip.width)
	{
		result = elem.clip.width;
	}
	else if (elem.style && elem.style.pixelWidth)
	{
		result = elem.style.pixelWidth;
	}

	return parseInt(result);
}


// Retrieve the rendered height of an element

function getObjectHeight(obj)
{
	var elem = getObject(obj);

	var result = 0;

	if (elem.offsetHeight)
	{
		result = elem.offsetHeight;
	}
	else if (elem.clip && elem.clip.height)
	{
		result = elem.clip.height;
	}
	else if (elem.style && elem.style.pixelHeight)
	{
		result = elem.style.pixelHeight;
	}

	return parseInt(result);
}


// Return the available content width space in browser window

function getInsideWindowWidth()
{
	if (window.innerWidth)
	{
		return window.innerWidth;
	}
	else if(isIE6CSS)
	{
		// measure the html element's clientWidth
		return document.body.parentElement.clientWidth;
	}
	else if (document.body && document.body.clientWidth)
	{
		return document.body.clientWidth;
	}

	return 0;
}


// Return the available content height space in browser window

function getInsideWindowHeight()
{
	if (window.innerHeight)
	{
		return window.innerHeight;
	}
	else if (isIE6CSS)
	{
		// measure the html element's clientHeight
		return document.body.parentElement.clientHeight;
	}
	else if (document.body && document.body.clientHeight)
	{
		return document.body.clientHeight;
	}

	return 0;
}


// Sets "fudge factor" for initial object positioning, due to IE/Mac
// implementation bug.

function correctPositioning(obj)
{
	obj = getObject(obj);

	if (fudgeFactor.top == -1)
	{
		if ((typeof(obj.offsetTop) == "number") && obj.offsetTop > 0)
		{
			fudgeFactor.top = obj.offsetTop;
			fudgeFactor.left = obj.offsetLeft;
		}
		else
		{
			fudgeFactor.top = 0;
			fudgeFactor.left = 0;
		}

		if (obj.offsetWidth && obj.scrollWidth)
		{
			if (obj.offsetWidth != obj.scrollWidth)
			{
				obj.style.wdith = obj.scrollWidth;
			}
		}
	}

	return fudgeFactor;
}

// position second object relative to first

function positionRelativeTo(obj1, obj2, position, dx, dy, doNotWrap)
{
	var left1 = getObjectLeft(obj1);
	var top1  = getObjectTop(obj1);

	if (!dx)
		dx = 0;

	if (!dy)
		dy = 0;

	//debug(obj1 + ": left: " + left1 + ", top: " + top1);
	var bottom1 = top1 + getObjectHeight(obj1);
	var width1 = getObjectWidth(obj1);
	//debug(obj1 + ": bottom: " + bottom1 + ", width: " + width1);
	var top2  = 0;

	var left2 = left1 + width1 + dx;
	var width2 = getObjectWidth(obj2);
	var pageWidth = document.body.offsetWidth;

	switch (position)
	{
		case 'right':
			if (left2 + width2 > pageWidth)
				left2 = left1 - width2 - dx;

			top2 = top1 + dy;
			break;
		case 'left':
			break;
		case 'top':
			break;
		case 'bottom':
			left2 = left1 + dx;

			if (left2 + width2 > pageWidth)
				left2 = left1 - width2 - dx + width1;

			top2 = bottom1 + dy;
			break;
	}

	//debug(obj2 + ": moving to: left: " + left2 + ", top: " + top2);

	shiftTo(obj2, left2, top2);
}

function debug(msg)
{
	if (!debug.box)
	{
		debug.box = document.createElement("div");
		debug.box.setAttribute("style",
									  "background-color: white; " +
									  "font-family: monospace; " +
									  "border: solid black 3px; " +
									  "padding: 10px;");
		document.body.appendChild(debug.box);
	}

	var p = document.createElement("p");
	p.appendChild(document.createTextNode(msg));

	debug.box.insertBefore(p, debug.box.firstChild);
}
