
// -- collection_list_item --

function collection_list_item() {
	this.next = null;
	this.last = null;
	this.collection_parent = null;
}

// -- collection_list --

function collection_list() {
	this.fisrt = null;
	this.end = null;
	this.count = 0;
}

collection_list.prototype.collection_exists = function(item) {
	var loop = this.first;
	while (loop != null && loop != item) loop = loop.next;
	
	return loop
}

collection_list.prototype.collection_add = function(item) {
	if (item.collection_parent == null) {
		if (this.first == null) {
			this.first = item;
			this.end = item;
			this.count++;
		} else {
			this.end.next = item;
			item.last = this.end;
			this.end = item;
			this.count++;
		}
		
		item.collection_parent = this;
		
		return item;
	} else {
		if (item.collection_parent == this) context.error("Cannot add to collection, item allready included here!");
		else context.error("Cannot add to collection, item allready included in other collection!");
	}
	
	return null;
}

collection_list.prototype.collection_rem = function(item) {
	if (item.collection_parent == this) {
		if (item.last == null && item.next == null) {
			this.first = null;
			this.end = null;
		} else if (item.last == null) {
			this.first = this.first.next;
			this.first.last = null;
		} else if (item.next == null) {
			this.end = this.end.last;
			this.end.next = null;
		} else {
			item.last.next = item.next;
			item.next.last = item.last;
		}
		
		item.collection_parent = null;
		item.next = null;
		item.last = null;
		
		this.count--;
		return item;
	} else {
		context.error("Cannot remove, no such item in collection!");
	}
	
	return null;
}

collection_list.prototype.collection_flush = function() {
	if (this.first != null) {
		var loop = this.first;
		var next = null;
		while (loop != null) {
			next = loop.next;
			loop = next;
		}
		
		this.first = null;
		this.last = null;
	}
}

