/* Liquitech API - Element **********************************
 *	
 *	This API contains all the functions needed to manipulate DOM
 *	elements using ID.
 *
 *********************************************************************/

/* Get Element * *******************************************************
 *
 *	TAKES:		Element ID Name
 * 	RETURNS:	The object associated with the given elment id name
 *
 *************************************************************************/
 
	function getElement(element)
	{
		 return document.getElementById(element);
	}


/* API Variables * *******************************************************
 *
 *	NOTES:		These variables are used as global references for all BRD
 *				API functions that are included after this API file.
 *				Consequently this creates a dependency that this API file
 *				must be included before any other BRD API. 
 *
 *************************************************************************/
	
	var glassElement 	= getElement("glassPane");
	
/*  Hide Element * *******************************************************
 *
 *	TAKES:		Element Object
 * 	RETURNS:	Sets the display of the element object to "none" which
 *				has the effect of hidding the object.
 *
 *************************************************************************/
	
	function hideElement(element)
	{
		if (element.style)
		{
			element.style.display = "none";
		}
		else
		{
			getElement(element).style.display = "none";
		}
	}
	
	
/*  Show Element * *******************************************************
 *
 *	TAKES:		Element Object
 * 	RETURNS:	Sets the display of the element object to "block" which
 *				has the effect of making the object visable.
 *
 *************************************************************************/
	
	function showElement(element)
	{
		if (element.style)
		{
			element.style.display = "block";
		}
		else
		{
			getElement(element).style.display = "block";
		}
	}
	
/*  Disable Element * *******************************************************
 *
 *	TAKES:		Element ID Name
 * 	RETURNS:	Sets the disable of the element object to "true" which
 *				has the effect of disabling the object.
 *
 *************************************************************************/
	
	function disableElement(element)
	{
		getElement(element).disabled = true;
	}
	
	
/*  Enable Element * *******************************************************
 *
 *	TAKES:		Element ID Name
 * 	RETURNS:	Sets the disable of the element object to "false" which
 *				has the effect of enabling the object.
 *
 *************************************************************************/
	
	function enableElement(element)
	{
		getElement(element).disabled = false;
	}
	
/*  Is Visable * *******************************************************
 *
 *	TAKES:		Element ID Name
 * 	RETURNS:	True if the element is visable, false if it is not
 *
 *************************************************************************/
	
	function isVisable(elementId)
	{
		if (getElement(elementId).style.display == "block")
		{
			return true;	
		}
		else
		{
			return false;	
		}
	}
	
	
/*  Get Width ********************************************************
 *
 *	TAKES:		Element Object
 * 	RETURNS:	The Width of the element object.
 *
 *************************************************************************/	

	function getWidth(element)
	{
		return element.offsetWidth;
	}
	
	
/*  Get Height * *******************************************************
 *
 *	TAKES:		Element Object
 * 	RETURNS:	The Height of the element object.
 *
 *************************************************************************/
 
	function getHeight(element)
	{
		return element.offsetHeight;
	}
	

/*  Get Element Position X * *******************************************************
 *
 *	TAKES:		Element Object
 * 	RETURNS:	The X position of the element object
 *
 *************************************************************************/
	
	function getElementX(contentElement)
	{
		var iReturnValue = 0;
		while(contentElement != null)
		{
			iReturnValue += contentElement.offsetLeft;
			contentElement = contentElement.offsetParent;
		}
		
		return iReturnValue;
	}
	

/*  Get Element Position Y * *******************************************************
 *
 *	TAKES:		Element Object
 * 	RETURNS:	The Y position of the element object
 *
 *************************************************************************/
	
	function getElementY(contentElement)
	{
		 var curtop = 0;
			if(contentElement.offsetParent)
				while(1)
				{
				  curtop += contentElement.offsetTop;
				  if(!contentElement.offsetParent)
					break;
				  contentElement = contentElement.offsetParent;
				}
			else if(contentElement.y)
				curtop += contentElement.y;
			return curtop;

	}

/*  Get Position X * *******************************************************
 *
 *	TAKES:		Element Object
 * 	RETURNS:	The X position of the center of the webpage based on the width
 *				of the element object
 *	NOTE:		This function is based on a standard website width space of 818px,
 *				and calculates the center position of the element object within that
 *				space.  This eliminates the abiguity of various monitor sizes,
 *				resolutions, and aspect ratios.
 *
 *************************************************************************/
	
	function getPosX(contentElement)
	{
		var curleft = 0;
		
		if(contentElement.offsetParent)
		{
			while(1) 
			{
			  curleft += contentElement.offsetLeft;
			  if(!contentElement.offsetParent)
				break;
			  contentElement = contentElement.offsetParent;
			}
		}
		else if(contentElement.x)
		{
			curleft += contentElement.x;
		}
		
		return curleft;
		
	}
	

/*  Get Position Y * *******************************************************
 *
 *	TAKES:		NOTHING
 * 	RETURNS:	The value of the scroll offset plus 100px
 *	NOTE:		Used for postioning elements 100px below the top of the page
 *
 *************************************************************************/

	function getPosY()
	{
		return getPageYOffset() + 100;
	}


/*  Set Position X * *******************************************************
 *
 *	TAKES:		Element Object, X position value
 * 	RETURNS:	NOTHING, changes the X position of the element object.
 *
 *************************************************************************/
 
	function setPosX(element, posXValue)
	{
			element.style.left = posXValue + "px";	
	}
	
	
/*  Set Position Y * *******************************************************
 *
 *	TAKES:		Element Object, Y position value
 * 	RETURNS:	NOTHING, changes the Y position of the element object.
 *
 *************************************************************************/
	
	function setPosY(element, posYValue)
	{
			element.style.top = posYValue + "px";	
	}


/*  Set Background Color * *******************************************************
 *
 *	TAKES:		Element Object, color code value (e.g. #000000)
 * 	RETURNS:	NOTHING, sets the background color of the element object to the
 *				given color value.
 *
 *************************************************************************/
 
	function setBackgroundColor(element, colorValue)
	{
		getElement(element).style.backgroundColor = colorValue;
	}

	
/*  Fade Out Element * *******************************************************
 *
 *	TAKES:		Element Object, Starting Fade Opacity Value, Fade Rate value between 1 and 100
 * 	RETURNS:	NOTHING
 *	NOTE:		Fades the element object at the rade rate specified.
 *
 *************************************************************************/
	
	function fadeOutElement(fadeElementId, fadeOpacity, fadeRate)
	{
		fadeOpacity = fadeOpacity - fadeRate;
		
		if (fadeOpacity < 0) { fadeOpacity = 0; }
		
		setOpacity(fadeElementId, fadeOpacity)
		
		if (fadeOpacity > 0)
		{
			setTimeout("fadeOutElement('" + fadeElementId + "', " + fadeOpacity + ", " + fadeRate + ")", 100);
		}
		
	}
	
/*  Fade Element * *******************************************************
 *
 *	TAKES:		Element Object, Starting Fade Opacity Value, Fade Rate value between 1 and 100
 * 	RETURNS:	NOTHING
 *	NOTE:		Fades the element object at the rade rate specified.
 *
 *************************************************************************/
	
	function fadeInElement(fadeElementId, fadeOpacity, fadeRate)
	{
		fadeOpacity = fadeOpacity + fadeRate;
		
		if (fadeOpacity > 100) { fadeOpacity = 100; }
		
		setOpacity(fadeElementId, fadeOpacity)
		
		if (fadeOpacity < 100)
		{
			setTimeout("fadeInElement('" + fadeElementId + "', " + fadeOpacity + ", " + fadeRate + ")", 50);
		}
		
	}


/*  Set Focus * *******************************************************
 *
 *	TAKES:		Element Object Id
 * 	RETURNS:	NOTHING
 *	NOTE:		Sets the focus of the element that corresponds to the 
 *				element object Id.
 *
 *************************************************************************/
 
	function setFocus(elementId)
	{
		if (elementId != "")
		{
			document.getElementById(elementId).focus();
		}
	}
	
	
/*  Set Opacity * *******************************************************
 *
 *	TAKES:		Element Object Id
 * 	RETURNS:	NOTHING
 *	NOTE:		Sets the opacity of the element
 *
 *************************************************************************/
 
	function setOpacity(elementId, opacityValue)
	{
		if (elementId != "")
		{
			var fadeElement = getElement(elementId);
			fadeElement.style.filter = "alpha(opacity=" + opacityValue + ");"; // IE
			fadeElement.style.zoom = "1";
			fadeElement.style.MozOpacity = opacityValue/100; // FireFox, Safari
		}
	}
	
	
/*  Get Tag Group * *******************************************************
 *
 *	TAKES:		Element tag, regular expression that describes the id pattern
 *				of the  group of tag elements you're looking for.
 * 	RETURNS:	Element Array of matched element tag ids with the provided regular
 *				expression.
 *	NOTE:		Uses push to load the element array
 *
 *************************************************************************/
	
	function getTagGroup(elementTag, regExp)
	{
		var tagArray = document.getElementsByTagName(elementTag);
		var elementArray = new Array();
		
		for(ctr=0; ctr < tagArray.length; ctr++)
		{
			if ((tagArray[ctr].id).search(regExp) != -1)
			{
				if (elementArray.length > 0)
				{
					elementArray = elementArray.concat(new Array(tagArray[ctr]));
				}
				else
				{
					elementArray = new Array(tagArray[ctr]);
				}
			}
		}
		
		return elementArray;
	}
	
/*  Remove All Children * *******************************************************
 *
 *	TAKES:		Parent Node Element
 * 	RETURNS:	NOTHING
 *	NOTE:		Removes all the child nodes within the Parent Node Element
 *
 *************************************************************************/
	
	function removeAllChildren(parentNode)
	{
		if (parentNode)
		{
			while(parentNode.firstChild)
			{
				parentNode.removeChild(parentNode.firstChild);	
			}
		}
	}
	
 /* Contains  Point * *******************************************************
 *
 *	TAKES:		Element, Point X, Point Y
 * 	RETURNS:	True if the Element contains the Point
 *	NOTES:		
 *
 *************************************************************************/
 
 	function containsPoint(element, xValue, yValue)
	{
		var objectUpperLeft 	= new Array(getElementX(element), getElementY(element));
		var objectUpperRight	= new Array(getElementX(element) + getWidth(element), getElementY(element));
		var objectLowerLeft		= new Array(getElementX(element), getElementY(element) + getHeight(element));
		var objectLowerRight	= new Array(getElementX(element) + getWidth(element), getElementY(element) + getHeight(element));
		
		/*var temp = "objectUpperLeft: (" + objectUpperLeft[0] + ", " + objectUpperLeft[1] + ")\n";
		temp += "objectUpperRight: (" + objectUpperRight[0] + ", " + objectUpperRight[1] + ")\n";
		temp += "objectLowerLeft: (" + objectLowerLeft[0] + ", " + objectLowerLeft[1] + ")\n";
		temp += "objectLowerRight: (" + objectLowerRight[0] + ", " + objectLowerRight[1] + ")\n\n";
		temp += "Point: (" + xValue + ", " + yValue + ")\n";
		*/
		//alert(temp);
		
		if ((xValue > objectUpperLeft[0]) & (yValue > objectUpperLeft[1]) &
			(xValue < objectUpperRight[0]) & (yValue > objectUpperLeft[1]) &
			(xValue > objectLowerLeft[0]) & (yValue < objectLowerLeft[1]) &
			(xValue < objectLowerRight[0]) & (yValue < objectLowerRight[1]))
		{
			
			return true;	
		}
		else
		{
			
			return false;	
		}
	}
	
 /* Contains  Mouse Point * *******************************************************
 *
 *	TAKES:		Element
 * 	RETURNS:	True if the Element contains the Mouse Point
 *	NOTES:		- Requires that the "getMousePosition" function be set to the
 *				"onmousemove" event trigger.
 *				- This function will often be used in conjunciton with the 
 *				"onmouseout" event, which seems to fire on the last pixel of the
 *				element, so that the mouse cursor is actually technically inside
 *				the element when this event fires (along the left and bottom edges).
 *				As a result this function accounts for this 1 pixel discrepancy when
 *				the mouse cursor is exiting the element along the left and bottom
 *				edges.
 *
 *************************************************************************/
 
 	function containsMousePoint(element)
	{
		var mouseOffsetX		= 0;
		var mouseOffsetY		= 0;
		var elementUpperLeft 	= new Array(getElementX(element), getElementY(element));
		
		if (mousePositionX > elementUpperLeft[0])
		{
			mouseOffsetX = 1
		}
		
		if (mousePositionY > elementUpperLeft[0])
		{
			mouseOffsetY = 1
		}
		
		return containsPoint(element, mousePositionX + mouseOffsetX, mousePositionY + mouseOffsetY);
	}
	
 /* Contains  Element * *******************************************************
 *
 *	TAKES:		Element Source, Element Test
 * 	RETURNS:	True if the Element contains the Point
 *	NOTES:		
 *
 *************************************************************************/
 
 	function containsElement(elementSource, elementTest)
	{
		var testUpperLeftX	= getElementX(elementTest);
		var testUpperLeftY	= getElementY(elementTest);
		
		var testUpperRightX	= testUpperLeftX + getWidth(elementTest);
		var testUpperRightY	= testUpperLeftY;
		
		var testLowerLeftX	= testUpperLeftX;
		var testLowerLeftY	= testUpperLeftY + getHeight(elementTest);
		
		var testLowerRightX	= testUpperRightX;
		var testLowerRightY	= testLowerLeftY;
		
		
		if ( containsPoint(elementSource, testUpperLeftX, testUpperLeftY) ||
			 containsPoint(elementSource, testUpperRightX, testUpperRightY) ||
			 containsPoint(elementSource, testLowerLeftX, testLowerLeftY) ||
			 containsPoint(elementSource, testLowerRightX, testLowerRightY) )
		{
			return true;	
		}
		else
		{
			return false;	
		}
		
	}
			 

