// Quick function for Cedar Cycles, though #hashes aren't supported in URLs
// It'll do for now

jQuery.setQueryVar = function(obj, name, value, attr) {
	if (typeof obj == 'object' && typeof obj.tagName == 'string') {
		attr = attr || {
			'A': 'href',
			'IMG': 'src'
		}[obj.tagName];
		if (attr) {
			obj.setAttribute(attr, jQuery.setQueryVar(obj.getAttribute(attr), name, value));
		}
	} else {
		return new String(obj).replace(/(\?[^#]*)?$/, function(match) {
			return match.replace(/($|\?[^#]*)/, function(match) {
				var replaced = false;
				return '?' + jQuery.map(match.substr(1).split(/&/), function(pair) {
					if ((match = pair.match(/^([^=]*)=(.*)$/)) && decodeURIComponent(match[1]) == name) {
						pair = match[1] + '=' + encodeURIComponent(value);
						replaced = true;
					}
					return pair;
				}).join('&') + (replaced ? '' : encodeURIComponent(name) + '=' + encodeURIComponent(value));
			});
		});
	}
};

jQuery.fn.setQueryVar = function(name, value, attr) {
	this.each(function() {
		jQuery.setQueryVar(this, name, value, attr);
	});
	return this;
};
