/*
** This software is part of a product of Xias by U. Kalatchoff & A. Z'Graggen (2005)
** Copying and reusing this software without prior authorization of Xias is explicitely
** forbidden. (contact: info@xias.ch)
*/


/*
** Auxiliary objets and functions
** (window and display mangement,...)
**
** Last modified: 17.10.2005
**
*/

// Fix Mask for window
/*function createWindowMask(){

}
x_runtime.mask=createWindowMask(document);
*/


// Dialog Object
x_runtime.dialogs=new Object();
x_runtime.dialogCounter=0;

function Dialog(dialog){
   this.elt=typeof(dialog)=='object' ? dialog : document.getElementById(dialog);
   this.winObject= arguments.length==1 ? window : arguments[1];
   if(this.elt.id==''){
      this.elt.id='dialog'+x_runtime.dialogCounter;
      x_runtime.dialogCounter++;
   }
   this.id=this.elt.id;
   x_runtime.dialogs[this.id]=this;
   this.onshow=null;
   this.onhide=null;
}


// own methods

Dialog.prototype.show=function(){
   // args[0]: positionment, [1,2]: x,y
   var position=(arguments[0] ? arguments[0] : 'centered');
   var leftPos=(arguments[1] ? arguments[1]+'px' : '0px');
   var topPos=(arguments[2] ? arguments[2]+'px' : '0px');
   var runtime=this.winObject.x_runtime;
   if(runtime.actualDialog && runtime.actualDialog!=this){
      runtime.actualDialog.hide();
   }
   runtime.actualDialog=this;
   var elt=this.elt;
   if(elt.style.visibility!='visible'){
      if(position=='custom'){
         elt.style.left=leftPos;
         elt.style.top=topPos;
      }
      else fixInWindow(elt,'centered');
      x_runtime.blockClickEvents=true;
      setTimeout('x_runtime.blockClickEvents=false;',100);
      runtime.winClickListener.onclick=tempClickHandler;
      elt.style.visibility='visible';
      setTimeout('x_focusFirstDialogInput(x_runtime.dialogs["'+this.id+'"]);',100);
   }
   if(this.onshow)this.onshow();
   return false;
}
   function x_focusFirstDialogInput(dialog){
      var elts=dialog.elt.getElementsByTagName('input');
      if(elts.length>0)elts[0].focus();
   }

Dialog.prototype.hide=function(){
   if(this.winObject.x_runtime){
      this.elt.style.visibility='hidden';
      this.winObject.x_runtime.winClickListener.onclick=null;
      this.winObject.x_runtime.actualDialog=null;
      if(this.onhide)this.onhide();
   }
}

// function to temporarily capture all win events
// - to be part fo Dialog Object?
// - to be replaced with a mask system?
function tempClickHandler(evt){
   if(this.winObject){
      var runtime=this.winObject.x_runtime;
      if(!x_runtime.blockClickEvents){
         evt=x_getDomEvent(evt,this.winObject);
         var elt=runtime.actualDialog.elt;
         if(elt.style.visibility=='visible'){
            if(!hasParent(evt.target,elt) && evt.target!=elt){// if click not on dialog
               runtime.actualDialog.hide();
            }
         }
      }
   }
}

// Function to place an element in a fixed position
function fixInWindow(elt){
   // args[1,2]:where
   var position='centered';
   /*if(arguments.length>1){
      position=
   }*/
   var xPos=Math.floor(document.body.clientWidth-elt.offsetWidth)/2;
   var yPos=Math.floor(document.body.clientHeight-elt.offsetHeight)/2;
   elt.style.left=xPos+'px';
   elt.style.top=yPos+'px';
}