var ModalBox = Class.create(
{
	initialize: function(content)
	{		
		this.modalBox = new Element('div', {className: 'modalBox'});
		
		this.events = new Element('div');
		document.body.appendChild(this.events);
		
		this.buildControls();
		
		this.modalBox.appendChild(content);
	},
	
	buildControls: function()
	{	
		this.modalBox.appendChild(new Element('button', {className: 'closeButton'}).update('X').observe('click', this.close.bind(this)));
	},
	
	draw: function()
	{
		document.body.appendChild(this.modalBox);
		this.greyPage();
		this.position();
	},
	
	greyPage: function()
	{
		this.grey = new Element('div');
		var pageSize = this.getPageSize();
		
		this.grey.setStyle(
		{
			position: 'fixed',
			top: 0,
			left: 0,
			width: pageSize[0] + 'px',
			height: pageSize[1] + 'px',
			backgroundColor: '#666',
			zIndex: 900,
			opacity: 0.4
		});
		
		document.body.appendChild(this.grey);
	},
	
	close: function()
	{
		this.grey.remove();
		this.modalBox.remove();
	},
	
	position: function(stopFade)
	{
		var pageScroll = document.viewport.getScrollOffsets();
		var modalBoxTop = (pageScroll[1] + (document.viewport.getHeight() / 2) - (this.modalBox.getHeight() / 2));
		var modalBoxLeft = (pageScroll[0] + (document.viewport.getWidth() / 2) - (this.modalBox.getWidth() / 2));
		
		this.modalBox.setStyle(
		{
			position: 'absolute',
			zIndex: 1000,
			top: '50px',
			left: modalBoxLeft + 'px'
		});
		
		this.modalBox.hide();
		
		if (stopFade) {
			this.modalBox.show();
		}
		else
		{
			this.modalBox.appear();
		}
	},
	
    getPageSize: function()
    {
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}

		var windowWidth, windowHeight;
		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}

		return [pageWidth,pageHeight];
	}
});

