
	// mode = {'standard', 'transparent'}
	function dialog(caption, mode)
	{
		if(mode == undefined) mode = 'standard';
		
		dialog.parentClass.constructor.apply(this, arguments);
		
		var protoElement = document.getElementById('window_'+ mode +'_prototype');
		this.element = protoElement.cloneNode(true);
		this.element.removeAttribute('id');
		protoElement.parentNode.appendChild(this.element);

		this.window = selectNode(".//*[contains(@class, 'dialog')]", this.element);
		this.window.dialog = this;
		
		this.closeButton = selectNode(".//*[contains(@class, 'closeButton')]", this.window);
		this.captionContainer = selectNode(".//*[contains(@class, 'captionContainer')]", this.window);
		this.contentContainer = selectNode(".//*[contains(@class, 'contentContainer')]", this.window);

		if(caption !== undefined) this.setCaption(caption);
		else this.captionContainer.className += ' hidden';
		
		this.closeButton.dialog = this;
		this.closeButton.onclick = function() {this.dialog.close(); if(this.dialog.autoDestroy) this.dialog.destroy();};

		this.captionContainer.onmousedown = function(event)
		{
			this.dialog.oldX = (window.event) ? window.event.clientX : event.pageX;
			this.dialog.oldY = (window.event) ? window.event.clientY : event.pageY;
		};

		this.captionContainer.onmouseup = function(event)
		{
			this.dialog.oldX = undefined;
			this.dialog.oldY = undefined;
		};		
		
		this.captionContainer.onmousemove = function(event)
		{
			if(this.dialog.oldX && this.dialog.oldY)
			{
				var x = (window.event) ? window.event.clientX : event.pageX;
				var y = (window.event) ? window.event.clientY : event.pageY;
			
				this.dialog.captionContainer.style.top = (this.dialog.captionContainer.offsetTop + y - this.dialog.oldY) + 'px';
				this.dialog.captionContainer.style.left = (this.dialog.captionContainer.offsetLeft + x - this.dialog.oldX) + 'px';
				
				this.dialog.oldX = x;
				this.dialog.oldY = y;
			}
		}

		this.window.fading = new fading(this.window);
		this.window.fading.value = 0;
	}

	extend(dialog, control);

	dialog.prototype.destroy = function()
	{
		this.window.dialog = undefined;
		this.element.parentNode.removeChild(this.element);
		dialog.parentClass.destroy.apply(this, arguments);
	}

	dialog.prototype.getType = function()
	{
		return 'dialog';
	}

	dialog.prototype.caption;
	dialog.prototype.closeButton;
	dialog.prototype.contentContainer;
	dialog.prototype.autoDestroy = false;

	// content - DOMFragment
	dialog.prototype.setContent = function(content) 
	{
		if(this.contentContainer.ownerDocument != content.ownerDocument)
			content = importElement(content, this.contentContainer.ownerDocument, false);

		if(content.outerHTML) this.contentContainer.innerHTML = content.outerHTML;
		else
		{
			while(this.contentContainer.lastChild) this.contentContainer.removeChild(this.contentContainer.lastChild);
			this.contentContainer.appendChild(content);
		}
	}

	dialog.prototype.setCaption = function(caption)
	{
		while(this.captionContainer.lastChild) this.captionContainer.removeChild(this.captionContainer.lastChild);
		this.caption = caption;
		this.captionContainer.appendChild(document.createTextNode(caption));
	}

	dialog.prototype.open = function(left, top)
	{
		document.eventer.forceEvent('onDialogOpen', {dialog: this})
	
		if(top == undefined) top = getScrollOffset().top;
		this.window.style.top = top + 'px';
		if(left != undefined) this.window.style.left = left + 'px';
		this.show();

		this.window.fading.unfade(0.03);
	}

	dialog.prototype.isOpen = function()
	{
		return !this.isHidden();
	}

	dialog.prototype.close = function()
	{
		this.hide();
		document.eventer.forceEvent('onDialogClose', {dialog: this})

		this.window.fading.fade(0.05);
	}
