MiniCMS.Treeviews = new Object();

MiniCMS.Treeview = function( id ) {
	this.TreeID = id;
	this.SelectedID = null;
	this.SelectedName = null;
	this.SelectedNode = null;
	this.SelectedChildnodes = null;
	this.Cookie = new MiniCMS.Cookie( id );
	for( var i in this.Cookie.All() )
		if( document.getElementById(this.TreeID+'_Childnodes_'+i.substring(1)) )
			this.ClickNode( i.substring(1) );
		else this.Cookie.Del( i );
	MiniCMS.Treeviews[id] = this;
}

MiniCMS.Treeview.prototype.ClickNode = function( id ) {
	var node = document.getElementById( this.TreeID + '_Node_' + id );
	var childnodes = document.getElementById( this.TreeID + '_Childnodes_' + id );
	if( childnodes ) {
		var visible = MiniCMS.ToggleDisplay( childnodes );
		var icon = MiniCMS.FindChild( node, 'className', /NodeIcon/ ).parentNode;
		icon.className = icon.className.replace( /Open|Closed/, visible ? 'Open' : 'Closed' );
		icon = document.getElementById( this.TreeID + '_Nav_' + id );
		icon.className = icon.className.replace( /Open|Closed/, visible ? 'Open' : 'Closed' );
		if( !visible ) this.Cookie.Set( '_' + id, 'x' );
		else this.Cookie.Del( '_' + id );
	}
}

MiniCMS.Treeview.prototype.Select = function( id ) {
	this.SelectedID = id;
	this.SelectedNode = document.getElementById( this.TreeID + '_Node_' + id );
	this.SelectedChildnodes = document.getElementById( this.TreeID + '_Childnodes_' + id );
	var namenode = MiniCMS.FindChild( this.SelectedNode, 'className', /NodeName/ );
	if( namenode )
		this.SelectedName = namenode.innerText ? namenode.innerText : namenode.textContent;
	else 
		this.SelectedName = null;
	if( this.OnSelect )
		this.OnSelect();
}

MiniCMS.Treeview.prototype.Open = function() {
	$('#'+this.TreeID).find('.Closed').click();
}

MiniCMS.Treeview.prototype.Close = function() {
	$('#'+this.TreeID).find('.Open').click();
}
