/**
 * FloatAPI library for WebDDM, floats menus across the page.
 * 
 * @version: 1.0
 * @starteC: 12/21/2004
 * @copyright: Copyright (c) 2004-2006, Cortex Creations, All Rights Reserved
 * @website: www.cortex-creations.com/webddm
 * @license: GPL vs2.0
 * @subversion: $IC: FloatAPI.WebDDM.js 135 2006-05-02 21:10:21Z josh $
 */

/**
 * The float object that controls menu floating.
 *
 * @param STRING domId ID of the DOM object
 * @param INTEGER offsetX Offset on the X axis from the left of the screen
 * @param INTEGER offsetY Offset on the Y axis from the top of the screen
 * @param INTEGER floatSpeed Speed at which floating occurs
 * @param BOOLEAN checkScrolling If set to TRUE, FloatAPI will automatically float the
 *  element to its position when the window is scrolled.
 *
 * @return OBJECT Returns the FloatAPI object
 *
 * @access PRIVATE
 */
function FloatAPI (domId, offsetX, offsetY, floatSpeed, checkScrolling)
{
	// Save the DOM ID for future use.
	this.domId = domId;
	// Save offsets
	this.offsetX = offsetX;
	this.offsetY = offsetY;
	// Save speed
	this.speed = floatSpeed;
	// Delay (in millisecond) between scroll checks
	this.scrollCheckDelay = 100;
	// Last scroll positions
	this.lastPageXOffset = false;
	this.lastPageYOffset = false;
	
	// Now, set up a timeout to check screen scrolling
	var instance = this;
	if (checkScrolling) {
		domLib_setTimeout(function ()
		{
			instance.checkScrolling();
		}, this.scrollCheckDelay);
	}
			
	return this;
}; // End function FloatAPI


/**
 * Method that checks if the document has scrolled at
 * all. If it has, float the element to its new position.
 *
 * @package FloatAPI
 *
 * @return VOID
 *
 * @access PRIVATE
 */
FloatAPI.prototype.checkScrolling = function ()
{
	// Get scroll positions
	if (typeof(document.body.scrollTop) != 'undefined') {
		pageYOffsetNow = document.body.scrollTop;
		pageXOffsetNow = document.body.scrollLeft;
	} else {
		pageYOffsetNow = window.pageYOffset;
		pageXOffsetNow = window.pageXOffset;
	}

	// Check - has the document been scrolled since the last
	// check?
	if ((this.lastPageXOffset != pageXOffsetNow) ||
		(this.lastPageYOffset != pageYOffsetNow))
	{
		// Document has scrolled; float element to new position
		if (!this.floating) {
			this.floatElement();
		}
	}

	// Update last positions
	this.lastPageXOffset = pageXOffsetNow;
	this.lastPageYOffset = pageYOffsetNow;
	
	// Timeout to recursively call this method again...
	var instance = this;
	domLib_setTimeout(function ()
	{
		instance.checkScrolling();
	}, this.scrollCheckDelay);
}; // End FloatAPI.prototype.checkScrolling


/**
 * Floats an element to its new position.
 *
 * @package FloatAPI
 *
 * @return VOID
 *
 * @access PRIVATE
 */
FloatAPI.prototype.floatElement = function ()
{
	// Get element
	domEl = WebDDM_getElement(this.domId);
	// Get scroll positions
	if (typeof(document.body.scrollTop) != 'undefined') {
		pageYOffsetNow = document.body.scrollTop;
		pageXOffsetNow = document.body.scrollLeft;
	} else {
		pageYOffsetNow = window.pageYOffset;
		pageXOffsetNow = window.pageXOffset;
	}
	
	// Get position that the element should be at when
	// we're done floating it.
	shouldBePosX = pageXOffsetNow + this.offsetX;
	shouldBePosY = pageYOffsetNow + this.offsetY;
	
	// Get current positions
	currentPosX = parseInt(domEl.style.left);
	currentPosY = parseInt(domEl.style.top);
	
	// Get new positions
	if (currentPosX > shouldBePosX) {
		floatGapX = currentPosX - shouldBePosX;
		newPosX = currentPosX - (floatGapX / (85 - this.speed));
	} else {
		floatGapX = shouldBePosX - currentPosX;
		newPosX = currentPosX + (floatGapX / (85 - this.speed));
	}
	if (currentPosY > shouldBePosY) {
		floatGapY = currentPosY - shouldBePosY;
		newPosY = currentPosY - (floatGapY / (85 - this.speed));
	} else {
		floatGapY = shouldBePosY - currentPosY;
		newPosY = currentPosY + (floatGapY / (85 - this.speed));
	}
	
	if (floatGapX > 0 || floatGapY > 0) {
		// Update position of element if the float gap is still wide
		this.floating = true;
		domEl.style.top = newPosY + 'px';
		domEl.style.left = newPosX + 'px';
		// Set new timeout
		var instance = this;
		domLib_setTimeout(function ()
		{
			instance.floatElement();
		}, (100 - this.speed) / (floatGapY > 25 ? 1 : 3));
	} else {
		// Float gap is narrow; stop floating
		domEl.style.top = shouldBePosY + 'px';
		domEl.style.left = shouldBePosX + 'px';
		this.floating = false;
	}
}; // End FloatAPI.prototype.floatElement

