// Copyright (c) 2005-2010 Wavecrest Computing, Inc.  http://www.wavecrest.net

var popupCounter = 0;
var defaultPopupWindow = "<div id=\"popupidXXX\" class=\"submittingpopup\" style=\"display: none;\">" +
						"<!--[if lte IE 7]><table border=\"0\"  cellpadding=\"0\" cellspacing=\"0\" ><tr class=\"popupiehack\"><td><img src=\"/include/images/iefill.gif\" width=\"386\" height=\"1\"  /><td></tr><tr><td><![endif]-->"+
						"<table class=\"popuptopperwrapper\" id=\"draggabletopperXXX\" cellpadding=\"0\" cellspacing=\"0\">" +
						"<tbody>" +
						"<tr>" +
						"<td class=\"popuptoppergraphic\"></td>" +
						"<td id=\"popupfillXXX\" class=\"popuptopperfill\">" +
						"<td id=\"popupimgXXX\" class=\"popuptopperclose\">" +
						"</tr>" +
						"</tbody>" +
						"</table>" +
						"<div id=\"popupmsgXXX\" class=\"popupmsg\">initializing...</div>" +
						"<div class=\"popupcontentbottom\">" +
						"<div id=\"popupactionXXX\" class=\"popupaction\">" +
						"<input type=\"button\" id=\"popupyesXXX\" name=\"popupyesXXX\" class=\"buttonM\" value=\"yes\">" +
						"<input type=\"button\" id=\"popupnoXXX\" name=\"popupnoXXX\" class=\"buttonM\" value=\"no\">" +
						"</div>" +
						"</div>" +
						"<!--[if lte IE 7]></td></tr></table><![endif]-->"+
						"</div>";
						
var blackoutBackground = "<div style=\"display:none\" id=\"blackout\" class=\"submittingBackground\" ></div>";

/* ------------------------------------------------------------------------ */
/**
	* Options:
	*			blackoutId - The id of the blackout div. If this option is not
	*						 passed, the screen will not blackout behind the popup.
	*			stationary - If this option is passed with any value, the popup div
	*						 will not be draggable.
	*			fixedWidth - Fixed width to set the popup window too.
	*						 A value of -1, sets to default width of XXX.
	*						 If this option is not passed, the window's width
	*						 will grow dynamically.
	*			positionId - The id of the object to display the popup next to.
	*						 If the object does not exist, or this option is not passed,
	*						 the popup will be centered.
	*			onhide		- Function to call during each hide. This means the ok and cancel button as
	*							well as the x image.
	*			bodyElement	- The id that is the full page/document wrapper. 
	*			
	* <BR><BR>
	* @param
	* @return
	* @author Rudy Coelho
**/
/* ------------------------------------------------------------------------ */
var floatingPopup = Class.create(
		{ 
			initialize: function( options )
			{
				this.popupid = popupCounter++;
				this.options = (options)?Object.clone(options):$H();
				
				this.bodyObj = $( (this.options.bodyElement)?this.options.bodyElement:"body" );
				if( this.bodyObj )
				{
					this.bodyObj.insert( { top: defaultPopupWindow.replace( /XXX/g, this.popupid ) } );
					this.bodyObj.insert( { top: blackoutBackground } );
					this.bodyObjDiv = $( this.bodyObj );
				}
				this.onclickFunction = null;

				this.timeoutId;
				this.popupDiv	= $( "popupid" + this.popupid );
				if( this.options.blackoutId )
				{
					this.blackoutDiv = $( options.blackoutId );
				}
				
			
				//if( this.options.fixedWidth )
				//	this.popupDiv.addClassName( "XXX" );
				
				this.filler = $( 'popupfill' + this.popupid );
				
				if( !this.options.stationary )
				{
					var topper = $( 'draggabletopper' + this.popupid );
					topper.observe( 'mouseover', this.enableDraggable.bind( this ) );
					topper.observe( 'mouseout', this.disableDraggable.bind( this ) );
				}
				
				$( "popupyes" + this.popupid ).observe( 'click',
																	function( oEvent )
																	{
																		oEvent = oEvent || window.event;

																		if( oEvent.type == "click" )
																		{
																			if( this.handleYes( oEvent ) == false )
																			{
																				return (false );
																			}
																		}
																	}.bindAsEventListener( this )
														);

				var closeFunction = function( oEvent )
									{
										oEvent = oEvent || window.event;

										if( oEvent.type == "click" )
										{
											this.handleNo();
										}
									}.bindAsEventListener( this );
									
				$( "popupimg" + this.popupid ).observe( 'click', closeFunction );
				$( "popupno" + this.popupid ).observe( 'click', closeFunction );
			},
			setBody: function ( bodyToSet )
			{
				var obj = $( "popupmsg" + this.popupid );
				obj.update( bodyToSet );
			},
			disableActions: function()
			{
				$( "popupaction" + this.popupid ).hide();
			},
			enableActions: function()
			{
				$( "popupaction" + this.popupid ).show();
			},
			disableOkButton: function()
			{
				$( "popupyes" + this.popupid ).hide();
			},
			enableOkButton: function()
			{
				$( "popupyes" + this.popupid ).show();
			},
			setOkLabel: function( newlabel )
			{
				$( "popupyes" + this.popupid ).value = newlabel;
			},
			disableCancelButton: function()
			{
				$( "popupno" + this.popupid ).hide();
			},
			enableCancelButton: function()
			{
				$( "popupno" + this.popupid ).show();
			},
			setCancelLabel: function( newlabel )
			{
				$( "popupno" + this.popupid ).value = newlabel;
			},
			setAction: function ( yesFunction )
			{
				this.onclickFunction = yesFunction;	
			},
			handleNo: function ()
			{
				this.hidePopup();
			},
			handleYes: function ( e )
			{
				var ret = true;
				
				try
				{
					if( this.onclickFunction != null )
					{
						ret = this.onclickFunction( e );
					}
				}
				catch( e )
				{
					alert( "ERROR: While performing action. " + e.toString() );
				}
				
				this.hidePopup();
				
				return ( ret );
			},
			enableDraggable: function ()
			{
				if( !this.draggableObj )
						this.draggableObj = new Draggable( this.popupDiv );
			},
			disableDraggable: function ()
			{
				if( this.draggableObj )
				{
					this.draggableObj.destroy();
					this.draggableObj = undefined;
				}
			},
			displayPopup: function( timeout )
			{
				var height = document.viewport.getHeight(); //visible Height
				var width = document.viewport.getWidth(); //visible Width
				var scrollTop = document.viewport.getScrollOffsets().top; //verticle scroll distance from top of page
				var scrollLeft = document.viewport.getScrollOffsets().left; //horizontal scroll distance from left side of page
				//var pageHeight = this.bodyObjDiv.getHeight() + 'px';
				var pageHeight = height + scrollTop + 'px';
				var pageWidth = width + scrollLeft + 'px';
				this.blackoutDiv.style.height = pageHeight;
				this.blackoutDiv.style.width = pageWidth;
	
				if( this.blackoutDiv )
					this.blackoutDiv.appear( { 'duration': 0.5, 'from': 0.0, 'to': 0.3, 'afterFinish': this._displayPopup.bind( this, timeout ) } );
				else
					this._displayPopup( timeout );
			},
			_displayPopup: function ( timeout )
			{
				var wv = 0;
				
				for( var i = 0; i < popupCounter; i++ )
				{
					var prevconf = $( "popupid" + i );
					if( prevconf )
						prevconf.hide();
				}
				
				this.popupDiv.show();
				
				wv = this.popupDiv.offsetWidth - 386;
				if( wv > 0 )
					this.filler.setStyle({width: wv + 'px'});
		
				this.positionPopup();
				
				if( this.timeoutId )
					window.clearTimeout( this.timeoutId )
						
				if( timeout )
					timeoutId = this.hidePopup.bind(this).delay( timeout );			
			},
			hidePopup: function ()
			{
				this.disableDraggable();
				
				this.popupDiv.hide();
				
				if( this.blackoutDiv )
					this.blackoutDiv.hide();
					
				if( Object.isFunction( this.options.onhide ) )
					this.options.onhide();
			},
			positionPopup: function ()
			{ 
				var positionDiv = $( this.options.positionId );
				if( positionDiv )
				{
					var offsetElement = $(positionDiv);
   					var offestEelementVal = offsetElement.cumulativeOffset();

					var offset = offestEelementVal[1] - (this.popupDiv.clientHeight)/2;
					this.popupDiv.setStyle({ 'top' : offset + 'px' });
					
					var viewport = document.viewport.getDimensions(); // Gets the viewport as an object literal
					var width = viewport.width; // Usable window width
					this.popupDiv.style.left = (width - this.popupDiv.getWidth())/2+'px';
				}
				else
				{
					var height = document.viewport.getHeight(); //visible Height
					var width = document.viewport.getWidth(); //visible Width
					var scollTop = document.viewport.getScrollOffsets().top; //verticle scroll distance from top of page
					var scollLeft = document.viewport.getScrollOffsets().left; //horizontal scroll distance from left side of page
					this.popupDiv.style.top = height/2 - this.popupDiv.getHeight()/2 + scollTop + 'px';
					this.popupDiv.style.left = width/2 - this.popupDiv.getWidth()/2 + scollLeft + 'px';
					
				}
			}	
		}
							
	);
	
	