var isDialog = {};
var isDialogUtil = {
	_isDialogIndex: 90, // zIndex
	_isDialogX: 0, // clicked x
	_isDialogY: 0, // clicked y

	_open: function(url, args, pId, pos, openOption) {
		var id = 'isd'+isDialogUtil._isDialogIndex;
		if (typeof isDialog[id] == 'undefined') {
			var newDialog = new _isDialog(id, {position:pos}, pId);
			newDialog.open(url, args, openOption);
		}
		else
			isDialog[id].open(url, args, openOption);
	},
	open: function(url, args, pId, openOption) {
		if (url)
			isDialogUtil._open(url, args, pId, 'center', openOption);
	},
	openMouse: function(evt, url, pId, openOption) {
		isDialogUtil._isDialogX = Event.pointerX(evt);
		isDialogUtil._isDialogY = Event.pointerY(evt);
		isDialogUtil._open(url, pId, 'mouse', openOption);
	},

	close: function(id) {
		if ($('isCalendarBox'))
			$('isCalendarBox').hide();
		isDialog[id].close();
	},

	_rePosition: function() {
		for (var dlgId in isDialog) {
			var dlg = isDialog[dlgId];
			if (dlg.id && $(dlg._dialogBoxId()).visible()) {
				dlg._setPosition();
			}
		}
	},

	LoadingCounter: 0,
	showLoading: function() {
		if (typeof LoadingDlg != 'undefined') {
			if (isDialogUtil.LoadingCounter == 0)
				LoadingDlg.display(true);
			isDialogUtil.LoadingCounter++;
		}
	},
	hideLoading: function() {
		if (typeof LoadingDlg != 'undefined') {
			if (isDialogUtil.LoadingCounter == 1)
				LoadingDlg.close();
			if (isDialogUtil.LoadingCounter > 0)
				isDialogUtil.LoadingCounter--;
		}
	}
};

var _isDialog = Class.create({
	initialize: function(id, options, pId) {
		this.id = id;
		this.children = new Array();
		if (pId)
			isDialog[pId].children.push(this.id);

		this.options = {
			opacity: '0.5',
			backColor: '#FFF',
			dlgBackColor: '',
			position: 'center',
			isModal: true,
			dragable: true
		};
		Object.extend(this.options, options || {});

		// background iframe
		Element.insert(document.body, { bottom:'<iframe id="'+this._backIframeId()+'"></iframe>' });
		$(this._backIframeId()).setStyle({
			width: '100%',
			height: '100%',
			position: 'absolute',
			opacity: '0.0',
			display: 'none'
		});

		// background div
		Element.insert(document.body, { bottom:'<div id="'+this._backgroundId()+'"></div>' });
		$(this._backgroundId()).setStyle({
			width: '100%',
			height: '100%',
			position: 'absolute',
			opacity: this.options.opacity,
			backgroundColor: this.options.backColor,
			display: 'none'
		});

		// dialog
		Element.insert(document.body, { bottom:'<div id="'+this._dialogBoxId()+'"></div>' });
		$(this._dialogBoxId()).setStyle({
			position: 'absolute',
			opacity: '1.0',
			backgroundColor: this.options.dlgBackColor,
			display: 'none'
		});

		isDialog[this.id] = this;
	},

	open: function(url, args, options) {
		var dlgId = this.id;
		var dlgBoxId = this._dialogBoxId();
		var params = { 'DialogId': this.id };
		Object.extend(params, args || {});
		options = Object.extend({ 'btnClose': true }, options || {});

		isDialogUtil.showLoading();

		new Ajax.Updater(this._dialogBoxId(), url, {
			method: 'post',
			parameters: params,
			evalScripts: true,
			onComplete: function(re) {
				isDialogUtil.hideLoading();

				var dlg = isDialog[dlgId];
				dlg.display(true);
				new Draggable(dlg._dialogBoxId(), {handle:dlgId+'_PopupTitle'});
				isUtil.autoBlur(dlgBoxId);

				if (options.btnClose)
					$(dlgId+'_btnClose').show();
			}
		});
	},

	display: function(isFirst) {
		this._setPosition(isFirst);

		if (Prototype.Browser.IE) {
			this._setZIndex(this._backIframeId());
			$(this._backIframeId()).show();
		}
		this._setZIndex(this._backgroundId());
		$(this._backgroundId()).show();
		this._setZIndex(this._dialogBoxId());
		$(this._dialogBoxId()).show();
	},

	close: function() {
		if (Prototype.Browser.IE) {
			$(this._backIframeId()).hide();
			isDialogUtil._isDialogIndex--;
		}
		$(this._backgroundId()).hide();
		$(this._dialogBoxId()).hide();
		isDialogUtil._isDialogIndex -= 2;
		this.children.each( function(dlgId) {
			isDialog[dlgId].close();
		});
		$(this._dialogBoxId()).innerHTML = '';
	},

	visible: function() {
		return $(this._dialogBoxId()).visible();
	},

	_setPosition: function(isFirst) {
		var viewport = document.viewport.getDimensions(); // total viewport
	    var scrollOffset = document.viewport.getScrollOffsets();

	    // now viewport
	    var pointViewport = isUtil.viewport();

		// set background position
		var backiframe = $(this._backIframeId());
		var background = $(this._backgroundId());

		if ((backiframe != null) && (backiframe != ''))
		{
			backiframe.setStyle({
				left: scrollOffset.left+'px',
				top: scrollOffset.top+'px',
				width: viewport.width+'px',
				height: viewport.height+'px'
			});
		}

		background.setStyle({
			left: scrollOffset.left+'px',
			top: scrollOffset.top+'px',
			width: viewport.width+'px',
			height: viewport.height+'px'
		});

		// set dialog
		var dlg = $(this._dialogBoxId());

		// for ie6, 7 ;;;
		if (Prototype.Browser.IE)
			dlg.show();

		// set dialog position
		if (isFirst === true) {
			if (this.options.position == 'center') {
				var top_point = Math.round(pointViewport.height/2)-Math.round(dlg.getHeight()/2);
				if (top_point < 0)
					top_point = 0;
				var left_point = Math.round(pointViewport.width/2)-Math.round(dlg.getWidth()/2);
				if (left_point < 0)
					left_point = 0;
	
				dlg.setStyle({
					left: (left_point+scrollOffset.left)+'px',
					top: (top_point+scrollOffset.top)+'px'
				});
			}
			else if (this.options.position == 'mouse') {
				dlg.setStyle({
					left: isDialogUtil._isDialogX+'px',
					top: isDialogUtil._isDialogY+'px'
				});
			}
		}
	},

	_setZIndex: function(id) {
		$(id).setStyle({zIndex:isDialogUtil._isDialogIndex++});
	},
	_dialogBoxId: function() {
		return 'isDialog_'+this.id;
	},
	_backgroundId: function() {
		return 'isDialog_background'+this.id;
	},
	_backIframeId: function() {
		return 'isDialog_backIframe'+this.id;
	}
});

// set event about popup
Event.observe(window, 'scroll', isDialogUtil._rePosition);
Event.observe(window, 'resize', isDialogUtil._rePosition);

Event.addOnLoad(function() {
	// default loading popup
	LoadingDlg = new _isDialog('LoadingDlg', {'dlgBackColor':''});
	$(LoadingDlg._dialogBoxId()).innerHTML = '<div class="large_loading_icon"></div>';
});
