(function($){
	jQuery.fn.extend({
		ReloadContent: function(newopts) {
			this.each(function() {
				var $this = $(this);
				if ($this.data("reloadContent")) {
					return;
				}
				$this.data("reloadContent", true);
				var obj = $this.ParseClass();
				if (!obj.selector) {
					if (window.console && console.error) {
						console.error('ReloadContent Error! Class is not setup correctly. Class should be setup as follows: {"url":"url_to_get_html_from","selector":"#id_of_replace_container"} ReloadContent will not work until this is fixed.');
					}
					return;
				}
				$this.change(function(e, type) {
					var val = $this.val();
					$(obj.selector).each(function() {
						var $this = $(this),
						_obj = $this.ParseClass(),
						url = (_obj.url? _obj.url : obj.url);
						if (url.indexOf("?") !== -1) {
							val = val.replace("?", "&");
						}
						url += val;
						
						if (type == "lazyload" && _obj.lazyLoad === false) {
							return;
						}
						
						$this.trigger("throb");
						
						$.get(url, function(data) {
							$this.trigger("unthrob");
							//inject the current HTML into the bottom of the page for use later
							if (obj.storeContent !== false && _obj.storeContent !== false) {
								$('<div></div>').css("display","none").append($this.children()).appendTo(document.body);
							}
							
							$this.html(data);
							// Do all the HTML updates
							$this.ReplacePlaceholders();
							// Initialize datatables
							$.dataTable.init();
							// Initialize tabs
							$.tabs.init();
							// Initialize datepickers
							$.datepicker.init();
							// Initialize lazyloader (Recursive lazyloading? Love it!)
							$.LazyLoad.init();
							// Initialize Graph
							if ($.Graph) {
								$.Graph.init();
							}
							// Initialize Reload Content
							$.ReloadContent.init();
							// Initialize Rounded Boxes
							if ($.RoundBox) {
								$.RoundBox.init();
							}
							
							$("a.FormDialog").FormDialog();
						});
					});
					
				});
				if (obj.lazyLoad == true) {
					$this.trigger("change", 'lazyload');
				}
			});
			return this;
		}
	});
	$.ReloadContent = {
		init: function(sel) {
			$(sel || "select.reloadContent").ReloadContent();
		}
	};
	$(document).ready(function() {
		$.ReloadContent.init();
	});
})(jQuery);
