jQuery.fn.extend({
	loadWithScripts: function( url, params, callback ) {
		if ( typeof url !== "string" ) {
			return this;

		// Don't do a request if no elements are being requested
		} else if ( !this.length ) {
			return this;
		}

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

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

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

			// Otherwise, build a param string
			} else if ( typeof params === "object" ) {
				params = jQuery.param( params, jQuery.ajaxSettings.traditional );
				type = "POST";
			}
		}

		var self = this;

		// Request the remote document
		jQuery.ajax({
			url: url,
			type: type,
			dataType: "html",
			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
						jQuery("<div />")
							// inject the contents of the document in
							.append(res.responseText)

							// Locate the specified elements
							.find(selector) :

						// If not, just inject the full result
						res.responseText );
				}

				if ( callback ) {
					self.each( callback, [res.responseText, status, res] );
				}
			}
		});

		return this;
	},
	
	updateURLs: function( view_param, view_value, id_param, id_value ) {
		if ( !this.length ) {
			return this;
		}
		var url_loc = this.href;
		var updated_loc;
		if(url_loc.indexOf("page") >= 0 && url_loc.indexOf("ct=conmod") < 0) {
			if(url_loc.indexOf(view_param) < 0) {
				updated_loc = url_loc + "&" + view_param + "=" + view_value;
				if(id_param && id_value) {
					updated_loc = updated_loc + "&" + id_param + "=" + id_value;
				}
			} else {
				var url_tokens = url_loc.split("?");
				url_tokens[1] = url_tokens[1].split("&");
				for(i=0; i<url_tokens[1].length; i++) {
					if(url_tokens[1][i].indexOf(view_param) >=0) 
						url_tokens[1][i] = view_param + "=" + view_value;
					if(id_param && id_value) {
						if(url_tokens[1][i].indexOf(id_param) >=0) 
							url_tokens[1][i] = id_param + "=" + id_value;					
					}
				}
				url_tokens[1] = url_tokens[1].join("&");
				updated_loc = url_tokens.join("?");
			}
			this.href = updated_loc;
		}
		
		return this;
	}
});

(function ($) { 
	$.updateContentURLs = function(view_param, view_value, id_param, id_value) {
		$("a").each(function() {
			var url_loc = this.href;	
			var updated_loc;			
			if(url_loc.indexOf("page") >= 0 && url_loc.indexOf("ct=conmod") < 0) {
				if(url_loc.indexOf(view_param) < 0) {
					updated_loc = url_loc + "&" + view_param + "=" + view_value;
					if(id_param && id_value) {
						updated_loc = updated_loc + "&" + id_param + "=" + id_value;
					}
				} else {
					var url_tokens = url_loc.split("?");
					url_tokens[1] = url_tokens[1].split("&");
					for(i=0; i<url_tokens[1].length; i++) {
						if(url_tokens[1][i].indexOf(view_param) >=0) 
							url_tokens[1][i] = view_param + "=" + view_value;
						if(id_param && id_value) {
							if(url_tokens[1][i].indexOf(id_param) >=0) 
								url_tokens[1][i] = id_param + "=" + id_value;					
						}
					}
					url_tokens[1] = url_tokens[1].join("&");
					updated_loc = url_tokens.join("?");
				}
				this.href = updated_loc;
			}
		});	
	},
	
	$.submitTFCBookmarkForm = function(form_id, list_id, save_url_selector, preloader_name, new_category_id, categoriesJSON ) {
		$("div[name="+preloader_name+"]").fadeIn();
		$("#"+list_id).load(save_url_selector, 
							$("#"+form_id).serialize(), 
							function(){
								$("#"+form_id)[0].reset();
								$("#"+new_category_id).hide();
								$("div[name="+preloader_name+"]").fadeOut();
							}
					);
		return false;
	}


})(jQuery) ;
