/*
 * Brita.com Pitcher Display & Sort Functions
 *	
 * Updated: 11/13/2008
 * Bla
 */

// create a sub namespace for the pitcherSorting code
$brita.pitcherSorting = {};

/*
 * showPitchers(): show the appropriate pitchers, using the sortBy() function
 * to figure out which ones to display
 */
$brita.pitcherSorting.showPitchers = function() {
	// get settings from controls
	var pitcherSortBy = $('#pitcherSortBy').val();
	var pitcherShow = $('#pitcherShow').val();
	
	// pass to sort function and get the list of appropriate pitchers
	var pitchersList = $brita.pitcherSorting.sortPitchers(pitcherShow, pitcherSortBy);
	
	// hide all pitchers
	$('ul#products>li').hide();
	
	// assign the sort order ids and sort (using the TinySort jQuery plugin)
	for(var i=0; i < pitchersList.length; i++) {
		$('ul#products>li#pitcher_'+pitchersList[i]+'>span.sort').attr('id', i);
	}
 	$('ul#products>li').tsort("span[class=sort]", {attr:"id"});

	// redisplay only the pitchers that we want
	for(var i=0; i < pitchersList.length; i++) {
		$('ul#products>li#pitcher_'+pitchersList[i]).show();
	}
}

/*
 * sortPitchers(): takes and returns an array of products that should be displayed
 */
$brita.pitcherSorting.sortPitchers = function (pitcherShow, pitcherSortBy) {
	
	var desiredPitchers = {};
	var finalPitcherList = new Array();

	// is pitcherShow set? Do we need to filter the array?
	switch (pitcherShow) {
		case 'softgrip':
			var desiredPitchers1 = $brita.pitcherSorting.getPitchersWithProperty(libPitchers, "features.softgrip == true");
			//var desiredPitchers2 = $brita.pitcherSorting.getPitchersWithProperty(libPitchers, "features.contoured == true");
			desiredPitchers = jQuery.extend(desiredPitchers1, desiredPitchers2);
		break;
		case 'fliptop':
			desiredPitchers = $brita.pitcherSorting.getPitchersWithProperty(libPitchers, "features.fliptop == true");
		break;
		case 'reminder':
			var desiredPitchers1 = $brita.pitcherSorting.getPitchersWithProperty(libPitchers, "features.reminder == true");
			var desiredPitchers2 = $brita.pitcherSorting.getPitchersWithProperty(libPitchers, "features.ereminder == true");
			desiredPitchers = jQuery.extend(desiredPitchers1, desiredPitchers2);
		break;
		default:
			// get everything. Slight hack; we check "short_name != undefined" which returns true for every object
			desiredPitchers = $brita.pitcherSorting.getPitchersWithProperty(libPitchers, "short_name != undefined");
	}
	
	// now that we have an object with the pitchers that we want, convert it to an array so that we
	// can sort it. (Javascript can't sort objects)
	for (var index in desiredPitchers) {
		finalPitcherList.push([index, desiredPitchers[index]]);
	}
	
	// Apply custom sort functions
	switch (pitcherSortBy) {
		case 'alpha':
			finalPitcherList.sort(function (a,b) { return ((a[1].short_name > b[1].short_name) ? 1 : -1); } );
		break;
		case 'price':
			finalPitcherList.sort(function (a,b) { return (a[1].price - b[1].price); } );		
		break;
		default:
			// capacity is default sort order
			finalPitcherList.sort(function (a,b) { return (a[1].capacity - b[1].capacity); } );
	}
	
	// finally, convert the pitchers list into a simple array of short codes, which is what
	// the display functions actually want
	for (var index in finalPitcherList) {
		finalPitcherList[index] = finalPitcherList[index][0];
	}
	
	return finalPitcherList;
}

/* 
 * getPitchersWithProperty(): given an array of pitchers (e.g. from libPitchers), return
 * an array which contains only those pitchers that match a certain property. The property 
 * should be an entire string, e.g.:
 *   capacity > 6
 *   features.contoured == true
 *   price <= 30.00
 *
 */
$brita.pitcherSorting.getPitchersWithProperty = function(pitchers, property) {
	
	// object to hold the return values
	returnPitchers = {};
	
	for (var index in pitchers) {
		// check the given property on each pitcher
		if (eval('pitchers[index].'+ property)) {
			// if the criteria matches, push the name (short code) of the pitcher
			// onto the array to return
			returnPitchers[index] = pitchers[index];
		}
	}
	return returnPitchers;
}

/*
 * init(): initialize the pitcher sorting functionality. Should be called from
 * $(document).ready()
 *
 */
$brita.pitcherSorting.init = function() {
	// sort and filter on initial page load
	$brita.pitcherSorting.showPitchers();
	
	// and then attach listeners
	$('#pitcherSortBy').change($brita.pitcherSorting.showPitchers);
	$('#pitcherShow').change($brita.pitcherSorting.showPitchers);
	
	// all the calls to the jQuery bubble plugin, so that the popover bubbles come up
	for (var pitcherShortCode in libPitchers) {
		$('body').bubble({
		    'trigger' : '#pitcher_'+pitcherShortCode, // selector for the trigger element
		    'popup' : '#tt_'+pitcherShortCode, // selector for the actual bubble (within 'body')
		    'distance' : 20, // distance in px it will travel
		    'hideDelay' : 50, // time before hiding
		    'effectTime' : 250, // total time for effect
			'showDelay' : 500
  		});
	}
}

// initialize all functions on document ready
$(document).ready($brita.pitcherSorting.init);