var FuzzyMatches = Class.create();
FuzzyMatches.prototype = {
	initialize: function(args) {
		this.webroot = args.webroot;
		this.weights = {
			1: args.var_1,
			2: args.var_2,
			3: args.var_3,
			4: args.var_4
		};
		for (var i=1; i<=4; i++) {
			eval('var fn =function(v){ nearMatches.setWeight(' + i + ', v); }');
			new Control.Slider('handle' + i, 'track' + i, {
				range: $R(0,4),
				sliderValue: eval('args.var_' + i),
				values: [0, 1, 2, 3, 4],
				onChange: fn
			});
		}
		this.matches = [];
	},
	add: function(args) {
//		Element.extend(args.row);
		args.score = this.getScore(args);
		this.matches.push(args);
	},
	setWeight: function(i, v) {
		this.weights[i] = v;
		this.show();
	},
	getScore: function(i) {
		return ((i.score_1 * this.weights['1']) + (i.score_2 * this.weights['2']) + (i.score_3 * this.weights['3']) + (i.score_4 * this.weights['4']));
	},
	sort: function(a, b) {
		// get highest scoring elements
		return this.getScore(b) - this.getScore(a);
	},
	show: function() {
		this.matches.sort(this.sort.bind(this));
		var tbody = $("fuzzy_results").down('tbody');
		while(tbody.childNodes.length > 0) {
			tbody.removeChild(tbody.childNodes[0]);
		}
		for(var i=0; i<this.matches.length; i++) {
			if (i == 20) break;
			this.matches[i].row.className = (i==0) ? 'first' : '';
//			this.matches[i].row.down('.scoreholder').update(this.getScore(this.matches[i]));
			tbody.appendChild(this.matches[i].row);
		}
	}
}
