var css_offsetTop = 0;

function CSS_setDropDownPosition(scrollingElement,
											itemDiv, itemHeaderDiv, itemFooterDiv, button, height, bClearDiv)
{
	if (!scrollingElement) scrollingElement = document.body;
	
	var rv = 0;
	
	var windowHeight = parseInt(CSS_getFrameHeight());
	var totalHeight  = (parseInt(height) +
								 button.offsetHeight) +
								((itemHeaderDiv != null) ? itemHeaderDiv.offsetHeight : 0) +
								((itemFooterDiv != null) ? itemFooterDiv.offsetHeight : 0);

	var coords = UTIL_getPageCoords(button);
	var scrollCoords = UTIL_getPageCoords(scrollingElement);
	var scrollTop = scrollingElement.scrollTop + scrollCoords.x;
	
	var c = (coords.y + totalHeight - scrollTop);
	//alert('c=' + c + ', windowHeight=' + windowHeight +
	//		', coords.y=' + coords.y + ', totalHeight=' + totalHeight);
	if (c > windowHeight)
	{
		var d = (coords.y - totalHeight);
		if (d < scrollTop)
		{
			height = coords.y - ((itemHeaderDiv != null) ? itemHeaderDiv.offsetHeight : 0) +
									  ((itemFooterDiv != null) ? itemFooterDiv.offsetHeight : 0);
			height -= scrollTop;
			rv = scrollTop;
		}
		else if (c - windowHeight == 0 || (coords.y - totalHeight) < 0)
		{
			height = (height - (coords.y - totalHeight) - 35);
		}
		else
		{
			rv = coords.y - totalHeight + button.offsetHeight;
		}
	}
	else
	{
		rv = coords.y + button.offsetHeight;
	}
	
	if (bClearDiv)
	{
		itemDiv.innerHTML    = "";
		itemDiv.style.height = height;
	}
	
	itemDiv.style.display  = "inline";
	itemDiv.style.position = "absolute";
	
	if (itemHeaderDiv)
	{
		itemHeaderDiv.style.display  = "inline";
	}
	
	return rv;
}

function CSS_setPosition(element, top, bottom, left, right)
{
	if (bIe)
	{
   	if (left != -1) element.style.posLeft = left;
   	if (top != -1)  element.style.posTop  = top + css_offsetTop;
   	if (bottom != -1) element.style.posBottom = bottom;
   	if (right != -1)  element.style.posRight = right;
	}
	else if (bMozilla)
	{
   	if (left != -1)   element.style.left = left;
   	if (top != -1)    element.style.top  = top;
   	if (bottom != -1) element.style.bottom = bottom;
   	if (right != -1)  element.style.right = right;
	}
}

function CSS_setHeight(element, height)
{ 
	if (bIe)
	{
		//changed to style.height for dashboard - posHeight reqs a int and CSS returns the units as well (e.g., '100px' ) 
		//no need to do it as posHeight as it works like this in both browsers
		element.style.height = height;		
	}
	else if (bMozilla)
	{
		element.style.height = height;
	}
}

function CSS_getHeight(element, frameName)
{
	return CSS_getSize(element, "height", frameName);
}

function CSS_getWidth(element, frameName)
{
	return CSS_getSize(element, "width", frameName);
}

function CSS_getFrameHeight()
{
	var rv = -1;
	
	if (bIe)
	{
		rv = document.body.clientHeight;
	}
	else
	{
		rv = self.innerHeight;
	}
	
	return rv;
}

function CSS_getFrameWidth()
{
	var rv = -1;
	
	if (bIe)
	{
		rv = document.body.clientWidth;
	}
	else
	{
		rv = self.innerWidth;
	}
	
	return rv;
}

function CSS_getSize(element, type, frameName)
{
	var rv = -1;
	
	if (bIe)
	{
		if (type == "height")
		{
			rv = element.clientHeight;
		}
		else if (type == "width")
		{
			rv = element.clientWidth;
		}
	}
	else
	{
		rv = document.defaultView.
   	   	  	getComputedStyle(element, "").getPropertyValue(type);
		rv = rv.substring(0, rv.length - 2);
	}
	
	return rv;
}

/**
 * @author bcollins
 */
function CSS_checkElementFit(element, yOffset, xOffset, coords, useParent, additionalXspacing, additionalYspacing)
{
	var elementHeight = parseInt(CSS_getHeight(element));
	var windowHeight = parseInt(CSS_getFrameHeight());
	
	var elementWidth = parseInt(CSS_getWidth(element));
	var windowWidth = parseInt(CSS_getFrameWidth());
	
	var additionalX = 0;
	var additionalY = 0;
	
	if (useParent)
	{
		windowHeight = parseInt(parent.CSS_getFrameHeight());
		windowWidth = parseInt(parent.CSS_getFrameWidth());
	}
	
	if (additionalXspacing)
	{
		additionalX = additionalXspacing;
	}
	
	if (additionalYspacing)
	{
		additionalY = additionalYspacing;
	}
	
	//debugging
	//parent.alert("coord x " + coords.x);
	//parent.alert("coord y " + coords.y);
	//parent.alert("divHeight " + elementHeight);
	//parent.alert("divWidth " + elementWidth);
	//parent.alert("windowWidth " + windowWidth);
	//parent.alert("windowHeight " + windowHeight);

	if ((elementWidth + coords.x) > windowWidth)
	{
		coords.x = (bIe) 
			? windowWidth - (elementWidth + additionalX)
			: windowWidth - (elementWidth + additionalX + 30);
	}
	
	if ((elementHeight + coords.y) > windowHeight)
	{
		coords.y = (bIe) 
			? windowHeight - (elementHeight + additionalY)
			: windowHeight - (elementHeight + additionalY + 10);
	}
	
	yOffset = (typeof(yOffset) == "undefined") ? 0 : yOffset;
	xOffset = (typeof(xOffset) == "undefined") ? 0 : xOffset;
	//debugging
	//parent.alert("on set coord x " + coords.x);
	//parent.alert("on set coord y " + coords.y);
	//parent.alert("on set yOffset " + yOffset);
	//parent.alert("on set xOffset " + xOffset);
	CSS_setPosition(element, 
			parseInt(coords.y) + parseInt(yOffset), -1, 
			parseInt(coords.x) + parseInt(xOffset), -1);
}
