function Filter(qs) { // optionally pass a querystring to parse
	this.params = {};
	
	if (qs == null) return;
	if (qs.length == 0) return;

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	//qs = qs.replace(/\+/g, ' ');
	qs = decodeURI(qs);

	var args = qs.split('~'); // parse out name/value pairs separated via ~
	
// split out each name=value pair
	for (var i = 0; i < args.length; i++) {
		var pair = args[i].split(':');
		var name = decodeURIComponent(pair[0]);
		
		var value = (pair.length==2)
			? decodeURIComponent(pair[1])
			: name;

		var values = value.split(';');
		
		this.params[name] = values;
	}
}

Filter.prototype.get = function(key, default_) {
	var value = this.params[key];
	return (value != null) ? value : default_;
}

Filter.prototype.contains = function(key) {
	var value = this.params[key];
	return (value != null);
}

Filter.prototype.set = function(key, value) {
	if (!this.params[key])
		this.params[key] = new Array();
	if (/^(above|below)/.test(value)) {
		this.params[key] = new Array();
	}
	if (-1 == jQuery.inArray(value, this.params[key]))
		this.params[key].push(value);
}

Filter.prototype.unset = function(key, value) {
	if (this.params[key] != null) 
		this.params[key] = jQuery.grep(this.params[key], function (a) { return a != value });
	if (this.params[key].length ==0) {
		this.params[key] = null;
	}
}
Filter.prototype.as_query = function() {
	var q = new Array();
	var vset = {};
	jQuery.each(this.params, function(k, v) {
		if (v != null)
			q.push(k + ':' + v.join(';'));
	});
	return encodeURIComponent(q.join('~'));
}


function filter_clicked(filter_id, tagname, tagvalue) {
	sp = $(filter_id);
	var qs = new Querystring();
	var fn = qs.get('fxn');
	var filter = new Filter(fn);
	if ('bold' == sp.css('font-weight')) {
		sp.css('font-weight', 'normal');
		filter.unset(tagname, tagvalue);
	}
	else {
		sp.css('font-weight', 'bold');
		filter.set(tagname, tagvalue);
	}
	qs.set('fxn', filter.as_query());
	if (location.pathname != '/gps-comparison/') {
		// <protocol>//<host>[:<port>]/<pathname>[<hash>][<search>] 
		var href = location.protocol + '//' + location.host + '/gps-comparison/?' + qs.as_query();
		location = href;
	} 
	else {
		location.search = qs.as_query();
	}
}
