var currentModalObject = null;
function myModal(){
	
	// callback on close
	this.onClose = function(){};
	
	// callback on open
	this.onOpen = function(){};
	
	// inserted object
	this.modalObject = null;
	
	this.bodyOverFlow = "";
	
	this.open = function(parameters){
		var self = this;
		currentModalObject = this; 
		if (typeof(parameters) != "undefined"){
			if ((typeof(parameters.onOpen) != "undefined")){
				this.onOpen = parameters.onOpen;
			}
			if ((typeof(parameters.onClose) != "undefined")){
				this.onClose = parameters.onClose;
			}
			if ((typeof(parameters.modalObject) != "undefined")){
				this.modalObject = parameters.modalObject.html()==null?null:parameters.modalObject;
			}
		}
		$('body').append("<div style='position: fixed; top: 0px; bottom: 0px; left: 0px; right: 0px; z-index: 10000;' id='modal-overlay'></div>");
		$('body').append("<div style='position: fixed; z-index: 10001;' id='modal-window'><div id='replaceContent'></div></div>");
		$('#replaceContent').replaceWith(this.modalObject);
		this.bodyOverFlow = $('body').css('overflow');
		$('body').css('overflow', 'hidden');
		this.onOpen.call();
		$(document).bind('keydown', function(event){
			switch (event.which){
				case 27:
					self.close();
					break;
			}
		});
	};
	
	this.close = function close(){
		$(document).unbind('keydown');
		$('#modal-overlay').remove();
		$('#modal-window').remove();
		$('body').css('overflow', this.bodyOverFlow);
		this.onClose.call();
	};
}
function closeModal(){
	currentModalObject.close();
}
