(function(win, doc) {
	var jsonp = 0;
	var ENDPOINT = "http://www.hornypharaoh.com/horny_connect/connect.php?callback=";
	// Used for trimming whitespace
	var rtrim = /^(\s|\u00A0)+|(\s|\u00A0)+$/g;

	function trim(text) {
		return (text || "").replace(rtrim, "");
	}

	function parseJSON(data) {
		if (typeof data !== "string" || !data) {
			return null;
		}

		// Make sure leading/trailing whitespace is removed (IE can't handle it)
		data = trim(data);

		// Make sure the incoming data is actual JSON
		// Logic borrowed from http://json.org/json2.js
		if (/^[\],:{}\s]*$/
				.test(data
						.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
						.replace(
								/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,
								"]").replace(/(?:^|:|,)(?:\s*\[)+/g, ""))) {

			// Try to use the native JSON parser first
			return window.JSON && window.JSON.parse ? window.JSON.parse(data)
					: (new Function("return " + data))();

		} else {
			// jQuery.error("Invalid JSON: " + data);
		}
	}
	var horny = {
		url:"http://www.hornypharaoh.com/horny_connect/connect.php",
		connect : function(callback,params) {
			var hook = "hornyconnect_" + (new Date()).getTime();
			var head = doc.getElementsByTagName("head")[0]
					|| doc.documentElement;
			var script = doc.createElement("script");
			var src = this.url+"?callback=" + hook;
			if (params) {
				src += "&"+params;
			}
			script.src = src;
			window[hook] = window[hook] || function(data) {

				callback(parseJSON(data));
				// Garbage collect
					window[jsonp] = undefined;

					try {
						delete window[hook];
					} catch (e) {
					}

					if (head) {
						head.removeChild(script);
					}
				};
			head.insertBefore(script, head.firstChild);

		}

	}
	window["HornyConnect"] = horny;

})(window, document);

