var g_curFileName = "modal";

window["g_jsFile_"+g_curFileName+"_js"] = true;


window.ModalDialog = {

instance : function() {
var win = window;
var dialog = ModalDialog.local;

do {
win = win.parent;

try {
if (win.ModalDialog)
dialog = win.ModalDialog.local;
} catch (e) {  }
} while (win != win.parent);

return dialog;
},


local : {
"okDialog" : okDialog,
"yesNoDialog" : yesNoDialog,
"okCancelDialog" : okCancelDialog,
"yesNoCancelDialog": yesNoCancelDialog,
"saveDialog" : saveDialog,
"waitDialog" : waitDialog,
"inputDialog" : inputDialog,
"genericDialog" : genericDialog
}
};



function okDialog(title, message, callback)
{
return genericDialog(
title, 
message, 
[
{value: "ok", text: "Ok", isDefault: true, isCancel: true, isHighlight: true}
], 
callback
);
}


function yesNoDialog(title, message, callback)
{
return genericDialog(
title, 
message, 
[
{value: "yes", text: "Yes", isDefault: true, isHighlight: true}, 
{value: "no", text: "No", isCancel: true}
], 
callback
);
}


function okCancelDialog(title, message, callback, properties)
{
return genericDialog(
title, 
message, 
[
{value: "ok", text: "OK", isDefault: true, isHighlight: true}, 
{value: "cancel", text: "Cancel", isCancel: true}
], 
callback,
properties
);
}


function yesNoCancelDialog(title, message, callback)
{
return genericDialog(
title, 
message, 
[
{value: "yes", text: "Yes", isDefault: true, isHighlight: true}, 
{value: "no", text: "No"},
{value: "cancel", text: "Cancel", isCancel: true}
], 
callback
);
}


function saveDialog(title, message, callback)
{
return genericDialog(
title, 
message, 
[
{value: true, text: "Save Changes", isHighlight: true},
{value: false, text: "Don't Save"},
{value: "cancel", text: "Cancel"}                                   
], 
callback
);
}


function waitDialog(title, message)
{
return genericDialog(
title, 
message, 
[], 
null, 
{width: 300, height: 90}
);
}


function inputDialog(title, message, defaultValue, callback, singleLine)
{        
var inputHtml = singleLine ? '<input type="text"/>' : '<textarea />';
var inputElt = jQuery(inputHtml).addClass("dialogInput").val(defaultValue);

var div = jQuery("<div />")
.append(jQuery("<div />").append(message))
.append(jQuery("<div />").append(inputElt));        

var callbackWrapper;
if(callback)
{
callbackWrapper = function(value){
callback(value == "ok" ? inputElt.val() : null)
};
}

var dialog = genericDialog(
title, 
div, 
[
{value: "ok", text: "OK", isDefault: true, isHighlight: true},
{value: "cancel", text: "Cancel", isCancel: true}
],
callbackWrapper, 
{width: 460}
);

inputElt.focus();

return dialog;
}



function genericDialog(title, message, buttonSpecs, callback, properties)
{        
properties = jQuery.extend({}, genericDialog.DEFAULT_PROPERTIES, properties);

var defaultAction = null;
var cancelAction = null;
var buttonCallbacksByLabel = {};
var buttonSpecsByLabel = {};

if (buttonSpecs)
{
jQuery.each(buttonSpecs, function(i, buttonSpec) {
buttonSpecsByLabel[buttonSpec.text] = buttonSpec;
buttonCallbacksByLabel[buttonSpec.text] = function(ev){
jQuery(this).dialog("close");
if(callback)
callback(buttonSpec.value);
};

if (buttonSpec.isDefault)
defaultAction = buttonCallbacksByLabel[buttonSpec.text];

if (buttonSpec.isCancel)
cancelAction = buttonCallbacksByLabel[buttonSpec.text];   
});    
}    

var dialogContents = jQuery('<div class="dialogContents" />').appendTo(document.body).append(message);

dialogContents.dialog({
title: title,
modal: true,
autoOpen: false,
resizable: false,
closeOnEscape: false,
draggable: false,
width: properties.width,
height: properties.height,
buttons: buttonCallbacksByLabel,
dialogClass: properties.dialogClass,
zIndex: 999999990, 
open: function()
{
window.setTimeout(
function()
{ 
jQuery(document).unbind('mousedown.dialog-overlay').unbind('mouseup.dialog-overlay') ; 
}, 100); 
var mediaSelector = 'applet, object, iframe, embed, plugin, select';
var mediaElements = jQuery(mediaSelector).not('.MODALDIALOG_hiddenForDialog').not(jQuery(this).find(mediaSelector));
mediaElements.addClass('MODALDIALOG_hiddenForDialog');
jQuery(this).data('hiddenMediaElements',mediaElements)        
},
close: function()
{
jQuery(this).data('hiddenMediaElements').removeClass('MODALDIALOG_hiddenForDialog');
}
});

var dialogContainer = dialogContents.parent();

var killKeyEvent = function(ev) 
{
ev.stopPropagation();
ev.stopImmediatePropagation();
};

dialogContainer.bind("keydown keyup", killKeyEvent);
dialogContainer.bind("keypress", function(ev)
{
killKeyEvent.call(this, ev);
if (ev.keyCode == 13 && defaultAction)
{        
defaultAction.call(dialogContents,ev);
}

else if (ev.keyCode == 27 && cancelAction)
{            
cancelAction.call(dialogContents,ev);
}
});

jQuery('button',dialogContainer).each(function(i){
var button = jQuery(this);
button.attr('tabIndex',-1);
if (buttonSpecsByLabel[button.text()].isHighlight) {
button.addClass('highlightButton');
}
});

if (!properties.displayTitleBarCloseButton)
{
jQuery('a.ui-dialog-titlebar-close', dialogContainer).attr('style', 'display:none');
}
else
{
jQuery('a.ui-dialog-titlebar-close', dialogContainer).attr('tabIndex',-1).click(function(ev){
if (cancelAction)
cancelAction.call(dialogContents,ev);
});
}

var dialog = new genericDialog.ModalDialog(dialogContents);

dialog.open();   

return dialog;
}


genericDialog.DEFAULT_PROPERTIES = {
width: 400, 
height: 240,
dialogClass: 'modalDialog',
showTitleBarCloseButton: true
};


genericDialog.ModalDialog = function(content)
{
this.content = content;
}


genericDialog.ModalDialog.prototype.open = function() 
{
this.content.dialog("open");
}


genericDialog.ModalDialog.prototype.close = function() 
{
this.content.dialog("close");
}


genericDialog.ModalDialog.prototype.isOpen = function() 
{
return this.content.dialog("isOpen");
}