/**	postJSON
 *	Identical to $.getJSON
 *	@function
 *	@name $.postJSON
 */
$.postJSON = function(url, data, callback) {
	$.post(url, data, callback, "json");
};

/**	isEmptyObject
 *	@function
 *	@name $.isEmptyObject
 *	@return true if is an empty object
 */
$.isEmptyObject = function(obj) {
	if (!obj || typeof(obj) != "object") { return false; }
	for (var p in obj) { return false; }
	return true;
};

/**	Parse a URI
 *	@function
 *	@name $.parseUri
 *
 *	@param {string}	uri	URI to parse.
 *	@return	{string}	uri.anchor
 *	@return	{string}	uri.authority
 *	@return	{string}	uri.directory
 *	@return	{string}	uri.file
 *	@return	{string}	uri.host
 *	@return	{string}	uri.hostNoWWW
 *	@return	{string}	uri.password
 *	@return	{string}	uri.path
 *	@return	{string}	uri.port
 *	@return	{string}	uri.protocol
 *	@return	{string}	uri.query
 *	@return	{string}	uri.queryKey
 *	@return	{string}	uri.relative
 *	@return	{string}	uri.source
 *	@return	{string}	uri.user
 *	@return	{string}	uri.userInfo
 */
$.parseUri = function(str) {
	var o = {
		strictMode: false,
		key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
		q:	{
			name:	"queryKey",
			parser: /(?:^|&)([^&=]*)=?([^&]*)/g
		},
		parser: {
			strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
			loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
		}
	},
	m	= o.parser[o.strictMode ? "strict" : "loose"].exec(str),
	uri = {},
	i	= 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});
	
	uri["hostNoWWW"] = uri["host"].replace(new RegExp(/^www\./i), "");
	
	return uri;
};

/**	copyToClipboard
 *	@function
 *	@name $.copyToClipboard
 */
$.copyToClipboard = function (text2copy) {
	if (window.clipboardData) {
		window.clipboardData.setData ("Text",text2copy);
	} 
	else {
		var flashcopier = 'flashcopier';
		if ($('#'+flashcopier).length == 0) {
			$(document.body).append($('<div id="'+flashcopier+'"></div>'));
		}
		$('#'+flashcopier).html('');
		var divinfo = '<embed src="/script/util/clipboard.swf" FlashVars="clipboard='+escape (text2copy)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
		$('#'+flashcopier).html(divinfo);
	}
};

$.querystring = function(s) {
	var r = {};
	if (s) {
		var q = s.substring(s.indexOf('?') + 1); // remove everything up to the ?
		q = q.replace(/\&$/, ''); // remove the trailing &
		jQuery.each(q.split('&'), function() {
			var splitted = this.split('=');
			var key = splitted[0];
			var val = splitted[1];
			if (!val) return;
			// convert numbers
			if (/^[0-9.]+$/.test(val)) val = parseFloat(val);
			// convert booleans
			if (val == 'true') val = true;
			if (val == 'false') val = false;
			// ignore empty values
			if (typeof val == 'number' || typeof val == 'boolean' || val.length > 0) r[key] = val;
		});
	}
	return r;
};
