// JavaScript Document

		var oPopLayer = null;
		var oPopLayerWindow = null;
		var oWhiteWashLayer = null;
		var iOpacity = 0;
		var iWhitewashMaxOpacity = 75;
		var iPreviousScrollPos = 0;
		var sLoadingHtml = '<div align="center"><img src="/img/throbber.gif" /><br />Loading</div>';
		var DEBUG = false;
		
		function popLayer_Show(sContentToDisplay){
			iOpacity = 0;
			if(!oWhiteWashLayer){return};
			oWhiteWashLayer.resize();
			oWhiteWashLayer.show();
			oPopLayer.Top = getScrollHeight();
			popLayer_HandleScroll();
			popLayer_FadeIn(iWhitewashMaxOpacity);
			var oContentElement = document.getElementById(sContentToDisplay);
			if(oContentElement){
				document.getElementById('popLayerContentWrap').innerHTML = oContentElement.innerHTML;
			}
			oPopLayer.style.display = 'block';
			oPopLayer.IsVisible = true;
		}
		function popLayer_Hide(){
			oWhiteWashLayer.hide();
			document.getElementById('popLayerActualContent').innerHTML = sLoadingHtml;
			oPopLayer.style.display = 'none';
			iOpacity = 0;
			oPopLayer.IsVisible = false;
		}
		function popLayer_HandleResize(){
			oWhiteWashLayer.resize();
			popLayer_HandleScroll();
		}
		function popLayer_HandleMouseWheel(event){
			if (!event){
				event = window.event;
			}

			if(oPopLayer.IsVisible){
				if (event.preventDefault){
						event.preventDefault();		
				}
				event.returnValue = false;	
				return false;
			}else{
				event.returnValue = true;
				return true;
			}
		}
		function popLayer_Init(){
/*			var sHtml = '';
			sHtml += '<div id="whiteWash" style="display:none;"></div>';
			sHtml += '<div id="popLayer" style="display:none;">';
				sHtml += '<div id="popLayerWindow">';
					sHtml += '<div id="popLayerTopCap"><div></div></div>';
					sHtml += '<div id="popLayerCloseButton" onClick="javascript:popLayer_Hide();"></div>';
					sHtml += '<div id="popLayerPadding">';
						sHtml += '<div id="popLayerContentWrap"></div>';
						if(DEBUG){sHtml += '<div id="debug" style="position:absolute; border:1px solid #f00; color:#f00; background-color:#fff; font-weight:bold; top:200px; left:0px;"></div>';}
					sHtml += '</div>';
					sHtml += '<div id="popLayerBottomCap"><div></div></div>';					
				sHtml += '</div>';
			sHtml += '</div>';
			document.write(sHtml);*/
			
			oPopLayer = document.getElementById('popLayer');
			oPopLayer.Top = 0;
			oPopLayerWindow = document.getElementById('popLayerWindow');
			oWhiteWashLayer = document.getElementById('whiteWash');
			oWhiteWashLayer.show = 		function(){ this.style.display = 'block'; }
			oWhiteWashLayer.hide = 		function(){ this.style.display = 'none'; }
			oWhiteWashLayer.resize = 	function(){
											this.style.width = document.body.scrollWidth+'px';
											this.style.height = document.body.scrollHeight+'px';
										}
			if (window.addEventListener){
				window.addEventListener('DOMMouseScroll', popLayer_HandleMouseWheel, false);
			}
			window.onmousewheel = document.onmousewheel = popLayer_HandleMouseWheel;	
			document.getElementById('popLayerActualContent').innerHTML = sLoadingHtml;
			var oImg = new Image();
			oImg.src = '/img/throbber.gif';
		}
		function debug(sDebug){
			if(DEBUG){document.getElementById('debug').innerHTML = sDebug;}
		}
		function popLayer_FadeIn(iMaxOpacity){
			iOpacity = iMaxOpacity;  // PREVENT FADING
			
			if(iOpacity > iMaxOpacity){iOpacity = iMaxOpacity;}
			if(iOpacity <= iMaxOpacity){
				oWhiteWashLayer.style.opacity = popLayer_GetSafeOpacity();
				oWhiteWashLayer.style.filter = 'alpha(opacity='+iOpacity+')';
				iOpacity += 10;
				if(iOpacity < iMaxOpacity){setTimeout('popLayer_FadeIn('+iMaxOpacity+')', 10);}
			}
		}
		function popLayer_GetSafeOpacity(){
			var iReturn = parseInt(iOpacity);
			if(iReturn >= 100){iReturn = 99;}
			return iReturn / 100
		}
		function getScrollHeight(){
			return 	(document.body.scrollTop?document.body.scrollTop:document.documentElement.scrollTop);
		}
		function popLayer_HandleScroll(){
			if(!oPopLayer || !oPopLayerWindow){return;}
			
			var iWindowHeight = (window.innerHeight?window.innerHeight:document.documentElement.clientHeight);
			var iPopLayerHeight = oPopLayerWindow.scrollHeight;
			var iScrollAmount = getScrollHeight();

			var bIsScrollingDown = (iPreviousScrollPos < iScrollAmount);
			iPreviousScrollPos = iScrollAmount;
			var bTopIsOffScreen = ((oPopLayer.Top+150 < iPreviousScrollPos));
			var bBottomIsOffScreen = ((oPopLayer.Top+iPopLayerHeight) > (iScrollAmount+iWindowHeight));

			if(!bTopIsOffScreen && !bBottomIsOffScreen){
				//we can see the full layer, so move it with the amount of scroll
				oPopLayer.Top = iScrollAmount;
				oPopLayer.style.top = iScrollAmount+'px';
				debug('see all');
				return;
			}

			var iTop = 0;
			
			if(bTopIsOffScreen && bBottomIsOffScreen){
				//the layer is taller than the window and we can only see the middle, so dont move the layer
				debug('see middle');
				return;	
			}
			if(bTopIsOffScreen){
				//we can see the bottom of the layer
				if(bIsScrollingDown){
					iTop = iScrollAmount-(iPopLayerHeight-iWindowHeight);
					debug('top off screen, scrolling down');
				}else{
					debug('top off screen, scrolling up');
					return;
				}
			}
			if(bBottomIsOffScreen){
				//we can see the top of the layer
				if(bIsScrollingDown){
					debug('bottom off screen, scrolling down');
					return;
				}else{
					iTop = iScrollAmount;
					debug('bottom off screen, scrolling up');
				}
			}
			
			oPopLayer.Top = parseInt(iTop);
			oPopLayer.style.top = iTop+'px';
		}
		
		window.onscroll = popLayer_HandleScroll;
		window.onresize = popLayer_HandleResize;
		window.onload = function(){iPreviousScrollPos = document.documentElement.scrollTop;}
		popLayer_Init();
