Array.prototype.inArray = function(value) { var i; for ( i = 0; i < this.length; i++ ) { if ( this[i] == value ) { return true; } } return false; };
Array.prototype.removeItems = function(itemsToRemove) { if (!/Array/.test(itemsToRemove.constructor)) { itemsToRemove = [ itemsToRemove ]; } var j; for ( var i = 0; i < itemsToRemove.length; i++ ) { j = 0; while ( j < this.length) { if (this[j] == itemsToRemove[i]) { this.splice(j, 1); } else { j++; } } } }

function addRecentlyViewed(id) {
	
	// Name the cookie!
	cookie_name = 'recent';
	
	// How many product should we keep in history?
	limit = 9;
	
	// Read the cookie and reverse the order of the contents...
	recent = $.cookie(cookie_name) ? $.cookie(cookie_name).split(',').reverse() : [];
	
	// Remove the id from the array if it exists...
	if(recent.inArray(id)) { recent.removeItems(id); }
	
	// Add the id to the array...
	recent.push(id);
	
	// Reverse it...
	recent.reverse();
	
	// Check the limits...
	recent = recent.length > limit ? recent.splice(0, limit) : recent;
	
	// Write the cookie...
	$.cookie(cookie_name, recent.join(','), { path: '/', expires: 30 });
	
	// Debugging... console.log(recent);
		
}

