


// 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.MessageBox = function(sourceUrl, parameters, targetDiv, targetForm, showAsPopup, handlers) {
		var fnSuperClass = NC.widget.MessageBox.superclass.constructor;
		fnSuperClass.call( this, -1);
 		this.sourceUrl = sourceUrl;
 		this.showAsPopup = showAsPopup
 		this.handlers = handlers;
 		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.MessageBox.OverlayDivName = 'NCDialogOverlay';
  
   	NC.widget.MessageBox.makeShimId = function ( target) {
 		return target + "-iShim";
 	}
 
   NC.widget.MessageBox.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 messageEl = document.getElementById( NC.widget.MessageManager.getMessageDialog().target);
			var region = NC.util.Region.getRegion( messageEl);
//			alert( pageX + ", " + pageY);
//			alert( region);
			if (pageX >= region.left && pageX < region.right &&
			pageY >= region.top &&
			pageY < region.bottom) {
				// do nothing;
			}
			else {
				NC.widget.MessageManager.hideDialog();
			}
	}
  
 
  
  NC.widget.MessageBox.closeDialog = function( )
  {
		NC.widget.Element.removeDocumentDomEvent(NC.core.EventType.Mouseup, NC.widget.MessageBox.documentMouseUp, false);
		var element = document.getElementById(this.target);
		var dialog = element.dialog;
		if (dialog.showAsPopup) { // else no response
			element.innerHTML = '';
			element.style.display = 'none';
			/* turn off overlays for now
			var transparent = document.getElementById( NC.widget.MessageBox.OverlayDivName);
			if (transparent != null) {
				transparent.style.visibility = "hidden";
			}
			*/
			var element = document.getElementById( this.target);     // get the element
			var shimId = NC.widget.MessageBox.makeShimId( this.target);
			var shimElement = document.getElementById( shimId);
			var oShim = new NC.widget.IFrameShim( shimId, element);
			oShim.setShimIframe( shimElement);
			oShim.hideIframe();
		 	if (this.handlers.ok) {
				this.okButton.removeDomEvent(this.target, NC.core.EventType.Click, this.onOK, false);
		 	}
		 	if (this.handlers.cancel) {
				this.cancelButton.removeDomEvent(this.target, NC.core.EventType.Click, this.onCancel, false);
		 	}

			element.dialog = null;
		}
  }
  
  NC.lang.extend(NC.widget.MessageBox, NC.widget.Element, 
  {
  	NCMessageBoxOk: 'NCMessageBoxOK',
  	NCMessageBoxCancel: 'NCMessageBoxCancel',
  	
 	show: function( widthX, leftX, topY) {
		responseText = "No response";
	 	var element = document.getElementById(this.target);
	 	element.dialog = this;
	 	var messageDiv = document.createElement('div');
	 	messageDiv.id = this.target + '_content';
	 	var buttonDiv = document.createElement( 'div');
	 	buttonDiv.id = this.target + '_buttonbar';
	 	var buttonContent = "";
	 	if (this.handlers.ok) {
			buttonContent += '<input type="button" border="0"  title="" class="ncbutton" name="okButton" id="' + this.NCMessageBoxOk + '" value="Ok"> ';
	 	}
	 	if (this.handlers.cancel) {
			buttonContent += '<input type="button" border="0"  title="" class="ncbutton" name="cancelButton" id="' + this.NCMessageBoxCancel + '" value="Cancel"> ';
	 	}
//	 	var oTextNode = document.createTextNode(buttonContent);
//	 	buttonDiv.appendChild( oTextNode);
		buttonDiv.innerHTML = buttonContent;
	 	
		element.innerHTML = '';
	 	element.appendChild(messageDiv);
	 	element.appendChild(buttonDiv);

	 	if (this.handlers.ok) {
			this.okButton = new NC.widget.Button(this.NCMessageBoxOk);
			this.okButton.init();
			this.okButton.addDomEvent(NC.core.EventType.Click, this.onOK, false);
			buttonElement = document.getElementById( this.NCMessageBoxOk);
			buttonElement.dialog = this;
	 	}
	 	if (this.handlers.cancel) {
			this.cancelButton = new NC.widget.Button(this.NCMessageBoxCancel);
			this.cancelButton.init();
			this.cancelButton.addDomEvent(NC.core.EventType.Click, this.onCancel, false);
			buttonElement = document.getElementById( this.NCMessageBoxCancel);
			buttonElement.dialog = this;
	 	}

		var funcIndex = this.sourceUrl.indexOf( NC.util.AjaxManager.getNcFuncFlag());
		if ( funcIndex > -1) {
			var ncFunc = this.sourceUrl.substring( funcIndex + NC.util.AjaxManager.getNcFuncFlag().length);
			if (ncFunc == 'showcategorybrowser') {
//				messageDiv.innerHTML = " here's the content";
				NC.widget.CategoryManager.showCategoryTree( this.target, messageDiv.id);
			}
			else {
				alert( 'unknown function: ' + ncFunc)
			}
		}
		else {
			var request = null;
				request = new NC.util.AjaxUpdate( 'GET', this.sourceUrl, messageDiv.id,  messageDiv.id, null);
			request.doUpdate();
		}

	 	var oShim;
 		if (this.showAsPopup) {
 			/* turn off overlays for now
			var transparent = document.getElementById( NC.widget.MessageBox.OverlayDivName);
			if (transparent != null) {
				transparent.style.visibility = "visible";
			}
			*/
			// add iframe shim
			var shimId = NC.widget.MessageBox.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
			if (typeof leftX == 'undefined' || leftX == -1) {
	 			leftX = (NC.util.Dom.getViewportWidth() - widthX)/2 ;
			}
			if (typeof topY == 'undefined' || topY == -1) {
	 			topY = (NC.util.Dom.getViewportHeight() - element.style.height)/3;
			}
			element.style.left=leftX + "px";
			element.style.top=topY + "px";
			if (widthX > 0) {
				element.style.width=widthX + "px";
			}
			element.style.display = 'block';
//  			oShim.config();
			NC.widget.Element.addDocumentDomEvent(NC.core.EventType.Mouseup, NC.widget.MessageBox.documentMouseUp, false);
 		}
 		else {
			element.style.display = 'block';
 		}
        setTimeout( 
            function() {
				if (oShim != null) {
		  			oShim.config();
				}
            }, 1000);
 	},

 	
 	hide: function() {
		NC.widget.MessageBox.closeDialog( );
 	},
 	
 	post: function() {
 		alert('Override this post function with your own');
 	}, 
 	
 	onOK: function( event) {
		var button = NC.core.Event.getEventTarget(event);
 		NC.widget.MessageManager.hideDialog();
 		button.dialog.handlers.ok( event, button.dialog.handlers);
 	},
 	
 	onCancel: function( event) {
		var button = NC.core.Event.getEventTarget(event);
 		NC.widget.MessageManager.hideDialog();
 		button.dialog.handlers.cancel( event, button.dialog.handlers);
 	}
  	
  });
  
 (function () {

 NC.widget.MessageManager = function() {
	/* private variables and methods */
 	var dialog = null;
 	
 	 	return {
 	 		/* public methods */
 	 		

 	 		showDialog: function( url, divId, callback, showAsPopup, widthX, leftX, topY) { 
				dialog = new NC.widget.MessageBox( url, '', 
				divId, divId, showAsPopup, callback);
				if (widthX == -1) {
					widthX = 300;
				}
				dialog.show( widthX, leftX, topY);
 	 		},
 	 		
 	 		hideDialog: function( ) { 
		 		NC.widget.MessageBox.closeDialog.call( dialog );
		 		dialog = null;
 	 		},
 	 		
 	 		isDialogShown: function() {
 	 			return ( dialog == null ? false: true);
 	 		},
			
			getMessageDialog: function() {
				return dialog;
			}
 	 	}
  }	
();

})(); 	
 
  
