/*
 * FormDialog
 *
 * Depends:
 *	ui.core.js
 *	ui.draggable.js
 *	ui.resizable.js
 *  ui.dialog.js
 */

(function($) {
	return;
	$.fn.extend({
		FormDialog: function(href, exts) {
			var dialog = false,
			createDialog = function(href, exts) {
				
				if (dialog === false) {
					dialog = $('<div title="Loading"><div class="loading"><img src="/i/loading.gif" alt="" /> Loading...</div></div>').dialog({
						dialogClass: (exts && exts.dialogClass) ? exts.dialogClass : '',
						bgiframe: true,
						modal: true,
						resizable: false,
						width: (exts && exts.dialogWidth) ? exts.dialogWidth : 450,
						position: ['center','middle']/*,
						/*open: function() {
							$("object").css("visibility", "hidden");
						},
						close: function() {
							$("object").css("visibility", "");
						}*/
					});
				} else {
					dialog.html('<div class="loading"><img src="/i/loading.gif" alt="" /> Loading...</div>');
					dialog.dialog("option", "title", "Loading");
				}
				dialog.find(".close").live("click", function(e) {
					if($(this).parents("div.ui-dialog-content")[0] != dialog[0]) {
						return; // cancel the event if it is not coming from within the active dialog
					}
					dialog.dialog('close');
					$("object", dialog).remove();
					return false;
				});
				
				$.get(href, null, htmlReturn, "html");
				dialog.dialog("open");
			},
			htmlReturn = function(data) {
				var rootNode = $(data);
				//dialog.html(rootNode.html()); /* IE didn't like this. Was outputting <form /> */
				dialog.html(data);
				$("form", dialog).bind("submit", function(e) {
					var form = $(this),
					str = form.serialize();
					
					$.post(form.attr("action"), str, htmlReturn, "html");
					e.preventDefault()
				});
				//$("select.StyledSelect", dialog).StyledSelect();
				if (typeof(rootNode.attr("title")) != "undefined") {
					if (rootNode.attr("title") == "") {
						var dialogClass = dialog.dialog('option', 'dialogClass');
						dialog.dialog('option', 'dialogClass', dialogClass + ' noTitle');
					}
					dialog.dialog('option', 'title', rootNode.attr("title"));
				}
				if (typeof(rootNode.attr("autoclose")) != "undefined") {
					setTimeout(function() {
						dialog.dialog('destroy');
					}, rootNode.attr("autoclose"));
				}
				if (typeof(rootNode.attr("redirect")) != "undefined") {
					var redirectTimer = 2000;
					if (typeof(rootNode.attr("redirectTimer")) != "undefined") {
						redirectTimer = rootNode.attr("redirectTimer");
					}
					setTimeout(function() {
						window.location = rootNode.attr("redirect");
					}, redirectTimer);
				}
				dialog.dialog("close").dialog("open");
				
			}
			if (href) {
				createDialog(href, exts);
			} else {
				this.live("click", function(e) {
					var _this = $(this),
					exts = _this.ParseClass(),
					href = (exts && exts.ajaxHref) ? exts.ajaxHref : _this.attr('href');
					if (typeof(href) === "undefined") {
						return;
					}
					
					createDialog(href, exts);
					e.preventDefault();
				});
			}
			return this;
		}
	});
	
	$("a.formdialog, button.formdialog").FormDialog();
})(jQuery);
