var oPoppers = { // send in the popups

		aPoppers : 	[],
		wPoppers : {},
		aSemaphor : {},
		oIETextAreaTricky : {},
		
		mInit : function (parent, update) {
			return function() {
				oPoppers.aPoppers=oIThelp.getAllElementsByClassName(oIThelp.getElementById(parent), 'popper');
				oLinks.oRels [ "popper" ] = function ( oLink, sId ) {
					var f=oLink.onclick;
					oLink.init=function (e) {
						var oPopper, i=0;
						var i=0;
						while ((oPopper=oPoppers.aPoppers[i++])!=null) {
							if (oPopper.id==oLink.rel.split('#')[1]) {
								aDivs=oPopper.getElementsByTagName('DIV');
								var oDiv, j=0;
								while ((oDiv=aDivs[j++])!=null)
									if (oDiv.className=='body') oDiv.parentNode.replaceChild(oIThelp.createEmbeddedTextNode('div', oIThelp.mGetLocalName("WAITING_FOR_RESPONSE"), 'body'), oDiv);
							}
						}
						f();
					}
					oLink.onclick = function ( e ) {
						oLink.init(e);
						if ( e && e.preventDefault ) e.preventDefault ();
						var popper = document.getElementById ( sId );
						popper.opener = this;
						popper.mPop ( true, e );
						this.className = this.className.attach ( "selected" );
						var restore = function () { oLink.className = oLink.className.detach ( "selected" ); }
						oGod.mSubscribe ( sId + " closed", restore );
						oGod.mSubscribe ( sId + " reopened", restore );
						return false;
					}
				}
			
				i = 0; var oPopper;
				while (( oPopper = oPoppers.aPoppers [ i++ ]) != null ) {
					oPoppers.mSetup ( oPopper, update );
				}
			}
		},
		
		mGetZIndex : function () {
			var max=0;
			for (word in oPoppers.wPoppers) {
				if (oPoppers.wPoppers[word]>max) max=oPoppers.wPoppers[word];
			}
			return max+4;
		},
		
		mSetup : function ( oPopper, update ) { // prepare popper
			oPopper.initialWidth=parseInt(oPopper.style.width);
			oPopper.iCSY			= 0;
			oPopper.iCSX			= 0;
			oPopper.mPop 			= oPoppers.mPop;
			oPopper.mCenter			= oPoppers.mCenter;
			oPopper.mEffect 		= oPoppers.mEffect;
			oPopper.mConstrain	 	= oPoppers.mConstrain;
			oPopper.mSetTitle		= oPoppers.mSetTitle;
			
			oPopper.className	 	= oPopper.className.replace ( "popper", "popped" );
			oPopper.bPopped			= false;
			oPopper.oBody			= oPopper.getElementsByTagName ( "div" ).item ( 0 );
			
			try {
				// setup titlebar
				var oTitlebar = oPopper.getElementsByTagName ( "h4" ).item ( 0 );
				oPopper.oTitle = oTitlebar.firstChild;
				if(oIThelp.bShim)
					oTitlebar.style.width = oPopper.style.width;
				
				var oA = document.createElement ( "a" );
				oA.href = oGod.DUMMY_LINK;
				oA.appendChild ( document.createTextNode ( oIThelp.mGetLocalName("CLOSE_WINDOW")));
				oTitlebar.insertBefore ( oA, oTitlebar.firstChild );
				oTitlebar.onmousedown = oPoppers.mDrag;
				oA.onmouseup = function () {
					oPopper.mPop ( false, window.Event );
				};
				
				// setup pstatusbar
				var oStatusBar = document.createElement('DIV');
				
				oPopper.statusBar=oIThelp.mGetStatusBar('pstatusbar');
				oStatusBar.appendChild(oPopper.statusBar);
				oStatusBar.onmousedown = oPoppers.mResize;
				oPopper.appendChild ( oStatusBar );
				
			}
			catch ( exception ) {};
			
			if (oIThelp.bShim) {
				var oFrame = document.createElement ( "iframe" );
				oFrame.src="blank.gif";
				oFrame.id=oPopper.id+'_shim';
				oFrame.scrolling='no';
				oFrame.frameBorder='0';
				oFrame.style.display='none';
				oFrame.style.position='absolute';
				oFrame.style.top='0px';
				oFrame.style.left='0px';
				oPopper.oShim=oFrame;
				oPopper.mShim=oPoppers.mShim;
				oLayout["worksheet"].appendChild ( oFrame );
			}
			//oLayout["worksheet"].appendChild ( oPopper );

			oGod.mSubscribe ( oPopper.id + " popper updated", function () {oPopper.mConstrain ();});
			oGod.mSubscribe ( oPopper.id + " popper filled", new function() {
					return function () {
					
					//alert(Popper.oBody);
					//oPopper.oBody.style.display='block';
					//oPopper.oBodyTemp.style.display='none';
						//oPopper.oBody.style.display='block';
						//oPopper.childNodes[].style.display='none';
						//alert(oPopper.oBodyTemp.innerHTML);
						//oPopper.oBody.innerHTML=oPopper.oBodyTemp.innerHTML;
						//alert(oPopper.oBody.innerHTML);
						//oPopper.replaceChild(oPopper.oBodyTemp, oPopper.oBody);
						oGod.mBroadcast(oPopper.id+" popper updated");
						if(oIThelp.bShim) oPopper.mShim();
					}
				});
			
			// Setup body & temporary body
			var aDivs=oPopper.getElementsByTagName('DIV');
			var oDiv, i=0;
			while ((oDiv=aDivs[i++])!=null)
				if (oDiv.className=='body') {
					oPopper.oBody=oDiv;
					//oDiv.style.display='none';
				}
			//var oBodyTemp=document.createElement('div');
			
			//oPopper.oBody.style.display='none';
			//oPopper.oBody.parentNode.appendChild(oBodyTemp);
			//oPopper.oBodyTemp=oBodyTemp;
			//oIThelp.mAddClass(oPopper.oBodyTemp, "body bodyselected");
			oPopper.oBodyTemp=oIThelp.createEmbeddedTextNode('div', '...', 'body');
			oPopper.appendChild(oPopper.oBodyTemp);
			/*oPopper.oBody.style.visibility='hidden';
			oPopper.oBodyTemp.style.visibility='visible';*/
			oPopper.oBody.style.display='none';
			oPopper.oBodyTemp.style.display='block';
			oPopper.oBodyTemp.style.paddingBottom='10px';
			
			/*oPopper.mClear=oPoppers.mClear;
			oPopper.mClear();*/
		},
		
		mPop : function ( bPop, e ) { // pop and position popper
 			//oPoppers.aSemaphor[this.id]=0;
			if ( arguments.length == 0 ) bPop = !this.bPopped;
			switch ( bPop ) {
				case true :
					this.oBody.style.display='none';
					this.oBodyTemp.style.display='block';
					var zIndex=oPoppers.mGetZIndex();
					oPoppers.wPoppers[this.id]=zIndex;
					this.style.zIndex=zIndex;
					if (oIThelp.bShim) this.oShim.style.zIndex=zIndex-1;
					if ( this.bPopped ) {
						oGod.mBroadcast ( this.id + " reopened" );
					}
					else {
						this.bInitializing=true;
						this.style.display = "block";
						
						if ( !this.bFirstpop ) {
							this.mCenter ();
							this.bFirstpop = true;
						}
						if ( arguments.length != 0 && e ){this.mEffect ( e );}
						this.bPopped = true;
						this.bInitializing=false;
					}
					oGod.mBroadcast ( this.id + " popper updated" );

					break;
				case false :
					oPoppers.wPoppers[this.id]=null;
					this.statusBar.clear();
					if (oIThelp.bShim) {
						var oIFrame = oIThelp.getElementById(this.id+'_shim');
						//oDebug.alertA(oIFrame);
						oIFrame.style.display = "none";
						//alert(oIFrame.style.display);
					}
					this.oBody.innerHTML = '';
					this.style.display = "none";
					this.bPopped = false;
					oGod.mBroadcast ( this.id + " closed" );
					oPoppers.wPoppers[this.id]=null;
					break;
			}
		},
		
		mCenter : function () { // center on stage
			var iL = (0.5 * ( oTools.mWindow ( "width" ) -  this.offsetWidth )) + (oClient.bIE ? (oTools.mWindow ( "xscroll" )) : 0);
			var iT = 100 + (oClient.bIE ? (oTools.mWindow ( "yscroll" )) : 0);
			this.style.left	= iL + "px";
			this.style.top	= ( iT > 0 ? iT : 0 ) + "px";
			this.iCSX = (oTools.mWindow ( "xscroll" ));
			this.iCSY = (oTools.mWindow ( "yscroll" ));
		},
		
		mDrag : function(e) { // drag show
			e = e ? e : window.event;
			switch ( e.type ) {
				case "mousedown" :
					window.oPop	= e.currentTarget ? e.currentTarget.parentNode : e.srcElement.parentNode;
					if(oClient.bIE){ // IE bug when the popper contains textarea and the textarea has a long content.
						var aTextAreas = oPop.getElementsByTagName('TEXTAREA');
						for(var trick = 0; trick < aTextAreas.length; trick++){
							var tmpArea = aTextAreas[trick];
							var oTmpDiv = document.createElement('DIV'), oRef = aTextAreas[trick].parentNode;
							oTmpDiv.style.height = aTextAreas[trick].offsetHeight;
							oTmpDiv.style.width = aTextAreas[trick].offsetWidth;
							oTmpDiv.style.border = '1px solid black';
							oTmpDiv.style.backgroundColor = 'white';
							oPoppers.oIETextAreaTricky[tmpArea.id] = new Array(tmpArea, oTmpDiv);
							aTextAreas[trick].parentNode.removeChild(aTextAreas[trick]);
							oRef.appendChild(oTmpDiv);
						}
					}
					oPop.iX		= e.clientX + oTools.mWindow ( "xscroll" );
					oPop.iY		= e.clientY + oTools.mWindow ( "yscroll" );
					oPop.iL		= oPop.offsetLeft;
					oPop.iT  	= oPop.offsetTop;
					oPop.iW		= oPop.offsetWidth;
					oPop.iH 	= oPop.offsetHeight;
					oLayout.oRoot.onmousemove = oPoppers.mDrag;
					oLayout.oRoot.onmouseup = oPoppers.mDrag;
					break;
				case "mousemove" :
					if ( oClient.bIE && e.button != 1 ) oLayout.oRoot.fireEvent ( "onmouseup" );
					else {
						var iX = e.clientX + oTools.mWindow ( "xscroll" );
						var iY = e.clientY + oTools.mWindow ( "yscroll" );
						var iH = oTools.mWindow ( "height" ) + oTools.mWindow ( "yscroll" );
						var iW = oTools.mWindow ( "width" ) + oTools.mWindow ( "xscroll" );
						var iO = 16;
						var iL = iX + ( oPop.iW - ( oPop.iX - oPop.iL )) > iW - iO ? iW - oPop.offsetWidth - iO : ( iX - ( oPop.iX - oPop.iL ) < 0 ? 0 : oPop.iL + iX + - oPop.iX );
						var iT = iY + ( oPop.iH - ( oPop.iY - oPop.iT )) > iH ? iH - oPop.offsetHeight : ( iY - ( oPop.iY - oPop.iT ) < 0 ? 0 : oPop.iT + iY + - oPop.iY );
						oPop.style.left = iL + "px";
						oPop.style.top = ( iT > 0 ? iT : 0 ) + "px";
						//oIThelp.setStatusBar('L: '+iL + '  T: '+iT);
						if ( e.preventDefault ) e.preventDefault ();
						else e.returnValue = false;
						if (oIThelp.bShim) oPop.mShim();
					}
					break;
				case "mouseup" : 
					if(oClient.bIE){
						for (sTextArea in oPoppers.oIETextAreaTricky){
							var oRef = oPoppers.oIETextAreaTricky[sTextArea][1].parentNode, oTmpDiv = oPoppers.oIETextAreaTricky[sTextArea][1], oArea = oPoppers.oIETextAreaTricky[sTextArea][0];
							oRef.removeChild(oTmpDiv);
							oRef.appendChild(oArea);
						}
						oPoppers.oIETextAreaTricky = {};
					}
					oPop.iCSX = (oTools.mWindow ( "xscroll" ));
					oPop.iCSY = (oTools.mWindow ( "yscroll" ));
					oLayout.oRoot.onmousemove = null;
					oLayout.oRoot.onmouseup = null;
					break;
			}
		},
		
		mResize : function ( e ) { // vertical resize
			e = e ? e : window.event;
			oIThelp.setStatusBar('mResize: 1');
			var oPop = e.currentTarget ? e.currentTarget.parentNode : e.srcElement.parentNode;
			if (oPop.className!=null && !oPop.className.contains('popped')) oPop=oPop.parentNode;
			switch ( e.type ) {
				case "mousedown" :
					window.oBody	= oPop.oBody;
					oBody.iH 		= oBody.offsetHeight;
					oBody.iY		= e.clientY;
					oLayout.oRoot.onmousemove = oPoppers.mResize;
					oLayout.oRoot.onmouseup = oPoppers.mResize;
					break;
				case "mousemove" :
					if ( oClient.bIE && e.button != 1 ) oLayout.oRoot.fireEvent ( "onmouseup" );
					else {
						var iHeight = oBody.iH - ( oBody.iY - e.clientY );
						oBody.style.height = ( iHeight > 0 ? iHeight : 1 ) + "px";
						if ( e.preventDefault ) e.preventDefault ();
						else e.returnValue = false;
						if (oIThelp.bShim && oPop.mShim) oPop.mShim();
					}
					break;
				case "mouseup" :
					oLayout.oRoot.onmousemove = null;
					oLayout.oRoot.onmouseup = null;
					break;
			}
		},
		
		mEffect : function ( e ) { // cheap effect
			if ( !( oClient.bIEwin || oClient.bMozilla || oClient.bOpera )) return;
			e 						= e ? e : window.event;
			var oDiv 				= document.createElement ( "div" );
			var yOffset				= oClient.bIEwin ? 0 : oTools.mWindow ( "yscroll" );
			oDiv.className 			= "effect";
			oDiv.startX 			= e.clientX - 5;
			oDiv.startY 			= e.clientY + yOffset - 5;
			oDiv.diffX 				= this.offsetLeft - oDiv.startX;
			oDiv.diffY				= this.offsetTop + yOffset - oDiv.startY;
			oDiv.endW				= this.offsetWidth;
			oDiv.endH				= this.offsetHeight;
			this.style.visibility 	= "hidden";
			oDiv.style.zIndex		= this.style.zIndex;
			this.iCount 			= 0;
			
			document.body.appendChild ( oDiv );
			var oPop = this;
			var effect = function () {
				
				switch ( oPop.iCount < 90 ) {
					case true :
						var iFactor 		= Math.sin ( oPop.iCount * Math.PI / 180 );
						oDiv.style.left 	= oDiv.startX + iFactor * oDiv.diffX + "px";
						oDiv.style.top 		= oDiv.startY + iFactor * oDiv.diffY + "px";
						oDiv.style.width 	= oDiv.endW * iFactor + "px";
						oDiv.style.height 	= oDiv.endH * iFactor + "px";
						oPop.iCount 		+= oClient.bIEwin ? 5 : 10;
						break;
					case false :
						window.clearInterval ( oPop.oInterval );
						oPop.bInitializing=false;
						oDiv.parentNode.removeChild ( oDiv );
						oPop.style.visibility = "visible";
						break;
				}
			}
			oPop.oInterval = window.setInterval ( effect, (oClient.bIEwin?10:30) ); 
		},
		
		mConstrain : function () {
			var iAdjustment = 70;
			var bConstrain =  20 + this.offsetHeight > oTools.mWindow ( "height" );
			//oIThelp.setStatusBar(this.id+' '+oTools.mWindow ( "height" )+' '+(20 + this.offsetHeight)+'g');
			this.className = this.className [ bConstrain ? "attach" : "detach" ] ( "constrained" );
			
			this.oBody.style.height = bConstrain ? oTools.mWindow ( "height" ) - this.offsetTop - iAdjustment + "px" : "auto";
			
			//alert((parseInt(this.style.top.replaceAll('px', '')) - this.iCSY + oTools.mWindow ( "yscroll" )));
			if(oClient.bIE){
				this.style.top = (parseInt(this.style.top.replaceAll('px', '')) - this.iCSY + oTools.mWindow ( "yscroll" )) + 'px';
				this.style.left = (parseInt(this.style.left.replaceAll('px', '')) - this.iCSX + oTools.mWindow ( "xscroll" )) + 'px';
			}
			this.iCSX = (oTools.mWindow ( "xscroll" ));
			this.iCSY = (oTools.mWindow ( "yscroll" ));
			
			//this.mShim();
			if (oIThelp.bShim) {
				this.oShim.style.top = this.style.top;
				this.oShim.style.left = this.style.left;
				this.oShim.style.height = this.offsetHeight;
			}
		},
		
		mSetTitle : function ( sTitle ) {
			if ( this.oTitle && this.oTitle.nodeType == Node.TEXT_NODE ) this.oTitle.nodeValue = sTitle;
		},
		
		mShim : function () {
			this.oShim.style.width = this.offsetWidth;
			this.oShim.style.height = this.offsetHeight;
			this.oShim.style.top = this.style.top;
			this.oShim.style.left = this.style.left;
			this.oShim.style.display = "block";
		}
}

// initialize
oGod.mOnload ( oPoppers.mInit("worksheet", true) );
