


// Copyright (c) NimbleCat, 2008

/*
* this object implements a modal dialog
* 
* */
// make sure that the required includes are there
 if (typeof NC == 'undefined') {
       alert("modaldialog.js requires the NC JavaScript framework");
 }
 
 if (typeof NC.widget == 'undefined') {
       alert("modaldialog.js requires the NC JavaScript framework");
 }
 
 /*
  * constructor
  */
  
  NC.widget.ModalDialog = function(sourceUrl, parameters, targetDiv, targetForm, showAsPopup) {
		var fnSuperClass = NC.widget.ModalDialog.superclass.constructor;
		fnSuperClass.call( this, -1);
 		this.sourceUrl = sourceUrl;
 		this.showAsPopup = showAsPopup;
 		if (this.sourceUrl.length > 0) {
	 		if (parameters.length > 0) {
		 		if (this.sourceUrl.lastIndexOf('?') == -1) { // no parameters
		 			this.sourceUrl += '?' + parameters;
		 		}
		 		else {
		 			this.sourceUrl += '&' + parameters;
		 		}
	 		}
 		}
 		this.parameters = parameters; // no longer required or used
 		this.target = targetDiv;
 		if (targetForm) {
 			this.targetFormName = targetForm;
 		}
 		else {
 			this.targetFormName = 'PopupForm'; // default
 		}
  }
  
  NC.widget.ModalDialog.OverlayDivName = 'NCDialogOverlay';
  
   	NC.widget.ModalDialog.makeShimId = function ( targetId) {
 		return targetId + "-iShim";
 	}

   NC.widget.ModalDialog.documentMouseUp = function( event) {
			var target  = NC.core.Event.getEventTarget(event);
			var pageX = NC.util.Event.getPageX( event);
			var pageY = NC.util.Event.getPageY( event);
			var dialog = NC.widget.DialogManager.getDialog();
			if (dialog != null) {
				var target = dialog.target;
				var dialogEl = document.getElementById(target);
				var region = NC.util.Region.getRegion(dialogEl);
				//			alert( pageX + ", " + pageY);
				//			alert( region);
				if (pageX >= region.left && pageX < region.right &&
				pageY >= region.top &&
				pageY < region.bottom) {
				// do nothing;
				}
				else {
					NC.widget.ModalDialog.closeDialog(target);
				}
			}
	}
  
  
  NC.widget.ModalDialog.closeDialog = function( targetDiv)
  {
		var element = document.getElementById(targetDiv);
//		var dialog = NC.widget.ResumeUploadManager.getUploadDialog();
		var dialog = element.dialog;
		if (dialog != null && dialog.showAsPopup) { // else no response
			dialog.onHide(targetDiv);
			element.innerHTML = '';
			element.style.display = 'none';
			/* turn off overlays for now
			var transparent = document.getElementById( NC.widget.ModalDialog.OverlayDivName);
			if (transparent != null) {
				transparent.style.visibility = "hidden";
			}
			*/
			var element = document.getElementById( targetDiv);     // get the element
			var shimId = NC.widget.ModalDialog.makeShimId( targetDiv);
			var shimElement = document.getElementById( shimId);
			var oShim = new NC.widget.IFrameShim( shimId, element);
			oShim.setShimIframe( shimElement);
			oShim.hideIframe();
			element.dialog = null;
			NC.widget.DialogManager.setDialog( null);
		}
  }
  
  NC.lang.extend(NC.widget.ModalDialog, NC.widget.Element, 
  {
 	show: function( leftX, topY, widthX, heightY, onShowFn) {
		NC.widget.DialogManager.setDialog( this);
 		responseText = "No response";
	 	var element = document.getElementById(this.target);
	 	element.dialog = this;
	 	var oShim;
 		if (this.showAsPopup) {
			var transparent = document.getElementById( NC.widget.ModalDialog.OverlayDivName);
			/* turn off overlays for now
			if (transparent != null) {
				transparent.style.visibility = "visible";
			}
			*/

			// add iframe shim
			var shimId = NC.widget.ModalDialog.makeShimId( this.target);
			var shim = document.getElementById( shimId);
			oShim = new NC.widget.IFrameShim( shimId, element); // create the shim object
			if (shim == null) { // no iframe yet
				oShim.createIframe();
			}
			else {
				oShim.setShimIframe( shim);
			}
			
	 		// position
			element.style.left=leftX + "px";
			element.style.top=topY + "px";
			if (widthX > 0) {
				element.style.width=widthX + "px";
			}
			if (heightY > 0) {
				element.style.height=heightY + "px";
			}
			element.style.display = 'block';
//  			oShim.config();
			NC.widget.Element.addDocumentDomEvent(NC.core.EventType.Mouseup, NC.widget.ModalDialog.documentMouseUp, false);
 		}
 		if (this.sourceUrl.length > 0) {
			var request = null;
			if (this.showAsPopup) {
				// add close link
				request = new NC.util.AjaxDialogDisplayExtended( 'GET', this.sourceUrl, this.target,  this.target, null, onShowFn);
			}
			else {
				request = new NC.util.AjaxUpdate( 'GET', this.sourceUrl, this.target,  this.target, null);
			}
			request.doUpdate();
	 	}
        setTimeout( 
            function() {
				if (oShim != null) {
		  			oShim.config();
				}
            }, 1000);
		
		this.onShow(this.target);
 	},

 	
 	hide: function() {
 		NC.widget.ModalDialog.closeDialog( this.target);
 	},
 	
 	onShow: function() {
 		// override if you want something to happen on show
 	},
 	
 	onHide: function() {
 		// override if you want something to happen on hide
 	},
 	
 	setShowFn: function( showFn) {
 		this.onShow = showFn;
 	},
 	
 	setHideFn: function( hideFn) {
 		this.onHide = hideFn;
 	},
 	
 	post: function() {
 		alert('Override this post function with your own');
 	}
  	
  });
  
   (function () {

 NC.widget.DialogManager = function() {
	/* private variables and methods */
 	var dialog = null;
 	
 	 	return {
  	 		
 			setDialog: function( _dialog) {
				dialog = _dialog;
			},
			
			getDialog: function() {
				return dialog;
			}
 	 	}
  }	
();

})(); 	
 

