/**
 * Create a box, centered on the screen, used in conjunction with the overlay
 * to create a modal dialog box widget.
 *
 *@author Sam McCallum <sam@palo-verde.us>
 */
function ModalBox(pageName) {
	ModalBox.prototype.closeAllModals();
	this.pageName = pageName;
	
	this.overlay = new Overlay({disableCloseOnClick: true});
	this.doModalBox();
};

ModalBox.prototype = {
	doModalBox: function () {
		this.modal = new Element('div',{className: 'modalBox'});
		this.modal.style.display = "none";
		document.getElementsByTagName('body')[0].appendChild(this.modal);
		this.modal.style.top = (document.viewport.getScrollOffsets().top + 100)+'px';
		this.modal.style.left = (document.viewport.getWidth()/2).floor() - (this.modal.getWidth()/2).floor()+'px';		
		this.loadModalContent();
	},
	
	loadModalContent: function () {
		new Ajax.Updater(this.modal, '/ctyf/app/ajax/transport/xhtml/'+this.pageName, {method: 'get', onComplete: this.attachModalClose.bind(this), evalScripts: true});
	},
	
	closeAllModals: function () {
		$$('.modalBox').each(
			function (mBox) {
				mBox.fade({duration: 0.2, afterFinish: function () {mBox.parentNode.removeChild(mBox);}});
			}
		);
		
		$$('.overlay').each(
			function (oLay) {
				oLay.fade({duration: 0.3, afterFinish: function () {oLay.parentNode.removeChild(oLay);}});
			}
		);
	},
	
	attachModalClose: function () {
		this.modal.appear({duration: 0.3});
		$$('.modalCloseButton').each(
			function (modalCloseButton) {
				modalCloseButton.observe('click', function (ev) {ModalBox.prototype.closeAllModals(); ev.stop();});
			}
		);
		
		$$('.overlay').each(
			function (modalCloseButton) {
				modalCloseButton.observe('click', function (ev) {ModalBox.prototype.closeAllModals(); ev.stop();});
			}
		);
	}
};

function doNewsletter() {
	new ModalBox('newsletter');
}

function doLogin() {
	new ModalBox('login');
}