
/*===============================================================================
	ModalDialogue.js
	John Larson
	4/07/09
	
	Component for modal dialogues.
	

===============================================================================*/


var ModalDialogue = new Class({
	
	Implements: [Options, Events],
	
	options: {
		useButtonImages:	true,
		buttonPath:			'images/buttons/',
		buttonFileFormula:	'button[theLabel].gif',
		width:				'500px',
		valueSet:			null
	},
	
	initialize: function(title, content, buttonLabelSet, callBackFunction, options) {
		this.setOptions(options);
		
		this.callBackFunction = callBackFunction;
		
		this.modalizer = new Modalizer;
		this.modalizer.setModalOptions({ hideOnClick : false });
		this.popUp = new PopUpWindow(title, 
			{injectLocation: document.body, isClosable: false, isDraggable: false, 
			 width: this.options.width, zIndex: 10000 });
		
		var contentDiv = new Element('div');
		if($type(content) == 'string')
			contentDiv.setHTML(content);
		else
			contentDiv.adopt(content);
			
		// build the button div:
		var buttonDiv = new Element('div');
		buttonDiv.setStyle('margin-top', '6px');
		buttonLabelSet.each(function(theLabel, index) {
			var buttonElement;
			if(this.options.useButtonImages)
				buttonElement = new Element('input', { 'type': 'image',
				  'src': this.options.buttonPath + this.options.buttonFileFormula.replace('[theLabel]', theLabel.replace(/ /g, '')) });
			else
				buttonElement = new Element('input', { 'type': 'submit', 'value': theLabel });
			
			buttonElement.setStyle('margin-right', '5px');
			
			var thisValue;
			if(this.options.valueSet)
				thisValue = (this.options.valueSet ? this.options.valueSet[index] : theLabel);
			
			buttonElement.addEvent('click', this.submitChoice.pass(thisValue, this));
			buttonDiv.adopt(buttonElement);
			
		}.bind(this));
		
		var contentHTML = new Element('div');
		contentHTML.adopt(contentDiv);
		contentHTML.adopt(buttonDiv);
		
		this.buttonDiv = buttonDiv;
		
		this.popUp.setContent(contentHTML);
		this.popUp.setPosition({}); // center it on the window
		
	},
	
	setTitle: function(newTitle) {
		this.popUp.setTitle(newTitle);
	},
	
	getModalChoice: function() {
		this.modalizer.modalShow();
		this.popUp.open();
		this.buttonDiv.getFirst().focus.delay(200, this.buttonDiv.getFirst());
	},
	
	submitChoice: function(theLabel) {
		this.popUp.close();
		this.modalizer.modalHide();
		this.callBackFunction(theLabel);
	}
	
});


ModalDialogue.Alert = new Class({
	
	Implements: [Options],
	
	options: {
		useButtonImages:	true,
		buttonPath:			'images/buttons/',
		buttonFileFormula:	'button[theLabel].gif',
		width:				'500px',
		valueSet:			null
	},
	
	initialize: function(title, options) {
		this.setOptions(options);
		this.messageDiv = new Element('div');
		this.modalDialogue = new ModalDialogue(title, this.messageDiv, ['Ok'], $empty, this.options)
	},
	
	alert: function(message) {
		this.messageDiv.setHTML(message);
		this.modalDialogue.popUp.moveToCenter();
		this.modalDialogue.getModalChoice();
	},
	
	setTitle: function(newTitle) {
		this.modalDialogue.setTitle(newTitle);
	}
});


