var altsolbox = altsolbox ? altsolbox : {};

altsolbox._ERROR_MESSAGE = "Oops.. there was a problem with your request.<br /><br />" +
"Please try again.<br /><br />" +
"<em>Click anywhere to close.</em>"; // the error message displayed when the request has a problem

altsolbox.init = function(opt) {
		return new altsolbox.Box(opt);
};

altsolbox._openSetup  = function() {
	this.overlay.setStyles({top: Window.getScrollTop()+'px', height: Window.getHeight()+'px'});
	altsolbox._setup.bind(this)(true);
	this.top = Window.getScrollTop() + (Window.getHeight() / 15);
	this.center.setStyles({top: this.top+'px', display: ''});
	this.fx.overlay.start(this.options.overlayOpacity);
	//this.center.className = 'mb_loading';
}


altsolbox._openFinish  = function() {
	this.center.className = '';
	this.center.setStyle('cursor', 'default');
	this.center.onclick = '';
	this.center.style.display = 'block';
	
	var wflag = false;
	var w = this.contents.offsetWidth;
	if (w < this.options.minWidth) {
		w = this.options.minWidth;
		wflag = true;
	} else if (w > this.options.maxWidth){
		w = this.options.maxWidth;
		wflag = true;
	}

	var hflag = false;
	var h = this.contents.offsetHeight;
	if (h < this.options.minHeight) {
		h = this.options.minHeight;
		hflag = true;
	} else if (h > this.options.maxHeight){
		h = this.options.maxHeight;
		hflag = true;
	}
	
	var fsizex = w/-2;
	
	
	this.center.setStyles({ marginLeft: fsizex+'px'});

	this.center.setStyles({ width: w+'px', height: h+'px' });
	
	this.options.onComplete(this);

}

altsolbox._setup = function(open) {
//		var elements = $A($$('object'));
//		elements.extend($$(window.ActiveXObject ? 'select' : 'embed'));
//		elements.each(function(el){ el.style.visibility = open ? 'hidden' : ''; });
		var fn = open ? 'addEvent' : 'removeEvent';
		window[fn]('scroll', this.eventPosition)[fn]('resize', this.eventPosition);
		document[fn]('keydown', this.eventKeyDown);
};
	



altsolbox._ajaxFailure = function (){
		this.contents.set('html','');
		this.error.clone().injectInside(this.contents);
		this.center.setStyle('cursor', 'pointer');
};



altsolbox._displayContent = function(responseText)
{

		this.center.setStyles({ width:  'auto', height:  'auto' });
		this.contents.empty();
		this.contents.set('html',responseText);
		altsolbox._openFinish.bind(this)();
		
};
	
	
altsolbox._keyboardListener = function(event) {
		// close the MOOdalBox when the user presses CTRL + W, CTRL + X, ESC
		if ((event.control && event.key == 'w') || (event.control && event.key == 'x') || (event.key == 'esc')) {
			altsolbox._close.bind(this)();
			event.stop();
		}		
};

	
altsolbox._close = function() {
		this.contents.empty();
		for(var f in this.fx) this.fx[f].cancel();
        this.center.style.display = 'none';
		this.fx.overlay.chain(altsolbox._setup.pass(false, this)).start(0);
		return false;
};
		




altsolbox.Box = function(opt)  {
	this.options = opt;

	if (!$chk(this.options)){
		this.options = {};
	}
	if (!$chk(this.options.evalScripts)){
		this.options.evalScripts = true;
	}
	if (!$chk(this.options.evalResponse)){
		this.options.evalResponse = true;
	}
	
	if (!$chk(this.options.onComplete)){
		this.options.onComplete = function(box){};
	}
	
	if (!$chk(this.options.minWidth)){
		this.options.minWidth = 200;
	}
	if (!$chk(this.options.minHeight)){
		this.options.minHeight = 150; 
	}
	if (!$chk(this.options.maxWidth)){
		this.options.maxWidth = 800; 
	}
	if (!$chk(this.options.maxHeight)){
		this.options.maxHeight = 550; 
	}

	if (!$chk(this.options.overlayOpacity)){
		this.options.overlayOpacity = 0.7; 
	}
	
	this.eventKeyDown = altsolbox._keyboardListener.bindWithEvent(this);
	
	// init the HTML elements
	// the overlay (clickable to close)
	this.overlay = new Element('div').setProperty('id', 'mb_overlay').injectInside(document.body);

	// the center element
	this.center = new Element('div').setProperty('id', 'mb_center').injectInside(document.body);
	this.topdiv = new Element('div').setProperty('id', 'mb_topdiv').injectInside(this.center);
	this.closelink = new Element('a').setProperties({id: 'mb_close_link', href: '#'}).injectInside(this.topdiv);
	this.closelink.onclick = this.overlay.onclick = altsolbox._close.bind(this);
	
	// the actual page contents
	this.contents = new Element('div').setProperty('id', 'mb_contents').injectInside(this.center);
	this.error = new Element('div').setProperty('id', 'mb_error').set('html',altsolbox._ERROR_MESSAGE);

	// init the effects
	this.fx = {
		overlay:	new Fx.Tween(this.overlay, {property: "opacity", duration: 400}).set(0)
	};

};




altsolbox.Box.prototype.openStatic  = function(content) {

	altsolbox._openSetup.bind(this)();
	this.center.setStyles({ width:  'auto', height:  'auto' });
	this.contents.empty();
	content.inject(this.contents);

	altsolbox._openFinish.bind(this)();
	

};


altsolbox.Box.prototype.open  = function(sLinkHref) {
	this.openRrequest(sLinkHref).send();
	return true; 
};


altsolbox.Box.prototype.close  = function() {
	altsolbox._close.bind(this)();
};


altsolbox.Box.prototype.openRrequest  = function(href) {
		
		altsolbox._openSetup.bind(this)();
		this.contents.empty();

		var displayContent = altsolbox._displayContent.bind(this);
		var ajaxFailure = altsolbox._ajaxFailure.bind(this);
		var ajaxOptions = {
		method: 		'get',
		url:			href,
		evalScripts: 	this.options.evalScripts,
		evalResponse: 	this.options.evalResponse,
		onComplete: 	displayContent, 
		onFailure: 		ajaxFailure
		};
		
		var req = new Request(ajaxOptions);
		return req;
};
	


