/*	jQuery.flXHRproxy 1.2.2 <http://flxhr.flensed.com/>
	Copyright (c) 2009 Kyle Simpson
	This software is released under the MIT License <http://www.opensource.org/licenses/mit-license.php>

	This plugin registers flXHR with the new jQuery XHR registry, so it can be chosen as a custom transport
	for Ajax communication. flXHR will work as a replacement of the native XHR, additionally allowing
	authorized cross-domain communication through flash's crossdomain.xml policy model.

	This plugin allows an author to register a set of flXHR configuration options to be tied to each URL
	that will be communicated with. Typical usage might be:

	jQuery.ajaxSetup({transport:'flXHRproxy'});
	jQuery.flXHRproxy.registerOptions('http://www.mydomain.com/',{xmlResponseText:false...});
	jQuery.flXHRproxy.registerOptions('http://rss.mydomain.com/',{xmlResponseText:true...});
	...
	jQuery.ajax({url:'http://www.mydomain.com/something.html'...});
	...
	jQuery.ajax({url:'http://rss.mydomain.com/feed.html'...});

*/

;(function( $ ){
	$.flXHRproxy = flensed.flXHR;	// make jQuery.flXHRproxy a reference alias to flensed.flXHR, for convenience
	var _opts = [], old_ajax = $.ajax;

	$.extend({	// extend the "success handler" so that XHRObj is passed along as third parameter, like all other frameworks
		ajax:function(s) {
			var old_success = s.success, my_xhr = null;
			s.success = function() {
				var args = $.makeArray(arguments);
				args.splice(2,0,my_xhr);
				if (typeof old_success === "function") old_success.apply(null,args);
			};
			my_xhr = old_ajax(s);
			return my_xhr;
		}
	});

	$.flXHRproxy.registerOptions = function(url,fopts) {	// call to define a set of flXHR options to be applied to a
															// matching request URL
		// set up flXHR defaults, if not already defined
		if (typeof fopts==="undefined"||fopts===null) fopts = {};
		if (typeof fopts.instancePooling==="undefined"||fopts.instancePooling===null) fopts.instancePooling = true;
		if (typeof fopts.autoUpdatePlayer === "undefined"||fopts.autoUpdatePlayer===null) fopts.autoUpdatePlayer = true;
		_opts.push(function(callUrl) {	// save this set of options with a matching function for the target URL
			if (callUrl.substring(0,url.length)===url) return fopts;
			else return null;
		});
	}
	$.xhr.register('flXHRproxy',function(as) {
		var tmp = null, useopts = null;
		if (as.async&&(as.type==="POST"||as.type==="GET")) {	// flXHR only supports async and GET/POST
			for (var i=0; i<_opts.length; i++) {	// loop through all registered options for flXHR
				if ((tmp=_opts[i](as.url))!==null) useopts = tmp;	// if URL match is found, use those options
			}
		}
		if (useopts !== null) {	// if any matching options were found, use them
			var old_onerror = useopts.onerror;
			useopts.onerror = function() {
				var errObj = arguments[0];
				var newErrObj = new Error(errObj);
				for (var i in errObj)
					if (errObj[i]!==Object.prototype[i]) newErrObj[i] = errObj[i];
				if (typeof old_onerror === "function") old_onerror.call(as,newErrObj.srcElement,"error",newErrObj);
				$.handleError.call(as,as,newErrObj.srcElement,"error",newErrObj);	// trigger error in jQuery
			};
			return new $.flXHRproxy(useopts);
		}
		else {	// else, fall back on standard XHR
			return $.xhr.registry['xhr'](as);
		}
	});
})(jQuery);
/**
 * jQuery.XHR
 * Copyright (c) 2008 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 8/7/2008
 *
 * @projectDescription Registry of XHR implementations
 *
 * @author Ariel Flesler
 * @version 1.0.0
 */
;(function( $ ){

	var as = $.ajaxSettings;

	$.xhr = {
		registry: {
			xhr: as.xhr
		},
		register:function( name, fn ){
			this.registry[name] = fn;
		}
	};

	// The built-in method is used by default
	// To set another one as default, use $.ajaxSetup({ transport:'my_xhr' })
	as.transport = 'xhr';

	// This handler is used instead, don't override it
	as.xhr = function(){
		return $.xhr.registry[ this.transport ]( this );
	};

})( jQuery );
