/**
 * Create method like load with encode attribute to fix IE7 accentuation code problem
 */
J.fn.extend({
	loadEncode: function( url, encode, params, callback ) {
		if ( typeof url != 'string' )
			return this._load( url );

		var off = url.indexOf(" ");
		if ( off >= 0 ) {
			var selector = url.slice(off, url.length);
			url = url.slice(0, off);
		}

		callback = callback || function(){};

		// Default to a GET request
		var type = "GET";

		// If the second parameter was provided
		if ( params )
			// If it's a function
			if ( J.isFunction( params ) ) {
				// We assume that it's the callback
				callback = params;
				params = null;

			// Otherwise, build a param string
			} else {
				params = J.param( params );
				type = "POST";
			}

		var self = this;

		// Request the remote document
		var TIMEOUT = 2500;
		var TENTATIVA = 0;
		function ajaxLoadRepeat(){
			J.ajax({
				contentType: 'application/x-www-form-urlencoded; charset='+encode,  
				url: url,
				type: type,
				cache: false,
				dataType: "html",
				timeout: TIMEOUT,
				data: params,
				complete: function(res, status){
					// If successful, inject the HTML into all the matched elements
					if ( status == "success" || status == "notmodified" ){
						// See if a selector was specified
						self.html( selector ?
							// Create a dummy div to hold the results
							J("<div/>")
								// inject the contents of the document in, removing the scripts to avoid any 'Permission Denied' errors in IE
								.append(res.responseText.replace(/<script(.|\s)*?\/script>/g, ""))
								// Locate the specified elements
								.find(selector) :
							// If not, just inject the full result
							res.responseText );
							
							self.each( callback, [res.responseText, status, res] );
					} else {
						if(TENTATIVA < 5) {
							TIMEOUT = TIMEOUT + 2000;
							ajaxLoadRepeat();
						}
						TENTATIVA++;	
					}
				}
			});
		}
		
		ajaxLoadRepeat();
		
		return this;
	},

	serialize: function() {
		return J.param(this.serializeArray());
	},
	serializeArray: function() {
		return this.map(function(){
			return J.nodeName(this, "form") ?
				J.makeArray(this.elements) : this;
		})
		.filter(function(){
			return this.name && !this.disabled &&
				(this.checked || /select|textarea/i.test(this.nodeName) ||
					/text|hidden|password/i.test(this.type));
		})
		.map(function(i, elem){
			var val = jQuery(this).val();
			return val == null ? null :
				val.constructor == Array ?
					J.map( val, function(val, i){
						return {name: elem.name, value: val};
					}) :
					{name: elem.name, value: val};
		}).get();
	}
});