var $E = function(selector, filter){return ($(filter) || document).getElement(selector);};
var $ES = function(selector, filter){return ($(filter) || document).getElements(selector);};

var products	=	{};
window.addEvent('domready', function() {
	products	=	new alva_products();
});

var alva_products	=	new Class({
	initialize: function()
	{
		this.set_product_bar_height();
	},
	
	set_product_bar_height: function()
	{
		var cont	=	$E('body.products #container');
		if(!cont) return false;
		
		var album	=	$E('body.products .album');
		if(!album) return false;
		
		var wcoords	=	window.getCoordinates();
		var ccoords	=	cont.getCoordinates();
		var acoords	=	album.getCoordinates();
		var wheight	=	wcoords.height;
		var scroll	=	this.get_scroll_position_y();
		var cheight	=	ccoords.height + $('top').getCoordinates().height + 200;			// add 200 to account for product thumbnails
		var atop	=	acoords.top + scroll;
		
		if((acoords.bottom - scroll) < wheight && album.getStyle('position') == 'absolute')
		{
			album.setStyles({
				position:	'',
				top:		''
			});
		}
		else if(atop <= cheight)
		{
			album.setStyles({
				position:	'absolute',
				top:		(cheight - 1) + 'px'
			});
		}
		
		this.set_product_bar_height.delay(10, this);
	},
	
	// ---------- code taken from http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html (thanks!!) ----------
	/**
	 * Get the Y scroll position of the window, cross-browser compatible. Works great.
	 * 
	 * Yo, big up for da SoftComplex mmmassive (http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html)
	 */
	get_scroll_position_y: function()
	{
		return this.f_filterResults (
			window.pageYOffset ? window.pageYOffset : 0,
			document.documentElement ? document.documentElement.scrollTop : 0,
			document.body ? document.body.scrollTop : 0
		);
	},
	
	/**
	 * http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html
	 */
	f_filterResults: function(n_win, n_docel, n_body)
	{
		var n_result = n_win ? n_win : 0;
		if (n_docel && (!n_result || (n_result > n_docel)))
			n_result = n_docel;
		return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
	}
	// ---------- end code taken from http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html ----------

});

