/**
 * Blik Results Manager
 * Manages products listings in search results. While 
 * BLIK.refreshSearchResults handles most filter events, functions
 * in this namespace handle some underlying rendering of the
 * results listing.
 *
 * Joe Bartlett, Electric Pulp <joe@electricpulp.com>
 */
BLIK.RM = {
	// Settings - change these values
	maxRows: 3, // maximum number of rows of results you want displayed

	// Other props - do not change these values
	isItemsClipped: false,

	initialize: function() {
		if ($('search-items') == null) return false; // Some pages don't have search results

		if (typeof $('search-items').down('li.search-item.visible', 1) != 'undefined') {
			BLIK.RM.cols = Math.floor($('search-items').getWidth() / ($('search-items').down('li.search-item.visible', 1).cumulativeOffset().left - $('search-items').down('li.search-item.visible').cumulativeOffset().left));
		} else {
			BLIK.RM.cols = 1;
		}
		BLIK.RM.maxItems = BLIK.RM.cols * BLIK.RM.maxRows;

		BLIK.RM.fixClears();

		// Determine if items must be clipped
		if ($$('#search-items li.search-item.visible').length > BLIK.RM.maxItems) {
			BLIK.RM.isItemsClipped = true;
			BLIK.RM.clipItems();
			$('bttn-view-all-products').observe('click', function(e) {
				Event.stop(e);
				BLIK.RM.openItems();
			});
		} else {
			$('bttn-view-all-products').hide();
		}
		
		BLIK.RM.updateCounts();
	},

/**
 * Functions to perform every time a filter is applied.
 */
	afterSearch: function() {
		BLIK.RM.fixClears();
		BLIK.RM.clipItems();
		BLIK.RM.updateCounts();
	},

/**
 * Clip height of items list to only show maxRows. Correct height
 * is established dynamically.
 */
	clipItems: function() {
		if (BLIK.RM.isItemsClipped) {
			if ($$('#search-items li.search-item.visible').length > BLIK.RM.maxItems) {
				var maxHeight = $('search-items').down('li.search-item.visible', BLIK.RM.maxItems).cumulativeOffset().top - $('search-items').down('li.search-item.visible').cumulativeOffset().top;
				$('search-items-outer').setStyle({ height:maxHeight+'px', overflow:'hidden' });
				$('bttn-view-all-products').show();
			} else {
				$('search-items-outer').setStyle({ height:'auto' });
				$('bttn-view-all-products').hide();
			}
		}
	},

/**
 * Fixes clearings to make sure items are always displayed neatly.
 * Especially important when filters are applied.
 */
	fixClears: function() {
		var i = 0;
		$$('#search-items li.search-item').each(function(el) {
			var prev = el.previous('li.clear-item');
			if (typeof prev != 'undefined') {
				el.removeClassName('clear');
				prev.removeClassName('clear').hide();
				if (el.hasClassName('visible')) {
					if (i%BLIK.RM.cols == 0) {
						el.addClassName('clear');
						prev.addClassName('clear').show();
					}
					i++;
				}
			}
		});
	},

/**
 * Open items list to its full size.
 */
	openItems: function() {
		if (BLIK.RM.isItemsClipped) {
			new Effect.Morph('search-items-outer', {
				afterFinish: function() { $('search-items-outer').setStyle({ height:'auto' }) },
				duration: 0.6,
				style: { height: $('search-items').getHeight()+'px' }
			});
		}
		BLIK.RM.isItemsClipped = false;
		BLIK.RM.updateCounts();
		$('bttn-view-all-products').hide();
	},

/**
 * Update #search-count-displayed and #serch-count-total to reflect
 * the total 
 */
	updateCounts: function() {
		var total = $$('#search-items li.search-item.visible').length;
		$('search-count-total').innerHTML = total;
		
		if (total>BLIK.RM.maxItems && BLIK.RM.isItemsClipped) $('search-count-displayed').innerHTML = BLIK.RM.maxItems;
		else $('search-count-displayed').innerHTML = total;
	}

};

document.observe('dom:loaded', BLIK.RM.initialize);
