/**
 * @author Ryan Johnson <ryan@livepipe.net>, modified by okjungsoo
 * @copyright 2007 LivePipe LLC
 * @package Control.Modal
 * @license MIT
 * @url http://livepipe.net/projects/control_modal/
 * @version 2.0.0.RC1
 */

if(typeof Control == 'undefined')
	throw("You require md/js/control/Control.js!!");

// Append close-button
Event.observe(window, 'load', function(){
	var dlgs = $A(document.getElementsByClassName('popup_dialog'));
	dlgs.each(function(dlg){
		var titleDiv = dlg.firstDescendant();
		titleDiv.innerHTML = titleDiv.innerHTML +
			'<div class="popup_dialog_close" onclick="Control.Modal.close();"> </div>';
	});
});

Control.Modal.positionContainer = function(element, offset_left, offset_top){
	if(!element._absolutized){
		element.setStyle({
			position: 'absolute'
		}); 
		element._absolutized = true;
	}
	var dimensions = element.getDimensions();
	Position.prepare();
	element.setStyle({
		top: offset_top + 'px', left: offset_left + 'px'
	});
}


Object.extend(Control.Modal.prototype,{
	element: false,
	
	initialize: function(element, container, options){
		this.element = $(element);
		this.container = container;
		this.options = {
			beforeOpen: Prototype.emptyFunction,
			afterOpen: Prototype.emptyFunction,
			beforeClose: Prototype.emptyFunction,
			afterClose: Prototype.emptyFunction,
			containerClassName: '',
			opacity: 0.3,
			zIndex: 9990,
			width: null,
			height: null,
			position: 'absolute', //'absolute' or 'relative'
			positioningMethod: function(){
				Control.Modal.center(this.container);
			},
			isCentered: true
		};
		Object.extend(this.options,options || {});
		
		if(this.element){
			this.element.onclick = function(event){
				this.open();
				Event.stop(event);
				return false;
			}.bindAsEventListener(this);
		}
		this.position = this.options.positioningMethod.bind(this);
	},
	open: function(){
		if(this.notify('beforeOpen') === false)
			return;		
		if(!Control.Modal.loaded) {
			Control.Modal.load();
		}
		Control.Modal.close();
		
		Event.observe(document, 'keydown',Control.Modal.onKeyDown);
		Control.Modal.current = this;

		this.container.setStyle({
			zIndex: this.options.zIndex + 1,
			width: (this.options.width? this.options.width +'px' : ''),
			height: (this.options.height? this.options.height +'px' : '')
		});

		this.container.addClassName(this.options.containerClassName);
		this.showModal();

		this.notify('afterOpen');
	},
	
	showModal: function(){
		this.position();
		this.container.show();
		
		if(this.options.isCentered){ 
			Event.stopObserving(window,'resize',this.position,false);
			Event.stopObserving(window,'scroll',this.position,false);
			Event.observe(window,'resize',this.position,false);
			Event.observe(window,'scroll',this.position,false);
		}
	},
	
	close: function(){
		if(this.notify('beforeClose') === false)
			return;
		
		if(Control.Modal.ie){
			$A(document.getElementsByTagName('select')).each(function(select){
				select.style.visibility = 'visible';
			});			
		}

		if(this.options.isCentered) {
			Event.stopObserving(window,'resize',this.position,false);
			Event.stopObserving(window,'scroll',this.position,false);			
		}
		Event.stopObserving(window,'keyup',Control.Modal.onKeyDown);
		Control.Modal.current = false;	

		this.container.hide();
		this.resetClassNameAndStyles();
		this.notify('afterClose');
	},
	resetClassNameAndStyles: function(){
		this.container.removeClassName(this.options.containerClassName);
		this.container.setStyle({
			height: null,
			width: null,
			top: null,
			left: null
		});
	},
	notify: function(event_name){
		try{
			if(this.options[event_name])
				return [this.options[event_name].apply(this.options[event_name],$A(arguments).slice(1))];
		}catch(e){
			if(e != $break)
				throw e;
			else
				return false;
		}
	}
});
if(typeof(Object.Event) != 'undefined')
	Object.Event.extend(Control.Modal);
Control.Modal.attachEvents();