Features = function() {
	this.data = {};
	this.currentList = [];
	this.currentId = -1;
	this.current = {};
	this.currentType = "";
	this.currentMin = -1;
	this.currentMax = -1;

	this.page = 1;
	this.pageSize = 12;
}

Features.prototype.createFeatures = function( data, callback ) {
	function yieldList() {
		if( this.current == null ) return;

		callback( this.currentType,
				  this.currentList,
				  this.current.name,
				  this.current.caption );
	}

	function resetLists() {
		yieldList();
		this.currentMin = -1;
		this.currentMax = -1;
		this.currentList = [];
		this.currentId = -1;
		this.current = null;
	}

	function create( parent ) {
		resetLists();
		switch( parent.typ ) {
			case 'OPTIONGROUP': this.currentType = "combobox"; break;
			case 'BITVECTOR': this.currentType = "checkboxlist"; break;
		}
		this.currentId = parent.id;
		this.current = parent;
	}

	function addToList( obj, parent ) {
		if( parent.id != this.currentId ) {
			create( parent );
		}
		this.currentList.push( { id: obj.id, name: obj.name } );
	}

	this.data = data;

	this.iterateFeatures( data, function( obj, parent ) {
		if( obj.typ == 'BOOL' ) {
			if( parent.typ == 'COMPOUND' ) {
				resetLists();
				callback( "checkbox",
					  obj.id,
					  obj.name );
			} else {
				addToList( obj, parent );
			}
		} else if( obj.typ == 'INT' ) {
			resetLists();
			callback( "textfield",
				  obj.id,
				  obj.name );
		} else if( parent.typ == 'RANGE' ) {
			if( this.currentId != parent.id ) {
				resetLists();
				this.currentId = parent.id;
			}

			if( obj.typ == 'MIN' ) {
				this.currentMin = obj.id;
			} else if( obj.typ == 'MAX' ) {
				this.currentMax = obj.id;
			}

			if( this.currentMin != -1 && this.currentMax != -1 ) {
				callback( 	"minmax",
							parent.name,
							this.currentMin,
							this.currentMax );
				this.current = null;
				resetLists();
			}
		}
	} );

	resetLists();
}

Features.prototype.setControl = function( id, value ) {
	this.iterateFeatures( this.data, function( obj, parent ) {
		if( obj.id == id ) {
			switch( obj.typ ) {
				case "BOOL":
					obj.kwert = value? "YES":"NO";
					break;
				case "INT":
				case "MIN":
				case "MAX":
					obj.mwert = value;
					break;
			}
		}
	} );
}

Features.prototype.getControls = function() {
	return {
		offset: this.offset,
		limit: this.limit,
		data: this.data
	};
}

Features.prototype.iterateFeatures = function( data, hof ) {
		function recurse( fl, hof ) {
			if( fl.children != null && fl.children.length > 0 ) {
				for( var i = 0; i < fl.children.length; i++ ) {
					recurse( fl.children[i], hof );
				}
			}

			if( fl.features != null && fl.features.length > 0 ) {
				for( var i = 0; i < fl.features.length; i++ ) {
					hof( fl.features[i], fl );
				}
			}
		}

		for( var i = 0; i < this.data.length; i++ ) {
			var fl = this.data[i];
			recurse( fl, hof );
		}
	};

Features.prototype.setPageSize = function( pageSize ) {
	this.limit = pageSize;
}

Features.prototype.setLimit = function( limit ) {
	this.limit = limit;
}

Features.prototype.setOffset = function( offset ) {
	this.offset = offset;
}

