
var DialogManager = function(){
	var _Dialogs = new Array();
	var _DialogPositioner = null;
	var _DialogRepository = null;
	var _DialogWhiteout = null;
	var _DialogIsOpen = false;
	var _IsLoaded = false;
	var _ShowDialogPrematureRequest = '';
	
	//var _DialogInitialState = '';
	
	var _Dialog = function(sId, iWidth, bScrollWithWindow){
		return {
			Id: sId,
			Width: iWidth,
			ScrollWithWindow: bScrollWithWindow,
			
			windowRef: function(){
				return document.getElementById(this.Id+'_dialogWindow');
			},
			show: function(bCenter){
//				var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
//				if(IE6){_DialogPositioner.style.top = 150 + document.documentElement.scrollTop + 'px';}

				if(bCenter){
					_DialogPositioner.style.top = '40%';
				}else{
					_DialogPositioner.style.top = 50 + document.documentElement.scrollTop + 'px';	
				}
				_DialogPositioner.style.marginLeft = (parseInt(this.Width/2)*-1)+'px';
				_DialogPositioner.style.display = 'block';
				
				var myDialogWindow = this.windowRef();
				myDialogWindow.style.display = 'block';
				myDialogWindow.style.width = this.Width+'px';

				if(this.ScrollWithWindow){
					_DialogPositioner.style.position = 'absolute';
				}
				
				_DialogPositioner.appendChild(myDialogWindow);
			}
		};
	}
	
	var _UnLoadDialogs = function(){
		if(_DialogPositioner.childNodes.length > 1){alert(_DialogPositioner.childNodes.length + ' un-closed dialogs found');}
		for(i=0;i<_DialogPositioner.childNodes.length;i++){
			_DialogRepository.appendChild(_DialogPositioner.childNodes[i]);
		}
	}
	
	return {
		Dialog: _Dialog,
		
		init: function(){
			DomReady.ready(function() {
				DialogManager.renderDialogCommonElements();
			});
			
			if(window.attachEvent){
				window.attachEvent('onresize', DialogManager.resizeWhiteout);
			} else {
				window.addEventListener('resize', DialogManager.resizeWhiteout, false);
			}
		},
		renderDialogCommonElements: function(){
			var oForm = document.getElementsByTagName('form')[0];
			if(!oForm){alert('Form not found.  Dialog init failed.'); return;}

			var oElem = document.createElement('div');
			oElem.id = 'dialog_whiteout';
			oElem.className = 'dialogWhiteout';
			oForm.appendChild(oElem);
			
			oElem = document.createElement('div');
			oElem.id = 'dialog_positioner';
			oElem.className = 'dialogPositioner';
			oForm.appendChild(oElem);
			
			oElem = document.createElement('div');
			oElem.id = 'dialog_repository';
			oElem.style.display = 'none';
			oForm.appendChild(oElem);
			
			_DialogPositioner = document.getElementById('dialog_positioner');
			_DialogRepository = document.getElementById('dialog_repository');
			_DialogWhiteout = document.getElementById('dialog_whiteout');
			_DialogWhiteout.show = 		function(){ this.style.display = 'block'; }
			_DialogWhiteout.hide = 		function(){ this.style.display = 'none'; }
			_DialogWhiteout.resize = 	function(){
											this.style.width = document.body.scrollWidth+'px';
											this.style.height = document.body.scrollHeight+'px';
										}
			_IsLoaded = true;
			if(_ShowDialogPrematureRequest.length > 0){
				DialogManager.showDialog(_ShowDialogPrematureRequest);
				_ShowDialogPrematureRequest = '';
			}
		},
		registerDialog: function(sId, iWidth, bScrollWithWindow){
			var oDialog = this.Dialog(sId, iWidth, bScrollWithWindow);
			_Dialogs.push(oDialog);
		},
		showDialog: function(sId, bCenter){
			if(!_IsLoaded){
				_ShowDialogPrematureRequest = sId;
				return;
			}
			_UnLoadDialogs();
			for(i=0; i<_Dialogs.length; i++){
				if(_Dialogs[i].Id == sId){
					_DialogWhiteout.show();
					_DialogWhiteout.resize();
					_Dialogs[i].show(bCenter);
					//_DialogInitialState = _Dialogs[i].windowRef().innerHTML;
					_DialogIsOpen = true;
					return;
				}
			}
		},
		hideDialog: function(){
			if(!_IsLoaded){return;}
			//_DialogPositioner.childNodes[0].innerHTML = _DialogInitialState;
			_UnLoadDialogs();
			_DialogPositioner.style.display = 'none';
			_DialogWhiteout.hide();
			_DialogIsOpen = false;
		},
		resizeWhiteout: function(){
			if(!_IsLoaded){return;}
			if(_DialogIsOpen){_DialogWhiteout.resize();}
		}
	}
}();

DialogManager.init();