/**
 * ModalDialogListener is called by control.js whenever a modal dialog
 * should be opened or closed.
 */ 
function ModalDialogListener(animateDialogs, modalDialogEvent) {
	this.animateDialogs = animateDialogs;
	this.modalDialogEvent = modalDialogEvent;
	this.modalDialogEvent.showModal.subscribe(this.showModal, this, true);
	this.modalDialogEvent.hideModal.subscribe(this.hideModal, this, true);
	this.modalDlg = false;
	this.modalDialogVisible = false;
}

/**
 * Open a modal dialog. The dialog is constructed (if needed), resized
 * the right size for the form in the modal_holder element and then opened.
 */
ModalDialogListener.prototype.showModal = function(type, args, me) {
	// If animation is requested, try to find the button:
	var srcButton = false;
	if (this.animateDialogs) {
		var buttonId = args[0];
		if (buttonId) {
			var buttons = document.getElementById('form_holder').getElementsByTagName('button');
			for (var i = 0; i < buttons.length; i++) {
				if (buttons[i].id == buttonId) {
					srcButton = buttons[i];
					break;
				}
			}
		}
	}

	var formCaption = args[1];

	var formDiv = document.getElementById('modal_holder').getElementsByTagName('div')[0];
	formDiv.style.position = 'relative';
	formDiv.style.backgroundColor = 'transparent';
	var width = parseInt(formDiv.style.width);
	var height = parseInt(formDiv.style.height);
	if (!this.modalDlg) {
		this.modalDlg = new YAHOO.ext.BasicDialog("modal-dlg", {
			modal:true,
			autoTabs:true,
			width: width + 23,
			height: height + 46,
			shadow:true
		});
		this.modalDlg.addKeyListener(27, this.hideModal, this);
		this.modalDlg.addListener('hide', this.modalDialogHidden, this, true);
		this.modalDlg.addListener('show', this.modalDialogShown, this, true);
	}
	document.getElementById('modal_header').innerHTML = formCaption;
	
	// TODO: This will work in YUI-EXT version 0.4
	//if (this.modalDlg.resizeTo) {
		//this.modalDlg.resizeTo(width + 23, height + 46);
		//this.modalDlg.center();
	//}
	// Fix for the current lack of resizeTo function:
	this.modalDlg.el.setSize(width + 23, height + 46);
	this.modalDlg.size={width:width + 23, height:height + 46};
	this.modalDlg.syncBodyHeight();
	
	if (srcButton) { // Animation is requested and the source button is found.
		var el = getEl(srcButton, true);
		var b = el.getBox();
		// Fix: If the wrong button is found by getEl, the position is bad...
		if (b.x == undefined || b.y == undefined || b.x < 1 || b.y < 1) { 
			el = getEl('dummy_button', true);
			el.setWidth(parseInt(srcButton.style.width));
			el.setHeight(parseInt(srcButton.style.height));
			var offsetX = parseInt(document.getElementById('input_sidebar').style.width) + 10;
			var offsetY = parseInt(document.getElementById('top_center').style.height) + 55;
			el.setX(offsetX + parseInt(srcButton.style.left));
			el.setY(offsetY + parseInt(srcButton.style.top));
		}
		this.modalDlg.show(el);
	} else {
		this.modalDlg.show();
	}
}

/**
 * If the modal dialog exists, hide it. Othewise pretend that it has been hidden.
 */
ModalDialogListener.prototype.hideModal = function(type, args, me) {
	if (this.modalDialogVisible) {
		if (this.modalDlg) {
			this.modalDlg.hide();
		} else {
			this.modalDialogHidden();
		}
	}
}

/**
 * This method should be called whenever the modal dialog has been hidden
 * or someone has tried to hide it. Refreshes and shows the page that launched
 * the modal dialog. 
 */
ModalDialogListener.prototype.modalDialogHidden = function() {
	this.modalDialogVisible = false;
	showForm(currentForm, 'modal_owner');
}

/**
 * Do things that must not be done before the dialog is showing.
 */
ModalDialogListener.prototype.modalDialogShown = function() {
	focusFirstField();
	this.modalDialogVisible = true;
}


