/*
 * 
 * Mootools-based Modal Dialog box
 * BL
 * 
 * Based on:
 * Smoothbox v20080623 by Boris Popoff (http://gueschla.com)
 * To be used with mootools 1.2
 *
 * Based on Cody Lindley's Thickbox, MIT License
 *
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 */

var ModalDialog = new Class({

	isInitialised: false,
	isOpen: false,
	Implements: [Options, Events, Chain],
	
	options: {
		height: 0,
		width: 0
	},
	
	hook: function(Element, Mode){
		if(Element==undefined){
			return;
		}
		if(Mode==undefined){
			Mode='click';
		}
		Element.ModalDialog = this;
		Element.addEvent(Mode, function(){
			this.ModalDialog.toggleBox();
		});
	},
	
	toggleBox: function(){
		if(this.isOpen){
			this.closeBox();
		}
		else{
			this.openBox();
		}
	},

	checkInitialised: function(){
		if(!this.isInitialised){
			this.initalise();
			this.isInitialised = true;
		}
	},

	openBox: function(){
		this.checkInitialised();
		this.Overlay.inject(document.body);
		this.DialogArea.inject(document.body);
		this.Overlay.set('tween', {
	    	duration: 400
	    });
		this.Overlay.tween('opacity', 0, 0.6);
		this.DialogArea.setStyle('display', 'block');
        this.calculateOverlaySize();
        this.calculateDialogPosition();
	},

	closeBox: function(){
		this.checkInitialised();
		this.Overlay.set('tween', {
	        duration: 400,
			onComplete: function(){
				this.element.ModalDialog.DialogArea.dispose(); this.element.dispose(); 
			}
	    });
	    this.Overlay.tween('opacity', 0.6, 0);
		this.DialogArea.setStyle('display', 'none');
	},
	
	confirmBox: function(){
		/*if(typeof this.preConfirm != 'undefined'){
			if(! this.preConfirm()){
				return false;
			}
		}*/
		
		if(typeof this.onConfirm != 'undefined'){
			if (!this.onConfirm()) return false;
		}
		this.closeBox();
	},

	cancelBox: function(){
		this.closeBox();
		if(typeof this.onCancel != 'undefined'){
			this.onCancel();
		}
	},

	initalise: function(){
        this.Overlay = new Element('div').setProperty('id', 'MD_Overlay');
        this.Overlay.setOpacity(0);
		this.Overlay.ModalDialog = this;

		this.DialogArea = new Element('div').setProperty('id', 'MD_DialogArea');
		
		this.DialogTitle = new Element('h1')
			.setProperty('id', 'MD_Title')
			.inject(this.DialogArea)
			.set('text', 'Additional Details Required');
		
		this.TextArea = new Element('div').setProperty('id', 'MD_TextArea').inject(this.DialogArea);
        this.TextArea.innerHTML = "Text Area";

		this.ButtonArea = new Element('div').setProperty('id', 'MD_ButtonArea').inject(this.DialogArea);
	},

	setContent: function(Content){
		this.TextArea.innerHTML = Content;
	},

	setButtons: function(Buttons){
		this.checkInitialised();
		if(Buttons == undefined){
			Buttons = 'Default';
		}
		if (this.ButtonArea != undefined) {
			this.ButtonArea.getElements('MD_Button').each(function(Button){
				Button.dispose();
			});
		}
		switch(Buttons){
			case "Default":
				this.addButton('Ok', function(){ this.ModalDialog.confirmBox(); });
				break;
			case "ConfirmCancel":
				this.addButton('Confirm', function(){ this.ModalDialog.confirmBox(); });
				this.addButton('Cancel', function(){ this.ModalDialog.cancelBox(); });
				break;
		}
	},

	addButton: function(Text, ClickFunction){
		if(typeof ClickFunction == 'undefined'){
			alert('The passed ClickFunction parameter ('+ClickFunction+') is undefined.');
		}
		var Button = 
			new Element('input')
			.set('type', 'button')
			.addClass('MD_Button')
			.set('value', Text)
			.set('title', Text)
			.addEvent('click', ClickFunction)
			.inject(this.ButtonArea);

		Button.ModalDialog = this;
	},

	calculateOverlaySize: function(){
	    // we have to set this to 0px before so we can reduce the size / width of the overflow onresize
	    $("MD_Overlay").setStyles({
	        "height": '0px',
	        "width": '0px'
	    });
	    $("MD_Overlay").setStyles({
	        "height": window.getScrollHeight() + 'px',
	        "width": window.getScrollWidth() + 'px'
	    });
	},

	calculateDialogPosition: function(){
	    if ($("MD_DialogArea")) {
			var width = parseInt($("MD_DialogArea").getStyle('width'));
			var height = parseInt($("MD_DialogArea").getStyle('height'));
			
			var leftPos = ( window.getScrollLeft() + window.getWidth() - width) / 2 + 'px'; 
			var topPos = ( window.getScrollTop() + window.getHeight() - height) / 2 + 'px';
	
	        $("MD_DialogArea").setStyles({
	            left: leftPos,
	            top: topPos,
	            display: "block"
	        });
	    }
	}

});
