var dialogSet = new Object();
dialogSet.dialogMap = {};

dialogSet.getOrCreateDialog = function(dialogId, url, width, height) {
	var _dialog = dialogSet.getDialogById(dialogId);
	if(_dialog == null) {
    	_dialog = dialogSet.createDialog(dialogId, url, width, height);
	}
	return _dialog;
}

// Checks if the dialog identified with dialogId already exists in the dialogSet
dialogSet.dialogExists = function(dialogId) {
    if (dialogSet.dialogMap[dialogId]) {
        return true;
    } else {
        return false;
    }
}

// returns the Dialog JavaScript object that matches dialogId
dialogSet.getDialogById = function(dialogId) {
    if (dialogSet.dialogExists(dialogId)) {
        return dialogSet.dialogMap[dialogId];
    } else {
        return null;
    }
}

// Adds a new dialog into the map.
// parameter dialog:   the Dialog JavaScript object
dialogSet.addDialogToMap = function(dialog) {
    var dialogId = dialog.displayId;

    if (dialogSet.dialogExists(dialogId)) {
        alert("Internal Error: dialogSet.addDialog: either trying to add the same dialog again by mistake," +
                        " or trying to add another dialog with the same dialogId.");
        return false;
    }

    dialogSet.dialogMap[dialogId] = dialog;
}

// Generates the HTML (div's) and the Dialog object.
// Leaves the content blank so that showDialog() can pass to the same dialog different URL parameters each time.
dialogSet.createDialog = function(dialogId, url, width, height) {
    var divs = '';
    var w =  width * 1;
    var h = height * 1 + 19;
    
    divs += '    <div id="' + dialogId + 'Shadow" class="dialog-shadow" style="width: ' + width + 'px; height: ' + h + 'px;" onselectstart="window.event.returnValue=false;"></div>\n';
    divs += '    <div id="' + dialogId + 'Content" class="dialog-content" style="width: ' + width + 'px; height: ' + height + 'px;">\n';
    divs += '        <iframe id="' + dialogId + 'IF" name="' + dialogId + 'IF" scrolling="yes" ' + 'style="width: 100%; height: 100%;" frameborder="0" src="about:blank"></iframe>';
    divs += '    </div>\n';
    divs += '    <div id="' + dialogId + 'Header" class="dialog-header" style="width: ' + width + 'px;" onMouseDown="dialogUtil.drag(event, this.offsetParent)">\n';
    divs += '        <img name="' + dialogId + 'XButton" src="images/close_dialog.gif" style="padding-right:2px;" \n';
    divs += '            align="right" onmouseover="dialogUtil.buttonOver(this);"\n';
    divs += '            onmouseout="dialogUtil.buttonOut(this);" onmousedown="dialogUtil.buttonPress(this, 0);"\n';
    divs += '            onmouseup="dialogUtil.buttonPress(this, 1); ';
    divs += '            dialogUtil.hideDialog(' + '\'' + dialogId + '\'' + ');"/>\n';
    divs += '    </div>\n';

    var dialogDiv = document.createElement("DIV");
    dialogDiv.className = "dialog";
    dialogDiv.style.display = "none";
	dialogDiv.id = dialogId;
	dialogDiv.innerHTML = divs;
	
	document.body.appendChild(dialogDiv);


    var dialog =  new Dialog(url, dialogId);
    dialogSet.addDialogToMap(dialog);
    return dialog;
}