// DIALOG 
//create a namespace 
YAHOO.namespace("qw.dialog")

//create a new object for this module:
YAHOO.qw.dialog.inDialog = function(){

	//Some shortcuts to use in our example:
	var Event = YAHOO.util.Event, Dom = YAHOO.util.Dom, lang = YAHOO.lang;
	
	return {
		
		init: function()
		{
		
			// Instantiate the Dialog
			this.dialog = new YAHOO.widget.Dialog("yui-dialog-panel", {
				fixedcenter: true,
				underlay:'none',
				visible: false,
				close: false,
				modal:true,
				constraintoviewport: true,
				evalScripts:true
				
			});
				
			this.show = function(op)
			{
				this.dialog.hide();
				showPleaseWait();
				this.dialog.op = op;
				var dlgbody = $('yui-dialog');
				caption = $('qwdlghd');
				if (caption) {
					caption.update(op.title);
				}
				
				showOnComplete = function(transport){
					// ADJUST WIDTH
					var dlgbody = $('yui-dialog');
					dlgbody.update(transport.responseText)
					
					var block = dlgbody.down("div");
					if (block) {
						width = block.getWidth();
						height = block.getHeight();

						var extraWidth = 12;
						$('yui-dialog-panel').style.width = ((width + extraWidth) + "px");
						$('yui-dialog-panel').style.height = ((height + 6) + "px");
						$('yui-dialog').style.width = ((width + extraWidth) + "px");
						$('yui-dialog').style.height = ((height + 6) + "px");
						$('yui-dialog-form').style.width = ((width + extraWidth) + "px");
						$('yui-dialog-form').style.height = ((height + 6) + "px");
						
						this.dialog.show();		
						ft = $('qwdlgfooter');
						ft.insert({after:"<a class='container-close' href='#' onclick='closeDialog();'>Close</a>"});
					}
					
					hidePleaseWait();
				}.bind(this);
				
				this.dialogApplet = op.applet;
				var dlgform = $('yui-dialog-form');
				if(!dlgform)
				{
					return;
				}
				dlgform.action = '/ajax/' + op.applet + '/'
				new Ajax.Request('/ajax/' + op.applet + '/', {
					parameters: op.choices.parameters,
					onSuccess: showOnComplete
				});						

			}
			
			this.hide = function(op){
				this.dialog.cancel();
			}
			
			this.ajaxSubmit = function(parameters)
			{
				showPleaseWait();
				var dlgform = $('yui-dialog-form');
				var oncomplete = function(transport){
						var dlgbody = $('yui-dialog');
						dlgbody.update(transport.responseText);
						var block = dlgbody.down("div");
						if (block) {
							width = block.getWidth();
							height = block.getHeight();

							var extraWidth = 12;
							$('yui-dialog-panel').style.width = ((width + extraWidth) + "px");
							$('yui-dialog-panel').style.height = ((height + 6) + "px");
							$('yui-dialog').style.width = ((width + extraWidth) + "px");
							$('yui-dialog').style.height = ((height + 6) + "px");
							$('yui-dialog-form').style.width = ((width + extraWidth) + "px");
							$('yui-dialog-form').style.height = ((height + 6) + "px");
							
							this.dialog.show();		
							ft = $('qwdlgfooter');
							ft.insert({after:"<a class='container-close' href='#' onclick='closeDialog();'>Close</a>"});
						}
						hidePleaseWait();
					}.bind(this);
				this.dialog.hide();
				dlgform.request({
					parameters: parameters,
					onComplete: oncomplete
				});
			}
			
			// We're all set up with our Dialog's configurations. Now, render the Dialog 
			
			this.dialog.render(); 
		}
		
		
	}

}()

YAHOO.util.Event.onDOMReady(YAHOO.qw.dialog.inDialog.init, YAHOO.qw.dialog.inDialog, true); 


function doDialog(op)
{
	YAHOO.qw.dialog.inDialog.show(op);
}

function closeDialog()
{
	YAHOO.qw.dialog.inDialog.hide();
}

function submitQWDialog(parameters)
{
	YAHOO.qw.dialog.inDialog.ajaxSubmit(parameters);
	return false;
}
