/*
 * 
 * id - Id of the window component
 * options [
 * width - Width of window component
 * heigth - Heigth of window component
 * opacity - Opacity of modal window
 * z-index -
 * ]
 */

var _CURRENT_DIALOG = new Array();

var ControlKeyDialog = {
	oldFunction: null,
	inited: false,
	
	init: function() {
		if (!ControlKeyDialog.inited) {
			ControlKeyDialog.oldFunction = document.onkeypress;
			ControlKeyDialog._putKeypressFunction();
			ControlKeyDialog.inited = true;
		}
	},
	
	stop: function() {
		document.onkeypress = ControlKeyDialog.oldFunction;
		ControlKeyDialog.inited = false;
	},

	_putKeypressFunction: function() {
		document.onkeypress = function(event) {
			var _event = (window.event) ? window.event : event;
			
			if (_CURRENT_DIALOG[_CURRENT_DIALOG.length -1] && _event.keyCode == KeyCode.ESCAPE) {
				_CURRENT_DIALOG[_CURRENT_DIALOG.length -1].close();
			}
			
			if (ControlKeyDialog.oldFunction) {
				ControlKeyDialog.oldFunction();
			}
		}
	}
}

ControlKeyDialog.init();

function closeAllDialog() {
	for (var i = 0; i < _CURRENT_DIALOG.length; i++) {
		_CURRENT_DIALOG[i].close();
	}
}

function Dialog(id, options) {
	this.id = id;
	this.options = options;
	
	this._getZIndexOption = function () {
		if (this.options && this.options.zIndex) {
			return this.options.zIndex;
		}
		return 10000;
	}
	
	this._getOpacity = function () {
		if (this.options && this.options.opacity) {
			return this.options.opacity;
		}
		return 0.2;
	}
	
	this._getWidthOption = function () {
		if (this.options && this.options.width) {
			return this.options.width;
		}
		return 469;
	}
	
	this._getHeigthOption = function () {
		if (this.options && this.options.heigth) {
			return this.options.heigth;
		}
		return 138;
	}
	
	this._getWidth = function () {
		var scrollWidth = Math.max(
			document.documentElement.scrollWidth,
			document.body.scrollWidth
		);
		var offsetWidth = Math.max(
			document.documentElement.offsetWidth,
			document.body.offsetWidth
		);

		if (scrollWidth < offsetWidth) {
			return $(window).width() + 'px';
		} else {
			return scrollWidth + 'px';
		}
	}
	
	this._getHeight = function () {
		var scrollHeight = Math.max(
			document.documentElement.scrollHeight,
			document.body.scrollHeight
		);
		var offsetHeight = Math.max(
			document.documentElement.offsetHeight,
			document.body.offsetHeight
		);

		if (scrollHeight < offsetHeight) {
			return $(window).height() + 'px';
		} else {
			return scrollHeight + 'px';
		}
	}

	this.beforeOpen = function() {
	}

	this.open = function () {
		this.beforeOpen();

		this._initModal();
		this._initWindow();
		
		_CURRENT_DIALOG[_CURRENT_DIALOG.length] = this;
	}

	this.close = function() {
		this._removeCurrentDialog();
		
		if ($('.bgModal').length > 0 && _CURRENT_DIALOG.length == 0) {
			$('.bgModal').fadeOut('fast');
		}
		$('#' + this.id).fadeOut('fast');
		
		this.afterClose();
	}
	
	this.afterClose = function() {
	}
	
	this._removeCurrentDialog = function() {
		var back = new Array();
		
		for (var i = 0; i < _CURRENT_DIALOG.length -1; i++) {
			back[i] = _CURRENT_DIALOG[i];
		}
		
		_CURRENT_DIALOG = back;
	}
	
	this._initModal = function () {
		if ($('.bgModal').length == 0) {
			$('<div/>').appendTo(document.body).addClass('bgModal').css({background: '#000000', 
				opacity: this._getOpacity(),
				position: 'absolute',
				width: this._getWidth(), 
				height: this._getHeight(),
				top: 0,
				zIndex: this._getZIndexOption()
				}).hide();
		}
		$('.bgModal').fadeIn('fast');
	}
	
	this._initWindow = function () {
		var realDiv = $('#' + this.id);
		var _width = this._getWidthOption();
		var _heigth = this._getHeigthOption();
		var pTop = ($(window).height() - _heigth) / 2;
		var pLeft = ($(window).width() - _width) / 2;

		pTop = Math.max(pTop, $(document).scrollTop());

		realDiv.css({top: pTop, left: pLeft, width: _width, heigth: _heigth, position: 'absolute', zIndex:(this._getZIndexOption() + 1)}).fadeIn('fast');
	}
}

