(function($){
	$.fn.extend({
		ConfirmDialog: function(opts) {
			this.live('click', function(e) {
				var $this = $(this),
				obj = $this.ParseClass(true);
				if ($this.is(":radio")) {
					if ($this.data("isChecked")) {
						return;
					}
					$.ConfirmDialog(obj, function(e) {
						$("input[name='" + $this.attr("name") + "']").data("isChecked", false);
						$this
						.attr("checked", true)
						.data("isChecked", true)
						.trigger("change", "confirmed");
					});
					$("input[name='" + $this.attr("name") + "']").each(function() {
						if ($.data(this, "isChecked")) {
							this.checked = true;
						}
					});
				} else if ($this.is(":checkbox")) {
					if ($this.hasClass("uncheck")) {
						if ($this.attr("checked")) {
							return;
						}
						$.ConfirmDialog(obj, function(e) {
							$("input[name='" + $this.attr("name") + "']");
							$this
							.attr("checked", false)
							.trigger("change", "confirmed");
						});
					} else {
						if (!$this.attr("checked")) {
							return;
						}
						$.ConfirmDialog(obj, function(e) {
							$("input[name='" + $this.attr("name") + "']").data("isChecked", false);
							$this
							.attr("checked", true)
							.data("isChecked", true)
							.trigger("change", "confirmed");
						});
					}
				} else {
					$.ConfirmDialog(obj, function(e) {
						var href = $this.attr("href");
						if ($this.data("onclick")) {
							$this.data("onclick")();
						} else if (href.substr(0, 11) == "javascript:") {
							eval(href);
						} else {
							if ($this.attr("target") == "_blank") {
								window.open(href);
							} else {
								window.location = href;
							}
						}
					});
				}
				return false;
			});
			var _this = this;
			$(function() {
				var names = [];
				$(_this.selector).each(function() {
					var $this = $(this);
					if ($this.is(":radio")) {
						if ($.inArray($this.attr("name"), names) == -1) {
							names.push($this.attr("name"));
						}
					} else if (this.onclick) {
						$this.data("onclick", this.onclick);
						this.onclick="";
					}
				});
				for (var i=0, name; name = names[i]; i++) {
					(function(name) {
						var inputs = $("input[name='" + name + "']").change(function(e, confirmed) {
							if ($(this).hasClass("confirmDialog") && confirmed !== "confirmed") return;
							inputs.data("isChecked", false);
							$.data(this, "isChecked", true);
						}).each(function() {
							$.data(this, "isChecked", this.checked);
						});
					})(name);
				}
			});
			return this;
		}
	});
	$.ConfirmDialog = function(opts, func) {
		if (!opts instanceof Object) {
			opts = {};
		}
		var title = (typeof(opts.confirmTitle) != "undefined")? opts.confirmTitle : 'Confirm',
		message = opts.confirmMessage || 'Are you sure you want to do this?',
		cancelText = (opts.cancelText || opts.cancelText === false) ? opts.cancelText : 'Cancel',
		continueText = opts.continueText || 'Confirm',
		html = '<div title="' + title + '">'+
			'<p>' + message + '</p>'+
			'<div class="buttons">'+
				'<span class="button primary">'+
					'<span>'+
						'<strong><input value="' + continueText + '" type="submit" value="' + continueText + '" /></strong>'+
					'</span>'+
				'</span>';
		html += ((cancelText !== false)? '<span class="button secondary close"><span><a href="javascript:void(0);">' + cancelText + '</a></span></span>' : '');
		html +=
			'</div>'+
		'</div>';
		var dialog = $(html).dialog({
			close: function() {
				dialog.dialog('destroy');
			},
			modal: true
		});
		if (typeof(opts.cancelClick) === "function") {
			dialog.find("span.secondary").click(function() {
				opts.cancelClick();
			});
		}
		dialog.find("span.primary").click(function() {
			if (typeof(func) === "function") {
				func();
			}
			setTimeout(function() {
				dialog.dialog("close");
			}, 1);
			return false;
		});
	};
	$("a.confirmDialog, input.confirmDialog").ConfirmDialog();
})(jQuery);
