// JavaScript Document
// modal windows
// ----------------------------------------------

// credits and copyright
// ----------------------------------------------
// theMechanism. Jeffrey Barke. 20080514
// This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License <http://creativecommons.org/licenses/by-sa/3.0/us/>

var tmModalWindows = {

	// properties and internal objects

	id: 'overlay',
	counter: 1,
	current: null,
	allOpen: new Array(),

	// constructor

	init: function() {
		// add modal css to document
		var css = Builder.node('link', { rel:'stylesheet', type:'text/css', media:'screen, projection, tv', href:'/_dev/client/tmModalWindows/modalWindows.css' } );
		$$('head')[0].appendChild(css);
		// create overlay
		this.createOverlay();
	},

	// public methods

	hideModal: function(objDiv) {
		if (!objDiv) { objDiv = $('tmModWin-' + tmModalWindows.current); }
		// remove from tmModalWindow's allOpen array
		tmModalWindows.current = null;
		var intIndex = tmModalWindows.allOpen.indexOf(objDiv.id.substr(9) * 1);
		if (intIndex > -1) {
			tmModalWindows.allOpen.splice(intIndex, 1);
		}
		// hide modal window
		new Effect.Parallel(
			[
			 	new Effect.Fade(objDiv, { sync: true } ),
				new Effect.Fade(objDiv, { sync: true } )
			],
			{ duration: 0.5, afterFinish: function() { 
				// if all modal windows are hidden, hide overlay
				if (tmModalWindows.allOpen.length == 0) {
					tmModalWindows.hideOverlay();
				}
			} }
		);
	},

	hideOverlay: function() {
		new Effect.Fade($(this.id), { duration: 0.2, fps: 80, from: 0.8, to: 0.0 } );
	},

	showModal: function(strUrl, objParameters) {
		if (tmModalWindows.current == null) {
			// create modal window and set properties
			var objDiv = Builder.node('div', { id:'tmModWin-' + tmModalWindows.counter, className:'modalWindow', style:'display:none;' } );
			var div = Builder.node('div', { className:'modalWindow-inner' } );
			if (strUrl.substr(0, 7) == 'http://' || strUrl.substr(0, 1) == '/') {
				div.innerHTML = '<div class="modalLoading">Please wait. Loading&#8230;</div>';
				if (!objParameters) { objParameters = { } }
				new Ajax.Request(strUrl, {
					method: 'get',
					parameters: objParameters,
					onSuccess: function(data) {
						div.innerHTML = data.responseText;
					}
				});
			} else {
				div.innerHTML = strUrl;
			}
			objDiv.appendChild(div);
			var objDim = Element.getPageSize();
			objDiv.style.top = (objDim.scroll.top + 100) + 'px';
			document.body.appendChild(objDiv);
			// update tmModalWindows properties
			tmModalWindows.current = tmModalWindows.counter;
			tmModalWindows.allOpen.push(tmModalWindows.counter);
			tmModalWindows.counter++;
			new Effect.Parallel(
				[
					new Effect.Appear(objDiv, { sync: true } ),
					new Effect.Appear(objDiv, { sync: true } )
				],
				{ duration: 0.5 }
			);
		}
	},

	showOverlay: function(strUrl, objParam) {
		new Effect.Appear($(this.id), { duration: 0.2, fps: 80, from: 0.00, to: 0.6, 
			afterFinish: function () {
				// always show modal window after showing overlay
				tmModalWindows.showModal(strUrl, objParam);
			}
		} );
	},

	// private methods

	createOverlay: function() {
		// create overlay and set properties
		var objOverlay = Builder.node('div', { id:tmModalWindows.id, style:'display:none;' } );
		var objDim =  Element.getPageSize();
		if (objDim.page.height >= objDim.window.height) {
			var intHeight = objDim.page.height;
		} else {
			var intHeight = objDim.window.height;
		}
		objOverlay.style.height = intHeight + 'px';
		objOverlay.style.width = objDim.page.width + 'px';
		// render
		document.body.appendChild(objOverlay);
		// attach click event
		objOverlay.onclick = function() {
			// never hide overlay until all modal windows are hidden
			for (var i = 0; i < tmModalWindows.allOpen.length; i++) {
				tmModalWindows.hideModal($('tmModWin-' + tmModalWindows.allOpen[i]));
			}
			return false;
		}
	}

}

// utilities
// ----------------------------------------------

// extend Prototype element to properly determine page size
Element.getPageSize = function() {
	var xScroll, yScroll, scrollOffsetY;
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight) {
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	if (self.pageYOffset) {
		scrollOffsetY = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		scrollOffsetY = document.documentElement.scrollTop;
	} else if(document.body) {
		scrollOffsetY = document.body.scrollTop;
	}
	var windowWidth, windowHeight;
	if(self.innerHeight) {
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if(document.body) {
		windowWidth=document.body.clientWidth;windowHeight=document.body.clientHeight;}
	if (windowHeight && yScroll) {
		pageHeight = Math.max(windowHeight, yScroll);
	}
	if (windowWidth, xScroll) {
		pageWidth = Math.max(windowWidth, xScroll);
	}
	return {
		page: {
			width: pageWidth,
			height: pageHeight
		},
		window: {
			width: windowWidth,
			height: windowHeight
		},
		scroll: {
			top:scrollOffsetY
		}
	};
}

// event listeners
// ----------------------------------------------

// create overlay on window load
Event.observe(window, 'load', tmModalWindows.init.bindAsEventListener(tmModalWindows), false);