/**
 * Part of JMF Library
 * This file contains modal dialog widget
 * SVN: $Id: modal.js 6163 2008-08-14 14:36:31Z mjezierski $
 */

/**
 * Modal dialog constructor
 * @constructor
 * @member JMF.Widgets.Modal
 * @param {Object} props Dialog properties
 */
JMF.Widgets.Modal = function(props) {
   props = props || {};
   props.attributes = props.attributes || {};
   props.attributes['class'] = props.attributes['class'] || 'mdM';
   props.style = props.style || {};
   
   props.style.position = props.style.position?props.style.position:JMF.browser.FF?'fixed':'absolute'; 
   props.style.display = 'none';

   JMF.Layout.call(this,props);

	this.headClass = props.headClass || 'mdH';
	this.headCancelBtnClass = props.hcBtnClass || 'mdHCBnt';
	this.contentClass       = props.cntClass || 'mdCnt';
	this.buttonbarClass     = props.bBarClass || 'mdBBar';
	
	this.head = new JMF.Layout({attributes:{'class':this.headClass}});

	this.cHBtn = new JMF.Layout({attributes:{'class':this.headCancelBtnClass,href:'#!'}},'a');


   this.content = new JMF.Layout({attributes:{'class':this.contentClass}});
	this.buttonbar = new JMF.Layout({attributes:{'class':this.buttonbarClass}});
	
	var instance = this;
	this.cHBtn.addListener('focus',function(evt){JMF.Events.event(evt).stopPropagation();evt.target.blur();});
	this.cHBtn.addListener('click',function(evt){JMF.Events.event(evt).stopPropagation();evt.preventDefault();instance.cancel();});
	
	//build tree
	JMF.Layout.prototype.appendChild.call(this,this.head);
	JMF.Layout.prototype.appendChild.call(this,this.content);
	JMF.Layout.prototype.appendChild.call(this,this.buttonbar);
	
	this.head.appendChild(this.cHBtn);
		
	//dnd
	var JEDD = JMF.Effects.Dragndrop.Drag;
   JEDD.setFlags(JEDD.F_MODE_USEVIEWPORTBOX,'md_modal');
   JEDD.add(this.DOMRef,'md_modal',this.head.DOMRef); 
};

JMF.inherit(JMF.Widgets.Modal,JMF.Layout);

/**
 * Shows dialog (also attaches to DOM)
 * @member JMF.Widgets.Modal
 */
JMF.Widgets.Modal.prototype.show = function() {
   if(this.DOMRef.parentNode !== document.body) {
      this.appendDOM(document.body);   	
   }

   this.cssStyle({visibility:'hidden',display:'block'});
   this.center();

   var zIndex  = JMF.Widgets.Pagecover.getZIndex();
	JMF.Widgets.Pagecover.setZIndex(zIndex+2);
	JMF.Widgets.Pagecover.show();
	this.cssStyle({zIndex:zIndex+3,visibility:'visible'});
};

/**
 * Default method called when cancel cross is clicked
 * This method can be ovverriden
 * @member JMF.Widgets.Modal
 */
JMF.Widgets.Modal.prototype.cancel = function() {
	this.hide();
};

/**
 * Hides modal dialog
 * @member JMF.Widgets.Modal
 */
JMF.Widgets.Modal.prototype.hide = function() {
	this.cssStyle({display:'none'});
	var zIndex  = JMF.Widgets.Pagecover.getZIndex();
	JMF.Widgets.Pagecover.setZIndex(zIndex-2);
	JMF.Widgets.Pagecover.hide();
};

/**
 * Overriden appendChild method
 * @member JMF.Widgets.Modal
 * @param {Object} childNode JMF.Layout object to add 
 */
JMF.Widgets.Modal.prototype.appendChild = function(childNode) {
	this.content.appendChild(childNode);
};

/**
 * Overriden removeChild method
 * @member JMF.Widgets.Modal
 * @param {Object} childNode JMF.Layout object to remove 
 */
JMF.Widgets.Modal.prototype.removeChild = function(childNode) {
	this.content.removeChild(childNode);
};

/**
 * Overriden insertBefore method
 * @member JMF.Widgets.Modal
 * @param {Object} childNode JMF.Layout object to insert
 * @param {Object} siblingNode Node to insert before, if null, object will be added at the begining 
 */
JMF.Widgets.Modal.prototype.insertBefore = function(childNode,siblingNode) {
	this.content.insertBefore(childNode,siblingNode);
};

/**
 * Centers mnodal dialog in current vieport
 * Automatically called on show
 * @member JMF.Widgets.Modal
 */
JMF.Widgets.Modal.prototype.center = function() {
	var bnds = this.DOMRef.getBounds();
	var vp = JMF.Domutil.getViewport();
	
	var top = Math.round(vp.height/2 - bnds.height/2 + vp.top);
	var left = Math.round(vp.width/2 - bnds.width/2 + vp.left);

   if(this.DOMRef.style.position == 'fixed') {
   	top -= vp.top;
   	left -= vp.left;
   }

	if(top < 0) {
		top = 0;
	}

	if(left < 0) {
		left = 0;
	} 
	
   this.cssStyle({top:top+'px',left:left+'px'});	
};

/**
 * Overriden destructor
 * @member JMF.Widgets.Modal
 */
JMF.Widgets.Modal.prototype.destroy = function() {
	JMF.Effects.Dragndrop.Drag.remove(this.DOMRef,'md_modal');
   if(this.DOMRef.parentNode) {
   	this.DOMRef.parentNode.removeChild(this.DOMRef);
   }
	JMF.Layout.prototype.destroy.call(this);
};