/**
 * Crossbrowser DHTML Library
 * This file contains various functions that ease the handling of layers
 *
 * @author Christian Schwanke
 */

// ****************************************************************************
// HIGH-LEVEL FUNCTIONS *******************************************************
// ****************************************************************************

/**
 * Shows the layer specified by the layerId parameter
 * 
 * @param layerId the id of the layer
 */
function showLayer(layerId)
{
	setLayerVisibility(layerId, true);
}

/**
 * Hides the layer specified by the layerId parameter
 * 
 * @param layerId the id of the layer
 */
function hideLayer(layerId)
{
	setLayerVisibility(layerId, false);
}

/**
 * Toggles the layer's visibility
 * 
 * @param layerId the id of the layer
 */
function toggleLayer(layerId)
{
	setLayerVisibility(layerId, !getLayerVisibility(layerId));
}

/**
 * Moves the layer specified by the layerId parameter 
 * to the given coordinates. In order to move only in one direction,
 * either coordinate-parameter may be null.
 *
 * @param layerId The id of the layer to move
 * @param x The new x coordinate
 * @param y The new y coordinate
 */
function moveLayerTo(layerId, x, y)
{
	if (x != null)
	{
		setLayerLeft(layerId, x);
	}
	
	if (y != null)
	{
		setLayerTop(layerId, y);
	}
}

/**
 * Moves the layer specified by the layerId parameter 
 * relative to its current position.
 *
 * @param layerId The id of the layer to move
 * @param deltaX The amount to move in x direction
 * @param deltaY The amount to move in y direction
 */
function moveLayerBy(layerId, deltaX, deltaY)
{
	setLayerLeft(layerId, getLayerLeft(layerId) + deltaX);
	setLayerTop(layerId, getLayerTop(layerId) + deltaY);
}

/**
 * Places a layer on the position retrieved from the given anchor.
 * If the anchor's position does not match the required position, the offset
 * parameters can be used to correctly set the layer.
 * Finally the show parameter determines wether the layer should be set to 
 * visible after positioning.
 *
 * @param layerId The id of the layer to place/position
 * @param anchorName The name/id of the anchortag that is used to calculate the position
 * @param xOffset The offset in the x direction from the anchor's position
 * @param yOffset The offset in the y direction from the anchor's position 
 * @param show true if the layer should be immediatly displayed after positioning
 */
function placeLayer(layerId, anchorName, xOffset, yOffset, show)
{
	var x = parseInt(getLeftPosition(anchorName));
	
	var y = parseInt(getTopPosition(anchorName));
	
	if (xOffset)
	{
		x = (x + parseInt(xOffset));
	}
	
	if (yOffset)
	{
		y = (y + parseInt(yOffset));
	}
	//alert(x);
	//alert(y);
	setLayerLeft(layerId, x);
	setLayerTop(layerId, y);
	setLayerVisibility(layerId, show);
}




// ****************************************************************************
// CORE FUNCTION **************************************************************
// ****************************************************************************

/**
 * Displays an error stating that the provided layerId could not be resolved
 *
 * @param layerId The id of the layer that could not be resolved
 */
function throwLayerNotFound(layerId)
{
	/*alert("Layer '" + layerId + "' nicht gefunden.");*/
}

/**
 * Determines wether the layer with the given id is currently visible
 *
 * @param layerId The id of the layer
 * @return true if the layer is visible
 */
function getLayerVisibility(layerId)
{
	var obj = findLayer(layerId);	
	if (obj == null)
	{
		throwLayerNotFound(layerId);
	}
	
	if (document.all || document.getElementById)
	{
		state = (obj.style.visibility == "visible");
	}
	else if (document.layers)
	{
		state = (obj.visibility == "show");
	}

	return state;
}

/**
 * Sets the visibility of the layer with the given id
 *
 * @param layerId The id of the layer
 * @param state The new visibility state; true == visible
 */
function setLayerVisibility(layerId, state)
{
	var obj = findLayer(layerId);
	if (obj == null)
	{
		throwLayerNotFound(layerId);
	}
	
	if (state)
	{
		if (document.all || document.getElementById)
		{
			obj.style.visibility = "visible";
		}
		else if (document.layers)
		{
			obj.visibility = "show";
		}
	}
	else
	{
		
		if (document.all || document.getElementById)
		{
			obj.style.visibility = "hidden";
		}
		else if (document.layers)
		{
			obj.visibility = "hide";
		}
	}
}

/**
 * Retrieves the y position of the layer with the given id.
 * The returned value is the y position in pixel calculated from
 * the beginning of the page/document.
 *
 * @param layerId The id of the layer
 * @return The absolute y position of the layer
 */
 function getLayerTop(layerId)
{
	var obj = findLayer(layerId);
	if (obj == null)
	{
		throwLayerNotFound(layerId);
	}
	
	if (document.all)
	{
		return obj.style.pixelTop;
	}
	else if (document.getElementById)
	{
		return obj.style.top;
	}
	else if (document.layers)
	{
		return obj.top;
	}	
}

/**
 * Sets the y position of a layer. The value parameter must specify
 * the absolute position in pixel measured from the beginning of the page/document
 *
 * @param layerId The id of the layer
 * @param value The new absolute y position in pixel
 */
function setLayerTop(layerId, value)
{
	var obj = findLayer(layerId);
	if (obj == null)
	{
		throwLayerNotFound(layerId);
	}
	
	if (document.all)
	{
		obj.style.pixelTop = value;
	}
	else if (document.getElementById)
	{
		obj.style.top = value + "px";		
	}
	else if (document.layers)
	{
	 	obj.top = value;
	}
}	

/**
 * Retrieves the x position of the layer with the given id.
 * The returned value is the x position in pixel calculated from
 * the left of the page/document.
 *
 * @param layerId The id of the layer
 * @return The absolute x position of the layer
 */
function getLayerLeft(layerId)
{
	var obj = findLayer(layerId);
	if (obj == null)
	{
		throwLayerNotFound(layerId);
	}

	if (document.all)
	{
		return obj.style.pixelLeft;
	}
	else if (document.getElementById)
	{
		return obj.style.left;
	}
	else if (document.layers)
	{
		return obj.left;
	}		
}	
	

/**
 * Sets the x position of a layer. The value parameter must specify
 * the absolute position in pixel measured from the left of the page/document
 *
 * @param layerId The id of the layer
 * @param value The new x position in pixel
 */
function setLayerLeft(layerId, value)
{
	var obj = findLayer(layerId);
	if (obj == null)
	{
		throwLayerNotFound(layerId);
	}
		
	if (document.all)
	{
		obj.style.pixelLeft = value;		
	}
	else if (document.getElementById)
	{
		obj.style.left = value + "px";			
	}
	else if (document.layers)
	{
		obj.left = value;		
	}
}		
	
/**
 * Retrieves the height of a layer
 *
 * @param layerId The id of the layer
 * @return The height of the layer in pixel
 */
function getLayerHeight(layerId)
{
	var obj = findLayer(layerId);
	if (obj == null)
	{
		throwLayerNotFound(layerId);
	}
	
	if (document.all || document.getElementById)
	{
		return obj.offsetHeight;
	}
	else if (document.layers)
	{
		return obj.clip.height;
	}
}

function setLayerHeight(layerId, height)
{
	var obj = findLayer(layerId);
	if (obj == null)
	{
		throwLayerNotFound(layerId);
	}
	
	if (document.all)
	{
		
		return obj.style.height = height + "px";
	}
	else if (document.getElementById)
	{

		return obj.style.height = height + "px";
	}
	else if (document.layers)
	{
		return obj.clip.height = height;
	}
}

/**
 * Retrieves the width of a layer
 *
 * @param layerId The id of the layer
 * @return The width of the layer in pixel
 */
function getLayerWidth( layerId )
{
	var obj = findLayer(layerId);
	if (obj == null)
	{
		throwLayerNotFound(layerId);
	}
	
	if (document.all || document.getElementById)
	{
		return obj.offsetWidth;
	}
	else if (document.layers)
	{
		return obj.clip.width;
	}
}	
	
function setLayerWidth(layerId, width)
{
	var obj = findLayer(layerId);
	if (obj == null)
	{
		throwLayerNotFound(layerId);
	}
	
	if (document.all)
	{
		return obj.style.width = width + "px";
	}
	else if (document.getElementById)
	{
		return obj.style.width = width + "px";
	}
	else if (document.layers)
	{
		return obj.clip.width = width;
	}
}
/**
 * Retrieves a layer object by its id. This method is capable of finding nested layers as well
 * by recursivly looking through the different document levels.
 * If the layer is not found, null is returned. This method will not throw an error message since
 * a catching-mechanism is not available in all browsers.
 * It's up to the calling function to deal with a null value.
 *
 * @param layerId The id of the Layer to look for
 * @param parentObject The object to start from. This parameter is mainly used during recursion
 * 			and can be omitted most of the time. When looking for a layer in a document
 *			this parameter is not applicable
 * @return The layer object for the given id or null if the layer cannot be resolved
 */
function findLayer(layerId, parentObject)
{
	if (parentObject == null)
	{
		parentObject = document;
	}

	if (document.getElementById)
	{
		return (document.getElementById(layerId));
	}
	else if (document.all)
	{
		return (document.all[layerId]);
	}
	else if (document.layers)
	{
		if (parentObject.layers.length > 0)
		{
			for (var i=0; i<parentObject.layers.length; i++)
			{
				if (parentObject.layers[i].id == layerId)
				{
					return parentObject.layers[i];
				}
				
				var tempLayer = findLayer(layerId, parentObject.layers[i].document);
				if (tempLayer != null)
				{
					return tempLayer;
				}
			}
			return null;
		}
		return null;
	}
}

/**
 * Changes a layer's content
 * 
 * @param layerId The id of the layer to change
 * @param content The new content as String.
 */
function setContent(layerId, content)
{
	var obj = findLayer(layerId);
	if (obj == null)
	{
		throwLayerNotFound(layerId);
	}
	
	if (document.all || document.getElementById)
	{
		obj.innerHTML = content;
	}
	else if (document.layers)
	{
		obj.document.open();
		obj.document.clear();
		obj.document.write(content);
		obj.document.close();
	}
}

/**
 * Calculates the absolute y position of a named anchor tag. 
 * An anchor-Tag used with this method must provide both id and name attribute with identical values.
 * example: <a name="positionAnchor" id="positionAnchor">...</a>
 * This method is useful to dynamically retrieve an elements position in a page by adding an anchor-
 * tag to that element/block.
 *
 * @param anchorName The name/id of the anchor to look for
 * @return the absolute y position in pixel within the document
 */
function getTopPosition(anchorName)
{
	if (document.all)
	{			
		var anchorItem = document.all[anchorName];
		var summe = anchorItem.offsetTop;
		while ((anchorItem = anchorItem.offsetParent) != null)
		{
			summe += anchorItem.offsetTop;
		}
		return summe;
	}
	else if (document.getElementById)
	{
		var anchorItem = document.getElementById(anchorName);
		var summe = anchorItem.offsetTop;
		while ((anchorItem = anchorItem.offsetParent) != null)
		{
			summe += anchorItem.offsetTop;
		}
		return summe;
	}	
	else if (document.layers)
	{
		for (var i=0; i<document.anchors.length; i++) 
		{
			if (document.anchors[i].name == anchorName)
			{
				return document.anchors[i].y;
			}
		}
	}
	return 0;
}

/**
 * Calculates the absolute x position of a named anchor tag. 
 * An anchor-Tag used with this method must provide both id and name attribute with identical values.
 * example: <a name="positionAnchor" id="positionAnchor">...</a>
 * This method is useful to dynamically retrieve an elements position in a page by adding an anchor-
 * tag to that element/block.
 *
 * @param anchorName The name/id of the anchor to look for
 * @return the absolute x position in pixel within the document
 */
function getLeftPosition(anchorName)
{
	if (document.all)
	{
		var anchorItem = document.all[anchorName];
		var summe = anchorItem.offsetLeft;
		while ((anchorItem = anchorItem.offsetParent) != null)
		{
			summe += anchorItem.offsetLeft;
		}
		return summe;	
	}	
	else if (document.getElementById)
	{
		var anchorItem = document.getElementById(anchorName);
		var summe = anchorItem.offsetLeft;
		while ((anchorItem = anchorItem.offsetParent) != null)
		{
			summe += anchorItem.offsetLeft;
		}
		return summe;
	}		
	else if (document.layers)
	{
		for (var i=0; i<document.anchors.length; i++) 
		{
			if (document.anchors[i].name == anchorName)
			{
				return document.anchors[i].x;
			}
		}
		return 0;
	}
}	

function swap(which) {
    var i = $('#'+which);
    var s = i.attr('src');
    var p = s.split('_');
    if (p[2]=='off.jpg') i.src = p[0]+'_'+p[1]+'_on.jpg';
    else i.src = p[0]+'_'+p[1]+'_off.jpg';
}

