(function($){
	$.fn.frameworkLoad = function(_arguments){
		return this.each(function(){
			var $this = typeof _arguments["element"] !== "undefined" ? _arguments["element"] : $(this);
			var type = _arguments["type"];
			var url = _arguments["url"];
			var data = make_query(_arguments["data"]);
			var callback = _arguments["callback"];
			var replace = typeof _arguments["replace"] !== "undefined" ? _arguments["replace"] : true;
			var browser_history = typeof _arguments["history"] !== "undefined" ? _arguments["history"] : false;
			var browser_history_title = typeof _arguments["history_title"] !== "undefined" ? _arguments["history_title"] : "";

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

			$.ajax({
				url: url,
				type: "POST",
				data: data,
				beforeSend: function(){
					$(".fwx-l").show();

					if(type == "form") $("input, textarea, select", $this).attr("disabled", "disabled");

					if(type == "a") $("nav a").removeClass("aa");

					if(	"beforeSend" in _arguments && $.isFunction(_arguments["beforeSend"])) _arguments["beforeSend"]();
				},
				success: function(_answer){
					var content = selector ? jQuery("<div>").append(_answer).find(selector) : _answer;

					if(replace){
						$this.fadeOut(200, function(){
							$this.replaceWith(content); $(".ajax").fadeIn(200);
						});
					}else{
						$this.children().not(".error").fadeOut(200, function(){
							$this.children(".error").hide();
							$this.children().not(".error").remove();
							$this.append(content);
							$(".ajax").fadeIn(200);
						});
					}

					if(browser_history){
						history.pushState({exists: true}, browser_history_title, url);
						if(browser_history_title != "") document.title = browser_history_title;

						$.ga(['_trackPageview', url.replace(/^[^\/]*(?:\/[^\/]*){2}/, "")])
					}

					if(type == "a") $('nav a[href="'+url+'"]').addClass("aa");

					if(	"success" in _arguments && $.isFunction(_arguments["success"])) _arguments["success"](_answer);
				},
				error: function(_jqXHR, _textStatus, _errorThrown){
					var error_class = "."+_jqXHR.responseText.toLowerCase().replace(/\s/g, "-");
					error_class = $(error_class).length == 0 ? ".generic" : error_class;

					if($(error_class).length > 0){
						if($(error_class).is("label")){
							$(error_class, $this).addClass("error").effect("bounce", {distance: 5, times: 5}, 100);
						}else{
							$(".error"+error_class, $this).effect("bounce", {distance: 5, effect: "show", times: 5}, 100);
						}

						$(".error", $this).not(error_class).each(function(){
							if($(this).is("label")){
								$(this).removeClass("error");
							}else{
								$(this).hide();
							}
						});
						$("html, body").scrollTop(0);
					}

					if(	"error" in _arguments && $.isFunction(_arguments["error"])) _arguments["error"](_jqXHR, _textStatus, _errorThrown);
				},
				complete: function(_jqXHR, _textStatus){
					$(".fwx-l").hide();

					if(type == "form") $("input, textarea, select", $this).removeAttr("disabled");

					window.setTimeout(function(){FB.Canvas.setSize({height: $("body").height()});}, 500);

					if(	"complete" in _arguments && $.isFunction(_arguments["complete"])) _arguments["complete"](_jqXHR, _textStatus);
				}
			});
		});
	}

	function make_query(_data)
	{
		var query_string = "";
		if(typeof _data !== "undefined"){
			if("serialize" in _data){
				query_string += _data.serialize();
			}else if("arraySerialize" in _data){
				query_string += array_to_query(_data.arraySerialize());
			}else{
				query_string += array_to_query(_data);
			}
		}
		return query_string+"&ajax=true";
	}

	function array_to_query(_array)
	{
		var query = "";
		for(var element in _array){
			query += "&"+element+"="+escape(_array[element]);
		}
		return query.substring(1);
	}
})(jQuery);
