var SimpleDialog = function() 
{
	// Dialog instance
	var dialog, dialogContentUrl;

	// return a public interface
    return {
        init : function()
        {
			this.createDialog();
        },
       
		createDialog : function()
		{
			if (!dialog)
			{	
				dialog = new Ext.BasicDialog("simple-dialog", { 
	                        autoTabs: false,
	                        modal: true,
	                      	collapsible: false,
	                        width: 350,
	                        height: 200,
	                        minWidth: 350,
	                        minHeight: 200,
	                        shadow: true,
	                        shadowOffset: 10,
	                        proxyDrag: true,
	                        shim: true,
	                        resizable: false
				});
            
				dialog.addKeyListener(27, dialog.hide, dialog);
				dialog.addButton('Close', dialog.hide, dialog);
				dialog.on('hide', this.clearContent);
			}
			return dialog;
		},
		       
        showDialog : function(invoker, title, url, width, height) 
        {
        	
        	this.load(url, title);
        	
        	if (width && height)
        	{
        		dialog.setContentSize(width + 15, height + 15);
        	}
        	else
        	{
        		dialog.resizeTo(dialog.minWidth, dialog.minHeight);
        	}
        		
            dialog.center();
            dialog.show(invoker.dom);
        },
        
        load : function(url, title) 
        {
        	if (title)
	        	dialog.setTitle(title);
			dialogContentUrl = url;
			dialog.on('show', this.doLoad);
        },

		update : function(content, title) 
        {
        	if (title)
	        	dialog.setTitle(title);
        	dialog.body.update(content);
        	dialog.body.setStyle("overflow","hidden");
        },
        
		doLoad : function() 
        {
        	SimpleDialog.update('<div id="simple-dialog-loading-indicator" class="loading-indicator">Loading...</div>' + 
							'<iframe id="simple-dialog-internal-frame" onreadystatechange="SimpleDialog.onContentLoad()" onload="SimpleDialog.onContentLoad()" style="visibility: hidden;" width="100%" height="0%" frameborder="0" scrolling="auto" src="' + dialogContentUrl + '" />');
        },

		onContentLoad : function()
		{
			var loadingIndicator = Ext.get('simple-dialog-loading-indicator');
			var internalFrame = Ext.get('simple-dialog-internal-frame');
			loadingIndicator.dom.styles = 'height: 0%; width: 0%; visibility: hidden;';
			loadingIndicator.remove();
			internalFrame.applyStyles('height: 100%; width: 100%; visibility: visible;');
			internalFrame.show();
		},
		
		clearContent : function(dlg)
		{
			SimpleDialog.update('<div></div>');
		}
    };
}();

Ext.onReady(SimpleDialog.init, SimpleDialog, true);
