(function($) {
	$.fn.contentPopup = function(options){

		var defaults = {
			url : null,
			width: 800,
			height: 400,
			/**
			 * if replace is true then replace the window, otherwise popup a dialog window
			 */
			replace : true,
			/**
			 * DOM ID of element to replace
			 */
			replaceID : "content",
			modal : false,
			resizable : false,
			draggable : false,
			/**
			 * if json is false then return only HTML from remote otherwise get only raw json code
			 */
			json : false
		}
		var options = $.extend(defaults, options);
		return this.each(function() {
			try {
				var dialogParams = jQuery.parseJSON('{'+$(this).attr("opts")+'}');
			} catch(e) {

			}
			$(this).click(function() {
				options = $.extend(options, dialogParams);
				var href = $(this).attr("href");
				var viewOnly = true; /* fetch the template view only */
				var url = href+"&viewOnly="+viewOnly;
				// if we want to replace the current content
				if(options.replace) {
					var d = $("#content").html("<p>Fetching data; Please wait</p>").show(1000);
				// else popup a dialog with the content
				} else {
					var d = $("<div></div>").appendTo("body");
					d.dialog({
						modal : options.modal,
						draggable : options.draggable,
						resizable : options.resizable,
						width : options.width,
						height: options.height,
						title : options.title,
						close: function() { d.dialog("destroy");}
					}).html("<p>Fetching data; Please wait</p>");
				}
				$.getJSON(url, function(data) {
					d.html(data)
				});
				return false;
			});
		});
	}
})(jQuery);
