
// generic utility functions.
if (window.utils === undefined) {
	var utils = {};
}

utils.capitalize = function (input) {
	// capitalize the first letter of every word in the input string.

	return input.replace(/\b[a-z]/g, function () {
		return arguments[0].toUpperCase();
	});
};

// a javascript object used as a key value store.
// can find it's length and return it's keys.
utils.Dictionary = function () {};
utils.Dictionary.prototype = {};
utils.Dictionary.prototype.length = function () {
	var result = 0;
	var key;
	for (key in this) {
		if (this.hasOwnProperty(key)) {
			result += 1;
		}
	}
	return result;
};
utils.Dictionary.prototype.keys = function () {
	var result = [];
	var key;
	for (key in this) {
		if (this.hasOwnProperty(key)) {
			result.push(key);
		}
	}
	return result;
};

utils.query = function (argument, value) {
	// return an object of argument, value pairs from the current query string.
	// if given the optional argument and value, set that argument to that value and
	// return resulting query string.

	var i, result, pair;
	var query_string = location.search;

	// remove initial ?
	query_string = query_string.replace(/^\?/, '');

	result = {};
	// split and build object.
	query_string = query_string.split('&');
	for (i=0; i<query_string.length; i+=1) {
		if (query_string[i]) {
			pair = query_string[i].split('=');

			result[pair[0]] = decodeURIComponent(pair[1]);
		}
	}

	// if changing something.
	if (argument) {
		result[argument] = encodeURIComponent(value);

		// rebuild query strying and set.
		query_string = '?';
		for (i in result) {
			if (result.hasOwnProperty(i)) {
				query_string += i + '=' + result[i] + '&';
			}
		}
		// remove extra &
		query_string = query_string.substring(0, query_string.length-1);

		location.search = query_string;
	}
	
	return result;
};

