// -----------------------------------------------------------------------------------
//
//  Modal Generator
//  requires Prototype and Scriptaculous
//
//	Modal v0.unversioned
//
// -----------------------------------------------------------------------------------
/*

	var myModal = new modal();
	
	myModal.afterShow( function () { alert('Showing modal'); } );
	myModal.afterHide( function () { alert('Hiding modal'); } );
	
*/
// -----------------------------------------------------------------------------------

// Check for prototype and scriptaculous

if((typeof Effect =='undefined') || 
   (typeof Prototype =='undefined') || 
   (typeof Element == 'undefined') || 
   (typeof Element.Methods =='undefined') ||
   parseFloat(Prototype.Version.split(".")[0] + "." +
              Prototype.Version.split(".")[1]) < 1.6) {
	throw("modal generator requires the Prototype JavaScript framework >= 1.6.0 and script.aculo.us");
}

// constructor

function modal (settings) {
	// initialise variables
	this.settings = $H( {} );
	this.settings.set('defaultOverlayId','overlay');
	this.settings.set('hideOnClick', true);
	this.settings = this.settings.merge(settings);
}

modal.prototype.show = function () {
	// show the modal	
	var objOverlay = this._getOverlay();

	// get the page size and resize overlay accordingly
	var arrayPageSize = getPageSize();
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';
	// check aftershow function
	if(typeof(this.settings.get('aftershowFunction')) == 'function') {
		// execute function
		this.settings.get('aftershowFunction')();		
	}
}

modal.prototype.hide = function () {
	// hide the modal
	var objOverlay = this._getOverlay();
	objOverlay.style.display = 'none';
	
	// check afterhide function
	if(typeof(this.settings.get('afterhideFunction')) == 'function') {
		// execute function
		this.settings.get('afterhideFunction')();
	}
}

modal.prototype.afterShow = function ( aftershowFunction ) {
	// set aftershow function
	this.settings.set('aftershowFunction', aftershowFunction);
}

modal.prototype.afterHide = function ( afterhideFunction ) {
	// set afterhide function
	this.settings.set('afterhideFunction', afterhideFunction);
}

modal.prototype._getOverlay = function () {

	if($(this.settings.get('defaultOverlayId')) == null)
	{
		// make div

		var objBody = document.getElementsByTagName("body").item(0);
		
		var objOverlay = document.createElement("div");
		objOverlay.setAttribute('id', this.settings.get('defaultOverlayId'));
		
		objOverlay.style.display = 'none';
		objOverlay.style.position = 'absolute';
		objOverlay.style.top = '0';
		objOverlay.style.left = '0';
		objOverlay.style.background = '#000000'; 
		objOverlay.style.width = '100%';

		objBody.insertBefore(objOverlay, objBody.firstChild);
	}
	
	var thisSelf = this;
		
	if(this.settings.get('hideOnClick') == true)
	{
		$(this.settings.get('defaultOverlayId')).onclick = function () { thisSelf.hide(); }
	}
	
	return $(this.settings.get('defaultOverlayId'));
}





/*  pagesize functions  */

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		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
		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 than height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less than width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}






