var fufi = (function ( $ ) {
	var fufiPublic = {};

	function getAjaxUrl ( url, useXhrSubDomain ) {
		useXhrSubDomain = useXhrSubDomain || false;

		if ( useXhrSubDomain === true ) {
			return 'http://xhr.' + window.location.hostname + '/' + url;
		} else {
			return 'http://' + window.location.hostname + '/' + url;
		}
	};

	fufiPublic.ajax = function ( url, options, callback ) {
		url = getAjaxUrl ( url );
		options.url = url;
		options.success = callback;

		return $.ajax ( options );
	};

	fufiPublic.getJson = function ( url, parameters, callback ) {
		return $.getJSON (
			getAjaxUrl ( url ),
			parameters,
			function ( response ) {
				callback ( response );
			}
		);
	};

	fufiPublic.getJsonp = function ( url, parameters, callbackName ) {
		return $.ajax ( {
			url: getAjaxUrl ( url, true ),
			data: parameters,
			dataType: 'jsonp',
			jsonp: 'callback',
			jsonpCallback: callbackName
		} );
	};

	fufiPublic.postJson = function ( url, parameters, callback ) {
		return $.post (
			getAjaxUrl ( url ),
			parameters,
			function ( response ) {
				if ( callback !== undefined ) {
					callback ( response );
				}
			},
			'json'
		);
	};

	fufiPublic.get = function ( url, parameters, callback ) {
		return $.get (
			getAjaxUrl ( url ),
			parameters,
			function ( response ) {
				if ( callback !== undefined ) {
					callback ( new AjaxResponse ( response ) );
				}
			}
		);
	};

	fufiPublic.post = function ( url, parameters, callback ) {
		return $.post (
			getAjaxUrl ( url ),
			parameters,
			function ( response ) {
				if ( callback !== undefined ) {
					callback ( new AjaxResponse ( response ) );
				}
			}
		);
	};

	fufiPublic.postMessage = function ( url, parameters, messageDiv, callback ) {
		return fufiPublic.post (
			url,
			parameters,
			function ( response ) {
				ShowMessage ( messageDiv, response );

				if ( callback !== undefined ) {
					callback ( response );
				}
			}
		);
	};

	fufiPublic.getMessage = function ( url, parameters, messageDiv, callback ) {
		return fufiPublic.get (
			url,
			parameters,
			function ( response ) {
				ShowMessage ( messageDiv, response );

				if ( callback !== undefined ) {
					callback ( response );
				}
			}
		);
	};

	fufiPublic.nl2Br = function ( str ) {
		return str.replace ( /(\r\n|\r|\n)/g, '<br />' );
	};

	fufiPublic.stripSlashes = function ( str ) {
		str = str.replace ( /\\'/g, '\'' );
		str = str.replace ( /\\"/g, '"' );
		str = str.replace ( /\\\\/g, '\\' );
		str = str.replace ( /\\0/g, '\0' );

		return str;
	};

	fufiPublic.addHttp = function ( str ) {
		if ( str.substr ( 0, 7 ) !== 'http://' && str.substr ( 0, 8 ) !== 'https://' ) {
			str = 'http://' + str;
		}

		return str;
	}

	return fufiPublic;
} ( jQuery ) );

function AjaxResponse ( response ) {
	this.response = response;

	this.isError = function () {
		if ( this.response.substr ( 0, 1 ) == '1' ) {
			return true;
		} else {
			return false;
		}
	}

	this.getContent = function () {
		return this.response.substr ( 2 );
	}

	this.getRawContent = function () {
		return this.response;
	}
}

function JsonpResponse ( response ) {
	this.response = response;

	this.isError = function () {
		return this.response.error;
	}

	this.getContent = function () {
		return this.response.data;
	}

	this.getRawContent = function () {
		return this.response;
	}
}

$.fn.insertAtCaret = function ( myValue ) {
	return this.each ( function () {
		//IE support
		if ( document.selection ) {
			this.focus();
			sel = document.selection.createRange();
			sel.text = myValue;
			this.focus();
		} else if ( this.selectionStart || this.selectionStart == '0' ) {
			var startPos = this.selectionStart;
			var endPos = this.selectionEnd;
			var scrollTop = this.scrollTop;
			this.value = this.value.substring ( 0, startPos ) + myValue + this.value.substring ( endPos, this.value.length );
			this.focus();
			this.selectionStart = startPos + myValue.length;
			this.selectionEnd = startPos + myValue.length;
			this.scrollTop = scrollTop;
		} else {
			this.value += myValue;
			this.focus();
		}
	} );
};
