/*
    dialog.js
    ====================================
    Create and use a dialog window
    Cross browser version
    By Giuliano Cesar <giulianojordao@yahoo.com.br>
*/
// Though "Dialog" looks like an object, it isn't really an object.  Instead
// it's just namespace for protecting global symbols.

function Dialog(url, width, height, name, scroll, action, init) {
    var isIE = false;
    var isOther = false;
    var isNS4 = false;
    var isNS6 = false;
    if(document.getElementById) 
    {
    	if(!document.all) { isNS6=true; }
    	if(document.all)  { isIE=true; 	}
    }
    else
    {
    	if(document.layers) { isNS4=true; }
    	else { isOther=true; }
    }
     
     if (typeof init == "undefined") {
          init = window;     // pass this window object by default
     }
     if (isIE) {
          var value =
               showModalDialog(url, init,
                         "dialogWidth:"+width+"px; dialogHeight:"+height+"px; resizable: no; help: no; status: no; scroll: "+scroll);
          if (action) {
               action(value);
          }
     } else {
          return Dialog._geckoOpenModal(url, action, init, name, width, height, scroll);
     }
};

Dialog._parentEvent = function(ev) {
     if (Dialog._modal && !Dialog._modal.closed) {
          Dialog._modal.focus();
          // we get here in Mozilla only, anyway, so we can safely use
          // the DOM version.
          ev.preventDefault();
          ev.stopPropagation();
     }
};

// should be a function, the return handler of the currently opened dialog.
Dialog._return = null;

// constant, the currently opened dialog
Dialog._modal = null;

// the dialog will read it's args from this variable
Dialog._arguments = null;

Dialog._geckoOpenModal = function(url, action, init, name, width, height, scroll) {
//     var nazwa=null? "Dialog":name;
     var dlg = window.open(url, name,"toolbar=no,menubar=no,personalbar=no," +
                                      "width="+width+",height="+height+",scrollbars="+scroll+",resizable=no," +
                                      "dependent=yes,z-lock=yes");
     Dialog._modal = dlg;
     Dialog._arguments = init;

     // capture some window's events
     function capwin(w) {
          w.addEventListener("click", Dialog._parentEvent, true);
          w.addEventListener("mousedown", Dialog._parentEvent, true);
          //w.addEventListener("focus", Dialog._parentEvent, true);
     };
     // release the captured events
     function relwin(w) {
          //w.removeEventListener("focus", Dialog._parentEvent, true);
          w.removeEventListener("mousedown", Dialog._parentEvent, true);
          w.removeEventListener("click", Dialog._parentEvent, true);
     };
     capwin(window);
     // capture other frames
     for (var i = 0; i < window.frames.length; capwin(window.frames[i++]));
     // make up a function to be called when the Dialog ends.
     Dialog._return = function (val) {
          if (val && action) {
               action(val);
          }
          relwin(window);
          // capture other frames
          for (var i = 0; i < window.frames.length; relwin(window.frames[i++]));
          Dialog._modal = null;
     };
};


