﻿Modalbox._insertContent = function( content )
{
	// This original ModalBox.js _insertContent function clones the node that it is going to display.
	// This means any references to the original elements will not be redirected to the elements visible
	// in the modalbox.
	// This version will remove the original element and then append it to the modalbox, which
	// will ensure that all references to the original elements will now point correctly to the
	// elements under the modalbox. This has to be done to allow the MutuallyExclusiveCheckBoxExtender
	// to keep working when the element is moved into the modalbox.

	$(this.MBcontent).hide().update("");
	if(typeof content == 'string') {
		setTimeout(function() { // Hack to disable content flickering in Firefox
			this.MBcontent.update(content);
		}.bind(this), 1);
	} else if (typeof content == 'object') { // HTML Object is given
		// If the content element is already part of the DOM remove it before assigning it to the modalbox.
		var _htmlObj = content.parentNode != null && typeof content.parentNode != "undefined" ? content.parentNode.removeChild( content ) : content;
		this.MBcontent.appendChild(_htmlObj); // Insert the removed node as a child of the modelbox.
		this.MBcontent.down().show(); // Toggle visibility for hidden nodes
		if(Prototype.Browser.IE) // Toggling back visibility for hidden selects in IE
			$$("#MB_content select").invoke('setStyle', {'visibility': ''});
	}
}

Modalbox._setPosition = function( )
{
	$(this.MBwindow).setStyle({left: Math.round((document.viewport.getWidth( ) - Element.getWidth(this.MBwindow)) / 2 ) + "px",
								top: Math.round((document.viewport.getHeight( ) - Element.getHeight(this.MBwindow)) / 2 ) + "px"});
}

Modalbox.resize = function( byWidth, byHeight, options )
{
	var wHeight = $(this.MBwindow).getHeight();
	var wWidth = $(this.MBwindow).getWidth();
	var hHeight = $(this.MBheader).getHeight();
	var cHeight = $(this.MBcontent).getHeight();
	var newHeight = ((wHeight - hHeight + byHeight) < cHeight) ? (cHeight + hHeight - wHeight) : byHeight;
	if(options) this.setOptions(options); // Passing callbacks
	if(this.options.transitions) {
		new Effect.ScaleBy(this.MBwindow, byWidth, newHeight, {
				duration: this.options.resizeDuration, 
				scaleFromTop: false,
			  	afterFinish: function() { 
					this.event("_afterResize"); // Passing internal callback
					this.event("afterResize"); // Passing callback
				}.bind(this)
			});
	} else {
		this.MBwindow.setStyle({width: wWidth + byWidth + "px", height: ((wHeight + newHeight)>0?(wHeight + newHeight):0) + "px"});
		setTimeout(function() {
			this.event("_afterResize"); // Passing internal callback
			this.event("afterResize"); // Passing callback
		}.bind(this), 1);
	}
}

Modalbox._appear = function( )
{ 
	// First appearing of MB
	if(Prototype.Browser.IE && !navigator.appVersion.match(/\b7.0\b/)) { // Preparing IE 6 for showing modalbox
		window.scrollTo(0,0);
		this._prepareIE("100%", "hidden"); 
	}
	this._setWidth();
	this._setPosition();
	if(this.options.transitions) {
		$(this.MBoverlay).setStyle({opacity: 0});
		new Effect.Fade(this.MBoverlay, {
				from: 0, 
				to: this.options.overlayOpacity, 
				duration: this.options.overlayDuration, 
				afterFinish: function() {
					$(this.MBwindow).show(); 
					this._setPosition(); 
					this.loadContent();
				}.bind(this)
		});
	} else {
		$(this.MBoverlay).setStyle({opacity: this.options.overlayOpacity});
		$(this.MBwindow).show();
		this._setPosition(); 
		this.loadContent();
	}
	this._setPosition = this._setPosition.bindAsEventListener(this);
	Event.observe(window, "resize", this._setPosition);
}

Modalbox.hide = function(options)
{
	// External hide method to use from external HTML and JS
	if(this.initialized) {
		// Reading for options/callbacks except if event given as a pararmeter
		if(options && typeof options.element != 'function') Object.extend(this.options, options); 
		// Passing beforeHide callback
		this.event("beforeHide");
		$(this.MBwindow).hide();
		this._deinit();
	} else throw("Modalbox is not initialized.");
}
