/**
	Lightweight linked list implementation
*/
function lightweight_list() {
	
	var self = this;
	
	self.first = null;
	self.last = null;
	
	self.count = 0;
	
	self.append = function(new_value) {
		self.count++;
		var node = {
			next:null,
			prev:null,
			value:new_value
		};
		if (!self.last) {
			self.first = node;
			self.last = node;
			return;
		}
		self.last.next = node;
		node.prev = self.last;
		self.last = node;
	};
	self.prepend = function(new_value) {
		self.count++;
		var node = {
			next:null,
			prev:null,
			value:new_value
		};
		if (!self.first) {
			self.first = node;
			self.last = node;
			return;
		}
		self.first.prev = node;
		node.next = self.first;
		self.first = node;
	}
	self.after = function(existing, new_value) {
		if (!existing || existing == self.last) {
			self.append(new_value);
			return;
		}
		var node = {
			next:null,
			prev:null,
			value:new_value
		};
		node.next = existing.next;
		node.prev = existing;
		node.next.prev = node;
		node.prev.next = node;
		self.count++;
	};
	self.before = function(existing, new_value) {
		if (!existing || existing == self.first) {
			self.prepend(new_value);
			return;
		}
		var node = {
			next:null,
			prev:null,
			value:new_value
		};
		node.next = existing;
		node.prev = existing.prev;
		node.next.prev = node;
		node.prev.next = node;
		self.count++;
	};
	self.remove = function (existing) {
		if (existing==self.first && existing==self.last) {
			self.first = null;
			self.last = null;
		}
		else if (existing==self.first) {
			self.first = existing.next;
			self.first.prev = null;
		}
		else if (existing==self.last) {
			self.last = existing.prev;
			self.last.next = null;
		}
		else {
			existing.next.prev = existing.prev;
			existing.prev.next = existing.next;
		}
		existing.next = null;
		existing.prev = null;
		var temp = existing.value;
		delete existing;
		self.count--;
		return temp;
	};
	self.clear = function () {
		while (self.first) {
			self.remove(self.first);
		}
		self.count = 0;
	};
}