
/*	
	window disabler is designed to disable all function of a browser window
	
	*********************************************************************
	*                                                                   *
	*    NO WARRANTY                                                    *
	*    This code is provided "as is" without warranty of any kind,    *
	*    either expressed or implied, including, but not limited to,    *
	*    the implied warranties of merchantability and fitness for a    *
	*    particular purpose.                                            *
	*	                                                                  *
	*    You expressly acknowledge and agree that use of this code      *
	*    is at your own risk.                                           *
	*                                                                   *
	*********************************************************************
	*/
	
	// version 1.2.3
	// 20091826
	//		Removed a work around hack to call the resize method of the class
	//			by using the "me" keyword. All references to "this" have now been converted to "me"
	//			thanks to Michael Kuehl and his tutorial at:
	//			http://www.switchonthecode.com/tutorials/javascript-tutorial-why-the-this-keyword-breaks
	
	// version 1.2.2
	// 20090629
	//		Removed minor bug that caused a javascript error whenn resizing window
	
	// version 1.2.1
	// 20090419
	// Cleaned up code and made calling of the resize function more efficient by using addEventListener and
	//		removeEventListener so that other classes can also add and remove resize event listeners
	//		which will be required when I add window resize detection to the modal window class
	// Added additional checking for document height/width and cleaned up window size code
	
	// version 1.2.0
	// 20090418
	// Added resizing to the disabler div so that when the window is resized the required
	//		size of the disabler division is revaluated and the new size applied
	// Minor alteration to getWindowSize to clean it up a bit
	
	// version 1.1.1 
	// 20090208
	// Minor changes to clean up code and documentation and change file name to be consistant with Blunt Web Apps file naming schema
	
	// version 1.1.0
	// 20080910
	// Altered window size script to better detect actual size of division needed to disable the entire client window.
	// 		This may not work even with the changes do to non standardized way that different browsers report page size
	//		Disabling division may not cover entire window if it is resized after the div is put in place
	//		Possible fix would be to call a resizer when the browser window is resized
	// Other improvements needed:
	//		The disabling div may not cover flash and some other types of elements
	//		Would like to find a way to insure that disabling div covers all elements of a page
	
	
	/*
	
		USAGE:
		
			initialize (instantiate) disabler class
			   disabler = new bluntWindowDisabler();
				 Note: Instantaition need to be done after the document has been loaded and not before.
				 				Instantaition before the document is loaded can result in an incorrect value being returned
								for the document height and width values
				 				This can be accomplishes by calling an onload in the body.
								
								A better way is to attach a listener to the body so that mulitple onload events can be loaded
								
								This is a cross browser example of attaching an event
								
								/////////////////////////////////////////////////////
								
								// inialize the disabler variable in the global scope
								var disabler;
								
								// create a funtion that will instantiate the class
								function instantaiteDisabler() {
									disabler = new bluntDisabler();
								}
								
								// attach a listener to the load event of the body
								if (window.addEventListener) {
									// most browsers
									window.addEventListener('load', instantaiteDisabler, false);
								} else if (window.attachEvent) {
									// windows/IE
									window.attachEvent('onload', instantaiteDisabler);
								}
								
								///////////////////////////////////////////////////////
				 
			set opacity of disabler (set as a percentage from 0 to 100 percent opaque) - default is 50
			   disabler.opacity = 50;
			set z-index of disabler - default is 1000
			   disabler.zIndex = 1000;
			set color of disabler - default is #000000 (black)
			   disabler.color = '#000000';  // use only valid color values
			disable window
			   disabler.disable();
			enable window
			   disabler.enable();
	
	*/

	function bluntWindowDisabler() {
		// set defaults
		var me = this;
		me.id = 'bluntWindowDisabler';
		me.opacity = 50;
		me.zIndex = 1000;
		me.color = '#000000';
		
		me.disable = function() {
			if (!document.getElementById(me.id)) {
				var b = document.getElementsByTagName('body')[0];
				var newdiv = document.createElement('div');
				var divIdName = me.id;
				newdiv.setAttribute('id',divIdName);
				b.appendChild(newdiv);
				div = document.getElementById(divIdName)
				windowSize = me.getWindowSize();
				with (div.style) {
					display = 'block';
					width = windowSize[0]+'px';
					minWidth = '100%';
					height = windowSize[1]+'px';
					position = 'absolute';
					top = '0';
					left = '0';
					backgroundColor = me.color;
					zIndex = me.zIndex;
				}
				if (typeof(div.style.opacity) == "string") {
					div.style.opacity = me.opacity/100;
				} else {
					div.style.filter = "alpha(opacity='"+me.opacity+"')";
				}
				// set resizing function
				if (window.addEventListener) {
					window.addEventListener('resize', me.resize, false);
				} else if (window.attachEvent) {
					window.attachEvent('onresize', me.resize);
				}
			} // else browser is already disabled or this div already exists
		} // end function disable
		
		me.enable = function () {
			if (document.getElementById(me.id)) {
				// disable the resizeing function
				if (window.removeEventListener) {
					window.removeEventListener('resize', me.resize, false);
				} else if (window.detachEvent) {
					window.detachEvent('onresize', me.resize);
				}
				var b = document.getElementsByTagName('body')[0];
				var olddiv = document.getElementById(me.id);
				b.removeChild(olddiv);
			} // else div does not exist
		} // end function this.enable
		
		me.getWindowSize = function() {
			/*
				When using this script it is best to have a div that wraps the contents of the page.
				See next comment for an explanation
			*/
			/*	This function, at first appears quite convoluted, but getting the size of the document area in all browsers is, to say the least, difficult. Many browsers will only return the height an width of the browser window. In order to successfully disable the entire document we must have the full height and width of the document and not just the browser window. This function starts out similar to other functions that get a windows size, except that at each pass we test to see if we've gotten something larger. Then to make things even more complex we get a list of the div's in a page and check the size of each looking for something larger than we already have. Even with all of this I am not satisfied that this function will always return a size that covers the entire document. I would like to alter this to read trough all elements, but I fear that would take too long. */
			var WindowWidth = 0, WindowHeight = 0;
			var D = document;
			var W = window;
			if (typeof(W.innerHeight) != 'undefined') {
				//WindowWidth = Math.max(WindowWidth, W.innerWidth); // not needed??? see below
				//		causes incorrect width to be reported in ie resulting in a width that causes a bottom scroll bar to appear
				//		it seems that IE reports the innerWidth including the verticle scroll bar
				//		which is odd since IE is one of the browsers that always have a vertical scroll even on short pages
				//		<saarcasm>leave it to M$ to surprise me by being inconsistent</sarcasm>
				WindowHeight = Math.max(WindowHeight, W.innerHeight);
			}
			if (typeof(D.body) != 'undefined' && D.body !== null) {
				if (typeof(D.body.clientWidth) != 'undefined' &&D.body.clientWidth !== null) {
					WindowWidth = Math.max(WindowWidth, D.body.clientWidth);
					WindowHeight = Math.max(WindowHeight, D.body.clientHeight);
				}
				if (typeof(D.body.scrollWidth) !== 'undefined' && D.body.scrollWidth !== null) {
					WindowWidth = Math.max(WindowWidth, D.body.scrollWidth);
					WindowHeight = Math.max(WindowHeight, D.body.scrollHeight);
				}
				if (typeof(D.body.offsetWidth) != 'undefined' && D.body.offsetWidth !== null) {
					WindowWidth = Math.max(WindowWidth, D.body.offsetWidth);
					WindowHeight = Math.max(WindowHeight, D.body.offsetHeight);
				}
				
			}
			if (typeof(D.documentElement) != 'undefined' && D.documentElement !== null) {
				if (typeof(D.documentElement.clientWidth) != 'undefined' && D.documentElement.clientWidth !== null) {
					WindowWidth = Math.max(WindowWidth, D.documentElement.clientWidth);
					WindowHeight = Math.max(WindowHeight, D.documentElement.clientHeight);
				}
				if (typeof(D.documentElement.scrollWidth) != 'undefined' && D.documentElement.scrollWidth !== null) {
					WindowWidth = Math.max(WindowWidth, D.documentElement.scrollWidth);
					WindowHeight = Math.max(WindowHeight, D.documentElement.scrollHeight);
				}
				if (typeof(D.documentElement.offsetWidth) != 'undefined' && D.documentElement.offsetWidth !== null) {
					WindowWidth = Math.max(WindowWidth, D.documentElement.offsetWidth);
					WindowHeight = Math.max(WindowHeight, D.documentElement.offsetHeight);
				}
			}
			if (typeof(D.getElementsByTagName('body')[0]) != 'undefined' && D.getElementsByTagName('body')[0] !== null) {
				if (typeof(D.getElementsByTagName('body')[0].clientWidth) != 'undefined' && 
																									D.getElementsByTagName('body')[0].clientWidth !== null) {
					WindowWidth = Math.max(WindowWidth, D.getElementsByTagName('body')[0].clientWidth);
					WindowHeight = Math.max(WindowHeight, D.getElementsByTagName('body')[0].clientHeight);
				}
				/*
				This routine has been removed temporarily
				It seems that the above was not returning the proper window size becasue instantaition was being called
				before the window/document was loaded
				instaniation cannot be done until the page has completely loaded
				
				n = D.getElementsByTagName('body')[0];
				divs = n.getElementsByTagName('div');
				for (i=0; i<divs.length; i++) {
					cWidth = divs[i].clientWidth;
					cHeight = divs[i].clientHeight
					if (cWidth > WindowWidth) {
						WindowWidth = cWidth;
					}
					if (cHeight > WindowHeight) {
						WindowHeight = cHeight;
					}
				}
				*/
			}
			//alert(WindowWidth+':'+WindowHeight);
			return [WindowWidth, WindowHeight];
		}  // end function me.getWindowSize
		
		me.resize = function() {
			// what surprizes me is that this works better (more seamlessly) in IE than in FF
			// first set width and height to 0 so it does not effect getting the new size
			var div = document.getElementById(me.id);
			with (div.style) {
				width = 0;
				height = 0;
			}
			windowSize = me.getWindowSize();
			with (div.style) {
				width = windowSize[0]+'px';
				height = windowSize[1]+'px';
			}
		} // end function me.resize
		
	} // end function bluntWindowDisabler
	
	// end file
