DialogManager = function( close_others ){
	this._instances = {};
	this._close_others = close_others == false ? false : true;
};
DialogManager.prototype = {
	_instances:null,
	_opened:null,
	_close_others:null,
	toString:function(){
		return "[DialogManager]";
	},
	set_opened:function( dialog ){
		if( this._close_others ){
			try{
				this._opened.close();
			}catch(e){}
		}
		this._opened = dialog;
	},
	toggle:function( div_or_id ){
		this._find( div_or_id ).toggle();
		return false;
	},
	open:function( div_or_id ){
		this._find( div_or_id ).open();
		return false;
	},
	close:function( div_or_id ){
		this._find( div_or_id ).close();
		return false;
	},
	_find:function( div_or_id ){
		try{
			var id = typeof(div_or_id)=="string" ? div_or_id : div_or_id.id;
			var d = this._instances[id];
			if( d==null )
			{
				d = new Dialog( $("#"+id), this );
				this._instances[id] = d;
			}
			return d;
		}catch(e){}
	}
};

Dialog = function( node, manager )
{
	try{
		var id = node.attr("id");
		this._manager = manager;
		this._opened = false;
		this._node = node;
		this._window = $( this._node.children(".window") );
		
		var node;
		
		this._button_toggle = $( "#"+id+"_toggle" );
		if( !this._button_toggle ){
			node = this._node.children(".toggle")[0];
			if( node ) this._button_toggle = $( node );
		}
		
		this._button_close = $( "#"+id+"_close" );
		if( !this._button_close ){
			node = this._node.children(".close")[0];
			if( node ) this._button_close = $( node );
		}
	}catch(e){}
};
Dialog.prototype = {
	toString:function()
	{
		return "[Dialog "+this._node.attr("id")+"]";
	},
	_manager:null,
	_node:null,
	_button_toggle:null,
	_button_close:null,
	_window:null,
	_opened:null,
	toggle:function(){
		if( !this._opened ){
			this.open();
		}else{
			this.close();
		}
	},
	open:function(){
		if( !this._opened )
		{
			this._manager.set_opened( this );
			if( this._button_toggle!=null ) this._button_toggle.addClass( "toggle_open" );
			if( this._button_close!=null ) this._button_close.addClass( "close_open" );
			this._window.addClass( "window_open" );
			this._opened = true;
		}
	},
	close:function(){
		if( this._opened )
		{
			if( this._button_toggle ) this._button_toggle.removeClass( "toggle_open" );
			if( this._button_close ) this._button_close.removeClass( "close_open" );
			this._window.removeClass( "window_open" );
			this._opened = false;
		}
	}
};
