DialogBox = Class.create({
	_obj: null,
	_id: null,
	_h: null,
	_w: null,
	_t: null,
	_c: null,
	_z: null,
	_pz: null,
	_posx: null,
	_posy: null,
	
	initialize: function ( p ) {
		this._id = "db_" + Math.ceil(Math.random()*100);
		if(! p)
			p = new Object();

		this._h = p.h;
		this._w = p.w;
		this._t = (p.t && p.t !== '') ? p.t : '';
		this._c = (p.c && p.c !== '') ? p.c : '';
		this._p = (p.p && p.p !== '') ? p.p : null;

		this._obj = new Object();
		
		var _shadow = document.getElementsByClassName("obg");
		if(_shadow.length > 0)
		{
			this._obj.shadow = _shadow[0];
			this._pz = parseInt(this._obj.shadow.getStyle("z-index"),0);
			this._z = this._pz + 2;
		}
		else
		{
			$(document.getElementsByTagName("body")[0]).insert('<div class="obg"></div>');
			this._obj.shadow = document.getElementsByTagName("body")[0].select("div[class='obg']")[0];
			this._z = 50;
		}

		$(document.getElementsByTagName("body")[0]).insert(this._templates['box'].evaluate(this));

		this._obj.viewer = document.getElementsByTagName("body")[0].select("div[id='viewer_" + this._id + "']")[0];

		if(this._h && this._h > 0)
			this._obj.viewer.setStyle({height: this._h + "px"});
		if(this._w && this._w > 0)
			this._obj.viewer.setStyle({width: this._w + "px"});
		
		this._obj.content = this._obj.viewer.select("div[class='content']")[0];
		this._obj.content.insert(this._c);
		
		this._obj.shadow.setStyle({height: document.viewport.getHeight() + "px", 'zIndex': this._z});
		this._obj.viewer.setStyle({'marginLeft': (this._obj.viewer.getWidth() / 2) * -1 + "px", 'marginTop': (this._obj.viewer.getHeight() / 2) * -1 + "px", 'zIndex': this._z + 1});		
		this._obj.viewer.select("img[class='close']")[0].observe( "click", this._close.bind( this ) );

		if(this._p)
		{
			if(this._p.closeable && this._p.closeable == 'no')
				this._obj.viewer.select("img[class='close']")[0].setStyle({"visibility": "hidden"});
		}
		
		this._obj.viewer.observe( "mousedown", this._mousedown.bindAsEventListener( this ) );
	},

	_mousedown: function ( e ) {
		var elt = this._obj.viewer;
		var dims = document.viewport.getDimensions( );
		
		if(e.findElement("div").hasClassName("title") && ! e.element().hasClassName("close"))
		{
			this._posx = elt.viewportOffset().left - e.pointerX();
			this._posy = elt.viewportOffset().top - e.pointerY();
			
			document.observe( "mousemove", this._mousemove.bindAsEventListener( this ) );
			document.observe( "mouseup", this._mouseup.bind( this ) );
			document.onmousedown = function(){return false;};
			document.onselectstart = function(){return false;};
		}
		return false;
	},
	
	_mousemove: function ( e ){
		this._obj.viewer.setStyle( {"margin": "0px"} );
		
		var nPosX = e.pointerX() + this._posx;
		var nPosY = e.pointerY() + this._posy;
		
		if(nPosX <= 0)
			nPosX = 0;
		
		if(nPosY <= 0)
			nPosY = 0;
		
		if(nPosX + this._obj.viewer.getDimensions().width < document.viewport.getDimensions().width)
			this._obj.viewer.setStyle( {"left": nPosX + "px"} );
		else
			this._obj.viewer.setStyle( {"left": document.viewport.getDimensions().width - this._obj.viewer.getDimensions().width + "px"} );
		
		if(nPosY + this._obj.viewer.getDimensions().height < document.viewport.getHeight())
			this._obj.viewer.setStyle( {"top": nPosY + "px"} );
		else
			this._obj.viewer.setStyle( {"top": document.viewport.getHeight() - this._obj.viewer.getDimensions().height + "px"} );
	},
	
	_mouseup: function ( ){
		document.stopObserving( "mousemove" );
		document.stopObserving( "mouseup" );
		document.onmousedown = null;
		document.onselectstart = null;
	},
	
	_close: function ( ) {
		if(this._pz)
			this._obj.shadow.setStyle({'zIndex': this._pz});
		else
			this._obj.shadow.remove();

		this._obj.viewer.remove();
	},
	
	_templates: {
		box: new Template(
			'<div class="viewer" id="viewer_#{_id}">' +
			'<table width="100%" height="100%" cellspacing="0" cellpadding="0">' +
				'<tr><td class="tl corner" /><td class="t" /><td class="tr corner" /></tr>' +
				'<tr><td class="l" /><td class="c">' +
					'<div class="title"><h1>#{_t}</h1><img src="img/close.jpg" class="close" /></div>' +
					'<div class="content"></div>' +
					'</td><td class="r" /></tr>' +
				'<tr><td class="bl corner" /><td class="b" /><td class="br corner" /></tr>' +
			'</table>' + 
			'</div>')
	}
});