function setIE6DialogMaxSize(dialogName) {
    if (isIE6()) {
        var windowHeight = self.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
        var dialog = $("#"+dialogName);
        var dialogHeight = dialog.height();

        var maxDialogHeight = windowHeight - verticalDialogOffset -50;
        if (dialogHeight > maxDialogHeight) {
            dialog.height(maxDialogHeight);
        }

        if (dialog.attr("scrollHeight") > dialog.attr("clientHeight")) {
            dialog.css("margin-right", "16px");
        } else {
            dialog.css("margin-right", "0");
        }
    }
}

var verticalDialogOffset = 50;

function getVerticalScrollOffset() {
    return self.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
}

function isIE6() {
    return $.browser.msie && $.browser.version.indexOf("6.")==0;
}

(function() {
	var scrollLock=function(e) {
        var verticalScrollOffset = getVerticalScrollOffset();
        $("#"+e.data.dialogId).parent().css("top",(verticalScrollOffset + e.data.verticalDialogOffset)+"px");
	};


    var defaults = {
        title: "dialog test",
    	dialogClass: "defaultDialogStyle",
    	name: "dialogId" };

    var dialogDefaults = {
			bgiframe: true,
	        autoOpen: false,
	        modal: true ,
	        open: function() {
				var windowHeight = self.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;

				var maxDialogHeightOption = $(this).dialog("option","maxHeight");
                var minDialogHeightOption = $(this).dialog("option","minHeight");
                var dialogPosition = $(this).dialog("option","position");

                if (!isNaN(dialogPosition[1])) {
                    verticalDialogOffset =dialogPosition[1];
                }

                var maxDialogHeight = windowHeight - verticalDialogOffset - 70;

                if (maxDialogHeightOption > 0 && maxDialogHeightOption < maxDialogHeight) {
                    maxDialogHeight = maxDialogHeightOption;
                }

                if (minDialogHeightOption > maxDialogHeight) {
                    $(this).dialog("option","minHeight", maxDialogHeight);
                }

                $(this).css("max-height", maxDialogHeight + "px");

                // voodoo dance for Internet Explorer 6.x. hide all listboxes
				// so they don't end up showing through in our dialogs
				// also IE6 cannot stand "position: fixed" so we need a clunkier
				// way to do this.
				if (isIE6()) {
					$("select").hide();
                    $(window).bind("scroll",{verticalDialogOffset : verticalDialogOffset, dialogId: $(this).attr("id")}, scrollLock);
				} else {
                    $(this).parent().css("position", "fixed");
                    $(this).parent().css("top", verticalDialogOffset  + "px");

                }
	        },
	        beforeClose: function() {
	        	//$(this).dialog("option","maxHeight",1000);
	        	// voodoo dance for Internet Explorer 6.x. make listboxes visible again
	        	if (isIE6()) {
					$("select").show();
					$(window).unbind("scroll",scrollLock);
				}
	        }
     };

    var settings = {};
    var dialogSettings = {};


    $.createDialog = function(dialogName,dialogOptions,options) {
        $.extend(settings, defaults, options);
        $.extend(dialogSettings, dialogDefaults, dialogOptions);

        $("body").prepend("<div id=\""+dialogName+"\" style=\"background-color: #eeeeee;\" onresize=\"setIE6DialogMaxSize('" + dialogName + "');\"></div>");
        $("#"+dialogName).dialog(dialogSettings);
	}
})(jQuery);
