/**
 * This file contains all parts needed to make a bottom bar sticky
 * at a specific threshold.
 * 
 * @author Jean-Bernard Valentaten <troggy.brains@gmx.de>
 * @copyright 2009 Jean-Bernard Valentaten
 * @license GNU/GPL
 * @version 1.0
 */

/**
 * Makes a specified element sticky and installs a listener
 * that will keep the element sticky when window resizes.
 * 
 * @param {Element,String} elem The element that shall stick to bottom
 * @param {int} minTop The threshold that must be reached to be sticky
 */
function makeSticky(elem, minTop) {
	elem = $(elem);
	if (!elem) return;
	elem.store('sticky', true);
	
	stickToBottom(elem, minTop);
	Event.observe(window, 'resize', stickToBottom.curry(elem, minTop));
}

/**
 * Installs the styles needed to make element stick to bottom
 * of its viewport.
 * 
 * @param {Element,String} elem The element that shall stick to bottom
 * @param {int} minTop The threshold that must be reached to be sticky
 */
function stickToBottom(elem, minTop) {
	elem = $(elem);
	if (!elem) return;
	
	var sticky = !!elem.retrieve('sticky');
	var top = parseInt(elem.cumulativeOffset().top);
	
	if (sticky && top<minTop) {
		elem.setStyle({top: minTop+'px', bottom:''}).store('sticky', !sticky);
	} else if (!sticky && document.viewport.getHeight()>minTop) {
		elem.setStyle({top:'', bottom:'0px'}).store('sticky', !sticky);
	}
}



