


function RSPPane ( id, manager, attributes )
{
	this.container     = null;
	this.content       = null;
	this.disable_close = false;
	this.disable_move  = false;
	this.height        = null;
	this.id            = id;
	this.immediate     = false;
	this.manager       = manager;
	this.max_height    = null;
	this.min_height    = null;
	this.max_width     = null;
	this.min_width     = null;
	this.pane          = null;
	this.title         = null;
	this.width         = null;



	this.left    = null;
	this.mouse   = null;
	this.overlay = null;
	this.PADDING = 5;
	this.top     = null;



	for ( var k in attributes )
	{
		if ( typeof this[k] != undefined )
		{
			this[k] = attributes[k];
		}
	}



	RSPPane.prototype.display = function ()
	{
		this.container.appendChild(this.pane);

		var pane_size      = DOMGetSize(this.pane);
		var container_size = DOMGetSize(this.container);

		//
		// Adjust the height as appropriate
		//
		if ( this.height )
		{
			if ( pane_size.height > this.height )
			{
				this.pane.childNodes[1].style.overflowY = 'scroll';
			}

			this.pane.style.height = this.height + 'px';
		}
		else if ( this.max_height && pane_size.height > this.max_height )
		{
			this.pane.style.height                  = this.max_height + 'px';
			this.pane.childNodes[1].style.overflowY = 'scroll';
		}
		else if ( this.min_height && pane_size.height < this.min_height )
		{
			this.pane.style.height = this.min_height + 'px';
		}

		//
		// Adjust the width as appropriate
		//
		if ( this.width )
		{
			this.pane.style.width = this.width + 'px';
		}
		else if ( this.max_width && pane_size.width > this.max_width )
		{
			this.pane.style.width = this.max_width + 'px';
		}
		else if ( this.max_width )
		{
			this.pane.style.maxWidth = this.max_width + 'px';
		}
		else if ( this.min_width && pane_size.width < this.min_width )
		{
			this.pane.style.width = this.min_width + 'px';
		}
		else if ( this.min_width )
		{
			this.pane.style.minWidth = this.min_width + 'px';
		}

		pane_size            = DOMGetSize(this.pane);
		this.left            = parseInt(( container_size.width - pane_size.width ) / 2);
		this.pane.style.left = this.left + 'px';
		this.top             = parseInt(( container_size.height - pane_size.height ) / 2);

		//
		// hack!
		//
		if ( this.top < 0 && this.container == document.getElementById('d_right') )
		{
			this.top = 0;
		}

		this.pane.style.top  = this.top + 'px';
	};



	RSPPane.prototype.dispose = function ( dispose )
	{
		if ( dispose )
		{
			this.manager.disposePane(this);
		}
		else
		{
			try
			{
				this.container.removeChild(this.pane);
			}
			catch ( err )
			{
				//
			}

			delete this.pane;
		}
	};



	RSPPane.prototype.initialize = function ()
	{
		var _this  = this;
		var closer = this.title;

		if ( !this.disable_close )
		{
			closer = [
				[
					'span',
					null,
					this.title
				],
				[
					'div.pane_closer',
					{
						onclick:     function ( event )
						{
							_this.dispose(true);
						},
						onmousedown: function ( event )
						{
							_this.dispose(true);
							return false;
						}
					},
					'x'
				]
			]
		}

		this.pane  = DOM([
			[
				'div',
				{
					style: {
						backgroundColor: '#fff',
						border:          '1px solid black',
						fontFamily:      'arial, sans-serif',
						fontSize:        '8pt',
						position:        'absolute',
						zIndex:          5
					}
				},
				[
					[
						'div',
						{
							onmousedown: function ( event )
							{
								if ( !_this.disable_move && _this.pane && !_this.mouse )
								{
									_this.startMove(event);
								}
							},
							style: {
								backgroundColor: '#444',
								color:           '#ddd',
								cursor:          ( this.disable_move ? '' : 'move' ),
								padding:         '2px'
							}
						},
						closer
					],
					[
						'div',
						{
							style: {
								backgroundColor: '#fff'
							}
						},
						this.content
					]
				]
			]
		]);

		if ( this.immediate )
		{
			this.display();
		}
	};


	RSPPane.prototype.move = function ( event )
	{
		if ( !event )
		{
			event = window.event;
		}

		if ( !this.mouse )
		{
			return;
		}

		var mouse_ = {
			x: ( event.pageX != undefined ? event.pageX : ( event.clientX != undefined ? event.clientX : 0 ) ),
			y: ( event.pageY != undefined ? event.pageY : ( event.clientY != undefined ? event.clientY : 0 ) )
		};

		var dx = mouse_.x - this.mouse.x;
		var dy = mouse_.y - this.mouse.y;

		this.left += dx;
		this.top  += dy;
		this.mouse = mouse_;

		if ( this.left < this.PADDING )
		{
			this.mouse.x += this.PADDING - this.left;
			this.left     = this.PADDING;
		}

		if ( this.top < this.PADDING )
		{
			this.mouse.y += this.PADDING - this.top;
			this.top      = this.PADDING;
		}

		this.pane.style.left = this.left + 'px';
		this.pane.style.top  = this.top  + 'px';
	};



	RSPPane.prototype.startMove = function ( event )
	{
		if ( !event )
		{
			event = window.event;
		}

		if ( this.mouse )
		{
			return;
		}

		var size   = DOMGetSize(this.pane.childNodes[1]);
		this.overlay = DOM([
			[
				'div',
				{
					style: {
						height:   size.height + 'px',
						left:     '0px',
						position: 'absolute',
						top:      '0px',
						width:    size.width + 'px',
						zIndex:   '6'
					}
				},
				' '
			]
		]);
		this.pane.childNodes[1].appendChild(this.overlay);

		//
		// Solve selection problem in IE
		//
		document.onselectstart = function () { return false; };
		document.onmousedown   = function () { return false; };

		//
		// Solve selection problem in FF
		//
		// document.onmousemove   = function () { return false; };

		var _this  = this;
		this.mouse = {
			x: ( event.pageX != undefined ? event.pageX : ( event.clientX != undefined ? event.clientX : 0 ) ),
			y: ( event.pageY != undefined ? event.pageY : ( event.clientY != undefined ? event.clientY : 0 ) )
		};

		var add    = function ( m, f, b ) { document.addEventListener    ? document.addEventListener(m,f,b)    : document.attachEvent('on'+m,f); };
		var remove = function ( m, f, b ) { document.removeEventListener ? document.removeEventListener(m,f,b) : document.detachEvent('on'+m,f); };

		//
		// Set document.onmouseup, document.onmousemove
		//
		var mm = function ( event )
		{
			if ( !event )
			{
				event = window.event;
			}

			if ( !event )
			{
				event = parent.event;
			}

			_this.move(event);

			if ( event.stopPropogation )
			{
				event.stopPropogation();
			}
		};
		var mu = function ( event )
		{
			_this.mouse = null;
			_this.overlay.parentNode.removeChild(_this.overlay);
			remove('mousemove',mm,false);
			remove('mouseup',mu,false);

			//
			// Undo "no select" rule
			//
			document.onmousedown   = null;
			document.onselectstart = null;
		};

		add('mousemove',mm,false);
		add('mouseup',mu,false);
	};



	this.initialize();
}



function RSPPaneManager ()
{
	this.panes = [];



	RSPPaneManager.prototype.clear = function ()
	{
		this.panes.map_(function ( pane ) { pane.dispose(false); });
		this.panes = [];
	};



	RSPPaneManager.prototype.disposeAll = function ()
	{
		this.panes.map_(function ( pane ) { pane.dispose(false); });
		this.panes = [];
	};



	RSPPaneManager.prototype.disposePane = function ( pane )
	{
		for ( var i = 0; i < this.panes.length; ++i )
		{
			if ( this.panes[i] == pane )
			{
				pane.dispose(false);
				this.panes.splice(i,1);
				--i;
			}
		}
	};



	RSPPaneManager.prototype.getPane = function ( id, attributes )
	{
		if ( !id )
		{
			id = attributes.title;
		}

		for ( var i = 0; i < this.panes.length; ++i )
		{
			if ( this.panes[i].id == id )
			{
				return this.panes[i];
			}
		}

		this.panes.push(new RSPPane(id,this,attributes));
		return this.panes[this.panes.length-1];
	};
}



RSPPaneFactory = function ( manager, container )
{
	this.container = container;
	this.manager   = manager;



	RSPPaneFactory.prototype.showClosingDialog = function ( id, content, buttons, options )
	{
		var dialog;

		options = _objectNVL(options,{
			container: this.container,
			immediate: true
		});

		options.content   = [
			[
				'div',
				{
					style: {
						padding: '2px',
						width:   '250px'
					}
				},
				[
					[
						'div',
						null,
						content
					],
					[
						'div',
						{
							style: {
								textAlign: 'center'
							}
						},
						buttons.map(function ( button ) {
							return [
								'input',
								{
									onclick: function ( event )
									{
										dialog.dispose(true);
										button.onclick(event);
									},
									type:    'button'
								},
								button.value
							];
						})
					]
				]
			]
		];

		dialog = this.manager.getPane(
			id,
			options
		);

		return dialog;
	};



	RSPPaneFactory.prototype.showLoginDialog = function ( id, success, options )
	{
		var dialog;

		options = _objectNVL(options,{
			container:    this.container,
			disable_move: true,
			immediate:    true,
			title:        'login required'
		});

		options.content      = [
			[
				'iframe',
				{
					frameborder:  0,
					marginheight: 0,
					style:        {
						height: '300px',
						width:  '290px'
					}
				},
				'login.inlay.php'
			]
		];

		dialog = this.manager.getPane(
			id,
			options
		);

		parent.window.sandbox.loginComplete = function ()
		{
			dialog.dispose(true);
			success();
		};

		return dialog;
	};
}




