CMS.Explorer = CMS.Widget.extend({
	title: "Explorer",
	multiple: true,
	ok: "Ok",
	cancel: "Annuleren",
	
	_window: null,
	_tree: null,
	
	_buttons: null,
	_ok: null,
	_cancel: null,
	
	_callbacks: [],
	
	show: function() {
		this._buttons = document.createElement("div");
		this._ok = document.createElement("button");
		this._cancel = document.createElement("button");
		addClass(this._buttons, 'Buttons');
		
		this._buttons.appendChild(this._cancel);
		this._buttons.appendChild(this._ok);
		
		var _this = this;
		addEvent(this._ok, 'click', function() {_this.handleOk();});
		addEvent(this._cancel, 'click', function(){_this.handleCancel();});
		
		this._ok.appendChild(document.createTextNode(this.ok));
		this._cancel.appendChild(document.createTextNode(this.cancel));
	
		this._tree = new CMS.Explorer.Tree();
		this._tree.multiple = this.multiple;
		
		this._window = new CMS.Window(this.title);

		this._window.add(this._tree);
		this._window.addElement(this._buttons);

		this._window.show();
	},
	
	hide: function() {
		this._window.hide();
	},
	
	handleOk: function() {
		this.hide();
		this.callback(this._tree.getSelected());
	},
	
	handleCancel: function() {
		this.hide();
		this.callback([]);
	},
	
	callback: function(value) {
		if (!this.multiple) {
			if (value.length>0) {
				value = value[0];
			} else {
				value = null;
			}
		}
	
		for (var i = 0; i < this._callbacks.length; i++) {
			this._callbacks[i](value);	
		}
	},
	
	addCallback: function(func) {
		this._callbacks.push(func);
		return this;
	}	
});