ModalDialog = function ModalDialog (div_content_id, style, width, height) {
	// div_content_id - content div id
	// width - width of modal dialog
	// height - height of modal dialog
	// style - style of fojo frame
   if (!div_content_id) return null;
   Core.flushElementCache();   

	this.content = (Core.getElementU(div_content_id) ? Core.getText(div_content_id) : div_content_id);
	this.width =  (width != null ? width : 0);	// width of dialog
	this.height = (height != null ? height : 0);	// height of dialog 
	this.style = (style ? style : 'note');	// frame style
	this.div_id =   div_content_id;	// id of div-container
	this.cont_id =  div_content_id+"_dlg";	// id contents (for height change)
	this.width_id = div_content_id+"_width";	// id contents and frame (for width change)
   this.fojo_id =  "fojo_"+div_content_id;
   this.bg_id = "fojo_bg_"+div_content_id;
	this.visible = 0;
	this.effect = 'none';//use .setEffect() to set effect
	this.is_scroll_on = 0;
   this.close_on_esc = 1;
	this.topNav_offset = 0;
	var this_ref = this; 	

	this.init = function ModalDialog_init () {
      var test_obj = Core.getElementU(div_content_id);

      if (test_obj.parentNode && test_obj.parentNode.tagName != 'BODY') {
         //put the dialog content into the BODY, so no other elements overlap it 

         window[this.div_id+'_dialog_onload'] = function dialog_onload() {
            var new_div = document.createElement("div");
            new_div.setAttribute("id", div_content_id);
            new_div.setAttribute("style", "display: none;");
            new_div.innerHTML = Core.getText(div_content_id);
           
            test_obj.parentNode.removeChild(test_obj);
            //if (document.getElementsByTagName("body")[0])
            //document.getElementsByTagName("body")[0].appendChild(test_obj);
            Core.getDocumentBody().appendChild(test_obj);
            Core.flushElementCache();
         };
         Core.addEvent(window, 'load', window[this.div_id+'_dialog_onload']);

      }

		var arrayPageSize = Core.getPageSize();
		var top_scroll = 0;//FIXME - not appeared yet - this.getTopOffset(Core.getPageScrollTop(), arrayPageSize[3]); 
      var this_width = (this.width == 0 ? 'auto' : this.width+'px');
      var this_height = (this.height == 0 ? 'auto' : this.height+'px');
      var bg_width = (this.is_scroll_on || !Core.detectBrowser().isFirefox? arrayPageSize[2]*4 + 'px' : '100%');
      var bg_height = (this.is_scroll_on || !Core.detectBrowser().isFirefox? arrayPageSize[1] + 'px' : '100%');

      //FIXME LATER: to optimize this code - consider using array.push + join instead of string concatenation!
		if(Core.detectBrowser().isSafari || Core.detectBrowser().isIE) {
      	this.topNav_offset = Core.getElementHeight('sub') || 0; // Safari hack
      }

		var content_str_fojo_bg1 =
		"<div class='fojo_bg fojo90' id='"+this.bg_id+"' style='display:none; "+(Core.detectBrowser().isSafari?'top:-'+this.topNav_offset+'px;':'')+" height:"+bg_height+"; width:"+bg_width+"'></div>"+
			"<div class='fojo "+this.style+"' id='"+this.fojo_id+"' style='display:none;'>";

      var content_str_fojo_wrap1 = 
			"<table class='framewrapper' id='"+this.width_id +
            "' style='margin:0 auto; z-index:9999; position:relative;top: "+top_scroll+"px;'>"+
         "<tr><td>" +
			"<div class='lefttop'></div>"+
			"<div class='righttop'></div>"+
			"<div class='centertop'></div>"+
			"<div id='"+this.cont_id+"' class='blockcontent' style='padding:0 4px 0 4px; "+
         "height:"+this_height+"; width:"+this_width+"; " + 
         "'>";

		var content_str_fojo_wrap2 = 
         "</div><div class='leftbottom'></div>"+
			"<div class='rightbottom'></div>"+
			"<div class='centerbottom'></div>"+
			"</td></tr></table>";

      var content_str_fojo_bg2 = 
         "</div>";


      if (Core.getElementU(this.fojo_id)) {//already inited before - prevents bg blinking
         var content_str = content_str_fojo_wrap1 + this.content + content_str_fojo_wrap2;
   		Core.setText(this.fojo_id, content_str);
      } else {
         var content_str = content_str_fojo_bg1 + content_str_fojo_wrap1 + this.content + content_str_fojo_wrap2 + content_str_fojo_bg2;
   		Core.setText(this.div_id, content_str);
      }
      Core.flushElementCache();

//      if (!this.aa1) {
//         this.aa1 = Core.getElementU(this.cont_id);
//      }


      this.clearCustomScrollableContainerCache();

      this.stayTop('e');
	}
	
   this.previous_scroll = 0;

	this.stayTop = function ModalDialog_stayTop (caller) {
      if (!this.isVisible()) return;
		var objFojoDiv = Core.getElementU(this.width_id);
      var scroll = (this.is_scroll_on ? Core.getPageScrollTop() : 0);
		var arrayPageSize = Core.getPageSize();
		var top_scroll = this.getTopOffset(scroll, arrayPageSize[3]);
	
		if(objFojoDiv && objFojoDiv.style) {
			objFojoDiv.style.top = top_scroll +'px';
		}
		
		var bg = Core.getElementU(this.bg_id);
      if (!Core.detectBrowser().isFirefox && !Core.detectBrowser().isSafari && bg) { 
         var dim = Core.getPageDimensions();
//			var dim = Core.getScreenDimensions();
         bg.style.width = dim.width + 'px';
			bg.style.height = dim.height + 'px';
      }

      //add scroll if the available screen is realy small and is not fitting whole dialog
      var bva = Core.getBrowserVisibleArea();
      var dialog_content = Core.getElementU(this.fojo_id);
      var content = Core.getElementSize(this.cont_id);

//      if (!this.aaa) {
//         this.aaa = Core.getElementU(this.fojo_id);
//         this.aab = Core.getElementSize(this.cont_id);
//      }
//this.ah0 = Core.getElementSize(this.cont_id).height;
//this.ah1 = Core.getElementU(this.cont_id).offsetHeight; 
//this.ah2 = Core.getElementU(this.cont_id).clientHeight; 
//this.ah3 = Core.getElementU(this.cont_id).height; 
//this.ah4 = Core.getElementU(this.cont_id).style.height;
      var avail_height = bva.height - 12;

      if (avail_height < content.height) {
         dialog_content.style.overflow = 'auto';
         dialog_content.style.height = avail_height+'px';
         this.dialog_is_scrollable = this.dialog_is_scrollable+1 || 0 ;
      } else {
         if (dialog_content) {
            dialog_content.style.height = 'auto';
            dialog_content.style.overflow = 'visible';
         }
         this.dialog_is_scrollable = 0;
      }


      //if we have set explicitly internal scrollable container, then we scroll it too:

      if (!this.scrollable_content_id && Core.getElementU('panel_content')) {
         //by default / or use this.setCustomScrollableContainerID
         this.scrollable_content_id = 'panel_content';
      }

      if (this.scrollable_content_id) {      
         var dialog_alt_content = Core.getElementU(this.scrollable_content_id);
         var alt_content = Core.getElementSize(this.scrollable_content_id);
       
         if (this.dialog_is_scrollable >1) { //<hack "catch resized height by CSS (with scroll)">
            if(!this.alt_h) {
               this.alt_h =  alt_content.height;
               this.paddings = content.height -  alt_content.height + 18;
//               this.paddings = content.height -  alt_content.height;
            }
         }
       
         var h = avail_height - this.paddings;
//          document.title = bva.height +'x'+ bva.width + ' - ' + content.height +'x'+ content.width + 
//                          ' - ' + alt_content.height +'('+ this.alt_h +')x'+ alt_content.width + ' - ' + this.paddings + ' h='+h ;
       
         if (h < this.alt_h) {      
            if (h < 70) h = 70;
            dialog_alt_content.style.height = (h)+'px';
         } else {
            if (this.alt_h) dialog_alt_content.style.height = this.alt_h + 'px';
         }
      }
		//this.height = Core.getElementU(this.fojo_id).offsetHeight-15;
	}

   this.scrollable_content_id = null;

   this.setCustomScrollableContainerID = function(id) {
      this.scrollable_content_id = id;
      this.clearCustomScrollableContainerCache();
   }

   this.clearCustomScrollableContainerCache = function() {
      if (this.scrollable_content_id && this.alt_h){
         var dialog_alt_content = Core.getElementU(this.scrollable_content_id);
         dialog_alt_content.style.height = 'auto';//this.alt_h + 'px';
      }

      this.alt_h = null;
      this.paddings = null;
      this.dialog_is_scrollable = null; 
   }
   
	this.show = function ModalDialog_show () { 
      if (this.isVisible()) return;//XXX init()?
	   this.setVisible(1);

      this.clearCustomScrollableContainerCache();

		this.onShowing();

		window[this.div_id+'_ref'] = this;
      
		if(!this.is_scroll_on) { 
         this.last_scroll_y_position = Core.getPageScrollTop();
         if (Core.detectBrowser().isIE) {
   			document.documentElement.style.overflow = "hidden";
         } else {
   			document.body.style.overflow = "hidden";
         }
		} else {
      	//Core.getElementU(fojo_bg_id).style.width = '100%';
		}

      this._showAndReposition = function () {
         Core.showElement(this.fojo_id);
         this.stayTop('k');
         window.scroll(0, this.last_scroll_y_position);
      }

		if (this.effect == 'fade') {
   	   Core.setClassName(this.bg_id, 'fojo_bg fojo10');
	      Core.hideElement(this.fojo_id);
         for (i=1; i<10; i++) {
			   window.setTimeout("Core.setClassName('"+this.bg_id+"', 'fojo_bg fojo"+(i*10)+"');", 80 + 40*i);
         }

			window.setTimeout("window." +this.div_id+'_ref._showAndReposition();', 500);

		} else if (this.effect == 'none') { 
         Core.setClassName(this.bg_id, 'fojo_bg fojo90');
         this._showAndReposition();
			//Core.showElement(this.fojo_id);
		}
      
      //This will load asyncronously before previous setTimeouts! 
  	   Core.showElement(this.bg_id); 
   	Core.showElement(this.div_id);

//		if(!this.height) { 
//			this.stayTop('a');
//		}
	

      this.prev_window_onkeypress = window.onkeypress;

      this.onKeyPress = function (e) {
         if (!e) var e = window.event;
         var code = e.keyCode;

         if (this_ref.close_on_esc == 1 && this_ref.isVisible() && code==27) {//ESC
            this_ref.onESC();
            return false;
         }
      }

      Core.addSystemEvent('keypress',  this.onKeyPress);

   	this.stayTop('b');

      this.reposition_interval_id = window.setInterval("window." +this.div_id+'_ref.stayTop("c")', 300);

		this.onShow();  
	}

	this.hide = function ModalDialog_hide () {
      if (!this.isVisible()) return;
		this.onHiding();

      window.clearInterval(this.reposition_interval_id);
      

		if(this.effect == 'fade') { 
         for (i=9; i>0; i--) {
			   window.setTimeout("Core.setClassName('"+this.bg_id+"','fojo_bg fojo"+(i*10)+"');", 80 + 30*(10-i));
         }
			window.setTimeout("Core.hideElement('"+this.bg_id+"');", 700);
			window.setTimeout("Core.hideElement('"+this.fojo_id+"');", 100);
			window.setTimeout("Core.hideElement('"+this.div_id+"');", 700);
		} else if (this.effect == 'none') {
			Core.hideElement(this.bg_id);
			Core.hideElement(this.fojo_id);
			Core.hideElement(this.div_id);
		}

		if(!this.is_scroll_on) {
         if (Core.detectBrowser().isIE) {
   			document.documentElement.style.overflow = "auto";
         } else {
   			document.body.style.overflow = "auto";
         }

         window.scroll(0, this.last_scroll_y_position); 
      }

      Core.removeSystemEvent('keypress', this.onKeyPress);
      window.onkeypress = this.prev_window_onkeypress;
      this.clearCustomScrollableContainerCache();      
		this.setVisible(0);

		this.onHide();
	}


   this.quickHide = function () {
      Core.hideElement(this.fojo_id);
   }

   this.quickShow = function () {
      Core.showElement(this.bg_id);
      Core.showElement(this.fojo_id);
      Core.showElement(this.div_id);
      this.stayTop('quick');
   }

	this.setContent = function ModalDialog_setContent (cont) {
      //NB! Avoid using this function directly!
      this.quickHide();
		this.content = cont;
      this.init();
      this.quickShow();
   }

	this.getContent = function ModalDialog_getContent () {
		return this.content;
	}

	this.setWidth = function ModalDialog_setWidth (w) {
		this.width = w;
		var objFojoWidth = Core.getElementU(this.width_id);
		objFojoWidth.style.width = this.width + 'px';
	}

	this.setHeight = function ModalDialog_setHeight (h) {
      alert('This function is not implemented completely yet (ModalDialog_setHeight)');
		this.height = h;
		var objFojoHeight = Core.getElementU(this.width_id);//cont_id
		objFojoHeight.style.height = this.height + 'px';
	}

   this.setClassName = function ModalDialog_setClassName(class_name) {
//      document.title = ' c ' + document.title;
//      var is_shown_now = this.isVisible();
//      var effect_old = this.effect;
//      this.effect = 'none';
//      if (is_shown_now) this.hide();

      Core.setClassName(this.width_id, class_name);
      this.clearCustomScrollableContainerCache();
      this.stayTop('d');

//      if (is_shown_now) this.show();
//      this.effect = effect_old;
   }


	this.setEffect = function ModalDialog_setEffect (str_effect) {
		if (str_effect != 'none' && str_effect != 'fade') return Logger.log('Unsupported effect');
      this.effect = str_effect;
	}

	this.isVisible = function ModalDialog_isVisible () {
		return this.visible ? 1 : 0;
	}
	
	this.setVisible = function ModalDialog_setVisible (v) {
		this.visible = v;
	}
	
	this.setPageScrollOn = function ModalDialog_setPageScrollOn () {
   	this.is_scroll_on = 1;
	}

	this.setPageScrollOff = function ModalDialog_setPageScrollOff () {
   	this.is_scroll_on = 0;
	}
   
	this.onScroll = this.stayTop;
      
	this.onShowing = function ModalDialog_onShowingDefault() {};
	this.onShow = function ModalDialog_onShowDefault() {};

   this.onESC = this.hide; //function ModalDialog_onCancelDefault() {};//N/A

	this.onHiding = function ModalDialog_onHidingDefault() {};
   this.onHide = function ModalDialog_onHideDefault() {};

	this.getTopOffset = function ModalDialog_getTopOffset (topYOffset, windowSize) {
      var sc = Core.getPageScrollTop(); 
      var fojo = Core.getElementU(this.fojo_id); 
      var h =  (fojo != null ? fojo.offsetHeight : 0);  
      var vis_h = Core.getBrowserVisibleArea().height;
		if(Core.detectBrowser().isSafari) { vis_h -= this.topNav_offset*0.85; } 
//		else if(Core.detectBrowser().isIE && Core.detectBrowser().version != '7.0') { vis_h -= this.topNav_offset*1.6; } 
      var top_scroll = (vis_h - h)/2 + sc;

//      document.title = 'h='+h + ' vis=' + vis_h + ' topnavoff=' + this.topNav_offset + ' sc='+sc + ' = ' + top_scroll;

		return Math.round(top_scroll);
	}
	
	this.replaceDialog = function ModalDialog_replaceDialog () {
	}

	this.init();
}
