/* ModalDialogManager class. Copyright by ADenis (adeniss@ukr.net) */
/* IE6 buletproof by Kostya Kulikov (kostiko@mail.ru) */

function ModalDialogManager(){
	
	var overlay = null;
	var container = null;
	var owner = null;
	var object = null;
	var clone = null;
	
	this.onHide = null;
	this.onShow = null;
	
	this.visible = false;
	
	this.selectElementArray = null;
	this.callbackFunction = null;
	
	this.createOverlay = function (){		
		overlay = document.createElement("div");		
		overlay.setAttribute("id","modalDialogOverlay");		
		overlay.style.display = "none";

		container = document.createElement("div");
		container.setAttribute("id","modalDialogContainer");		
		container.style.display = "none";
		
		document.body.insertBefore(overlay, document.body.firstChild);		
		document.body.appendChild(container);
	}
	
	this.open = function (dialog, makeClone, method, callback) {
		this.close();
		
		if(this.isIE6()) {
			this.disableControlElements(dialog);
			this.callbackFunction = callback;
			callback = this.onDialogLoaded;
		}
		
		if ($(dialog))
			object = $(dialog);
		
		if (object && object.tagName) {
			if (makeClone) {
				clone = object.cloneNode(true);			
				clone.style.display = "block";
				container.appendChild(clone);
			} else {
				owner = object.parentNode;
				container.appendChild(object);
				object.style.display = "block";
			}
			this.showIframeIE();
		} else if (dialog.indexOf(".html") > -1) {
			dialog = dialog.replace("http://"+document.location.host+"/", "");
			
			var params = dialog.split("?");
			var page = params[0];
			var request = (params[1])?params[1]:"";
			
			getDataUpdater().updateObject(page, request, container.getAttribute("id"), callback, false, method);
		} else {
			container.innerHTML = dialog;
			this.showIframeIE();
		}

		this.showOverlay();
	}
	
	this.close = function () {
		if(this.isIE6()) {
			this.enableControlElements();
		}
		
		this.hideOverlay();
		
		if (clone && clone.parentNode) {
			clone.parentNode.removeChild(clone);
			clone = null;
		}
			
		if (object && owner) {
			object.style.display = "none";
			owner.appendChild(object);
			
			owner = null;
			object = null;
		}	
			
		container.innerHTML = "";
		this.showIframeIE(false);
	}
	
	this.showOverlay = function (hide) {
		if (!hide) {
			
			if(!$("faContent")) { //Fuse File-Admin workaround
				window.scroll(0,0);
				document.body.style.overflow = "hidden";
			} else {
				overlay.style.height = $("faContent").offsetHeight + "px";
			}
			
			container.style.display = "block";
			overlay.style.display = "block";

			this.visible = true;
			
			if(this.onShow)
				this.onShow();
		} else {
			
			container.style.display = "none";
			overlay.style.display = "none";
			
			if(!$("faContent")) { //Fuse File-Admin workaround
				document.body.style.overflow = "visible";
			}
			
			this.visible = false;
			
			if(this.onHide)
				this.onHide();
		}
	}
	
	this.hideOverlay = function () {
		this.showOverlay(true);
	}
	
	this.setLabels = function (object, labelName, labelValue) {
		var childs = getChildsByName(object, labelName);
		for (var child in childs){
			childs[child].innerHTML = labelValue; 
		}
	}

	this.isIE6 = function() {
		if(navigator.userAgent.indexOf('MSIE 6.0')>=0 && navigator.userAgent.indexOf('Opera')<0) return true;
		return false;
	}
	
	this.onDialogLoaded = function() {
		dialogManager = getModalDialogManager();
		if(dialogManager.callbackFunction){
			if(typeof(dialogManager.callbackFunction) == "string")
				dialogManager.callbackFunction = new Function(dialogManager.callbackFunction);
			dialogManager.callbackFunction();
		}
		dialogManager.showIframeIE();
	}
	
	this.showIframeIE = function (show) {
		if(!this.isIE6) return;
		if(show === false) this.onDialogHideIE()
		else this.onDialogShowIE();		
	}

	this.onDialogShowIE  = function () {
		var dialogElem = $("dialogBox");
		if(!dialogElem) return;
		
		var iframeContainer = $("iframeContainer");
		
		if(!iframeContainer) {
			var iframeContainer = document.createElement("div");
			iframeContainer.setAttribute("id","iframeContainer");
			
			iframeElem = document.createElement("iframe");
			iframeElem.setAttribute("id","iframeWorkaroundIE");
			
			iframeContainer.appendChild(iframeElem);
			document.body.insertBefore(iframeContainer, container);
			//document.body.appendChild(iframeContainer);
		}
		
		this.resizeIframe(dialogElem);
		removeClass(iframeContainer, "hidden");
		dialogElem.onresize = this.onDialogResizeIE;
	}

	this.onDialogResizeIE = function (e) {
		//alert(event.srcElement);
		dialogElem = event.srcElement;
		getModalDialogManager().resizeIframe(dialogElem);
	}
	
	this.onDialogHideIE = function () {
		var iframeContainer = $("iframeContainer");
		if(!iframeContainer) return;
		addClass(iframeContainer, "hidden");
	}

	this.resizeIframe = function (dialogElem) {
		var iframeElem = $("iframeWorkaroundIE");
		if(!iframeElem) return;
		var dialogBorderWidth = 2;

		iframeElem.style.width = (dialogElem.clientWidth + 2*dialogBorderWidth) + "px";
		iframeElem.style.height = (dialogElem.clientHeight + 2*dialogBorderWidth) + "px";
		iframeElem.style.marginTop = dialogElem.currentStyle.marginTop;
	}
	
	this.disableControlElements = function(dialog) {
		var dialogInputsArray = null;
		var dialogSelectsArray = null;
		
		if ($(dialog)) {
			dialogSelectsArray = $(dialog).getElementsByTagName("select");
			dialogInputsArray = $(dialog).getElementsByTagName("input");
		}

		var documentSelectElementArray = document.getElementsByTagName("select");
		var documentInputElementArray = document.getElementsByTagName("input");
		
		this.selectElementArray = new Array();
		
		var skip = false;
		for(var i in documentSelectElementArray) {
			if(dialogSelectsArray) {
				for(var j in dialogSelectsArray) {
					if(documentSelectElementArray[i] == dialogSelectsArray[j]) {
						skip = true;
						break;
					} 
				}
			}
			if(!documentSelectElementArray[i].disabled && !skip) {
				skip = false;
				documentSelectElementArray[i].disabled = true;
				this.selectElementArray.push(documentSelectElementArray[i]);
			}
		}
		
		skip = false;
		for(var y in documentInputElementArray) {
			if(dialogInputsArray) {
				for(var j in dialogInputsArray) {
					if(documentInputElementArray[y] == dialogInputsArray[j]) {
						skip = true;
						break;
					} 
				}
			}
			if(!documentInputElementArray[y].disabled && !skip) {
				skip = false;
				documentInputElementArray[y].disabled = true;
				this.selectElementArray.push(documentInputElementArray[y]);
			}
		}
	}
	
	this.enableControlElements = function() {
		for(var i in this.selectElementArray) {
			this.selectElementArray[i].disabled = false;
		}
		this.selectElementArray = null;
	}

	
	this.createOverlay();
}

var modalDialogManager = null;

function getModalDialogManager() {
	if (!modalDialogManager) {
		modalDialogManager = new ModalDialogManager();
	}
	
	return modalDialogManager;
}