// This Feature has gone a long time unused, and should be removed
// from the site.

// Our catalog has tens of thousands of items, and we want
// to be able to show you the items you'll like the most.
// This code helps us guess what you might enjoy on our site.

// depends on globalv2.js

var ifdef = function(tryThis, orThis) {
	var ret = tryThis;
	
	if('undefined' == typeof ret) { ret = orThis; }
	
	return ret;	
}//ifdef()

///////////////////////////////////////////////////////////

// CONSTRUCTOR for a SmallMemory object, that tries to keep
// only the highest scoring values in a fixed number of cells.
// Bound to a Cookie named aCookieName
var SmallMemory = function(aMaxSize, aCookieName) {
	this._maxSize    = ifdef(aMaxSize, 10);
	this._cookieName = ifdef(aCookieName, 'jtv_memory');
	this._rememberFor = 2;
}//SmallMemory Ctor


SmallMemory.prototype = {
	
	// returns an array of values and scores, with the highest
	// scores first.
	values_and_scores: function() {
		var ret = new Array();
		var cookieString = readCookie(this._cookieName);

		if(null != cookieString) {
			var pairs = cookieString.split("+");
			for(var i=0; i<pairs.length; i++) {
				var k_v = pairs[i].split(',');
				ret[ret.length] = k_v;
			}//for
			
			// sort by decending score
			ret.sort(function(a,b) { return (b[1] - a[1]); });
		}//if
		
		return ret;
	},//values_and_scores()
	
	// Stores an new value, possibly discarding an existing value.
	remember: function(aValue, anOptionalScore) {
		
		var use_score = ifdef(anOptionalScore, 1);
		
		var old_memory = this.values_and_scores();
		var pluck_ret = this._pluckFromMemory(old_memory, aValue);

		var new_memory = pluck_ret.remaining;
		var new_cell  = ifdef(pluck_ret.plucked, [aValue, 0]);
		new_cell[1] = (new_cell[1] - 0) + (use_score - 0);
		
		if(this._maxSize <= new_memory.length) {
			this._trimMemory(new_memory, (this._maxSize - 1));
		}
		
		new_memory[new_memory.length] = new_cell;		
		
		this._storeMemory(new_memory);
	},//remember()
	
	// Removes a value from the memory, and returns the cell
	// associated with that value (if one exists)
	forget: function(aValue) {
		var old_memory = this.values_and_scores();
		
		var pluck_ret = this._pluckFromMemory(old_memory, aValue);
		
		this._storeMemory(pluck_ret.remaining);
		return pluck_ret.plucked;
	},//forget()
	
	///////////////////////////////////////////////////////////////////
	
	
	// returns a cell corresponding to aValue, 
	// and a new array with the cell corresponding to aValue removed.
	_pluckFromMemory: function(aMemoryArray, aValue) {
		var new_memory = [];
		var was_plucked; // = undefined
		
		for(var i=0; i < aMemoryArray.length; i++) {
			var old_cell = aMemoryArray[i];
			if(old_cell[0] == aValue) {
				was_plucked = ifdef(was_plucked, [aValue, 0]);
				was_plucked[1] = (was_plucked[1] - 0) + (old_cell[1] - 0);
			}
			else {
				new_memory[new_memory.length] = old_cell;
			}
		}//for
		
		return { plucked: was_plucked, remaining: new_memory };
	},//_pluckFromMemory()
	
	// Requires aMemoryArray to be sorted in order of best to worst elements.
	// Does *not*, in general, just lop off the tail of the list- lops off a
	// randomly selecteted bit of the tail. Alters the given array in-place.
	//
	// This may be needlessly complex- I suspect a completely random selection
	// would behave almost the same in the long run...
	_trimMemory: function(aMemoryArray, aMaxSize, aClearThreshold) {
		var useThreshold = ifdef(aClearThreshold, 0.5);
	
		while(aMemoryArray.length > aMaxSize) {
			var zap_length = Math.floor(aMemoryArray.length * useThreshold);
			if(zap_length == 0) { zap_length = 1; } //when zap_length < (1/aMemoryArray.length).
			
			var zap_offset = (aMemoryArray.length - zap_length);
			var zap_index  = zap_offset + Math.floor(zap_length * Math.random());
						
			aMemoryArray.splice(zap_index, 1);
		}//if
		
		return;
	},//_trimMemory()
	
	// Write to cookie.
	_storeMemory: function(aMemoryArray) {
		if(0 == aMemoryArray.length) {
			eraseCookie(this._cookieName);
		}
		else { //memory not empty
			var pairs = [];
		
			for(var i=0; i< aMemoryArray.length; i++) {
				pairs[pairs.length] = aMemoryArray[i].join(",");
			}//for
		
			createCookie(this._cookieName, pairs.join("+"), this._rememberFor);
		}//else
	}//_storeMemory()
	
};//SmallMemory.prototype

//////////////////////////////////////////////////////////////

var refinementsMemory = new SmallMemory(10, "jtv_refinements");

///////////////////////////////////////////

	var nval_string = getURLArgs().N;
	if('undefined' != typeof nval_string) {
		var nvals = nval_string.split(/(?: |\+)/);
		for(var i=0; i< nvals.length; i++) {
			refinementsMemory.remember(nvals[i]);
		}//for
	}//if Nvals were found
	
////////////////////////////////////////////

var getRefinementPreferences = function() {

        if("undefined" != typeof deprecated) {
           deprecated("getRefinementPreferences@guess_preferences.js");
        }

	var vals_scores = refinementsMemory.values_and_scores();
	var ret = [];
	
	for(var i=0; i<vals_scores.length; i++) {
		ret[ret.length] = vals_scores[i][0];
	}
	
	return ret;
	
}//getRefinememtPreferences()

