function Notebook(name) {
	this.name = name;

	this.load();
}

Notebook.prototype.records = null;
Notebook.prototype.cursor = 0;

Notebook.daysExp = 5;
Notebook.debug = 0;

Notebook.trace = function(str) {
	if (Notebook.debug) {
		alert(str);
	}
}	

Notebook.prototype.getExpires = function(days) {
	oneday = 1000*3600*24;
	s = 'expires=';
	d = new Date();
	expires = new Date(d.valueOf() + oneday*days);
	s += expires.toUTCString();
	return s;
}

Notebook.prototype.setCookie = function(value) {
	document.cookie = this.name + "=" + value + ";" + this.getExpires(Notebook.daysExp) + ";path=/;";
}

Notebook.prototype.save = function() {
	this.setCookie(this.serialize());
}

Notebook.prototype.load = function() {
	var cookie = Cookies(this.name);
	var str = cookie ? unescape(cookie) : '';
	this.records = new Array();
	
	if(str && str.length>0) {
		var records = str.split(':');
		for(var i=0;i<records.length;i++) {
			var record = unescape(records[i]);
			this.records.push(record.split('|'));
		}
	}
}

Notebook.prototype.moveFirst = function() {
	this.cursor = 0;
}

Notebook.prototype.moveNext = function() {
    if(!this.eof())
		this.cursor++;
}

Notebook.prototype.movePrev = function() {
	if(!this.bof()) 
		this.cursor--;
}

Notebook.prototype.moveLast = function() {
	this.cursor = this.records.length - 1;
}

Notebook.prototype.recordCount = function() {
	return this.records.length;
}

Notebook.prototype.fieldCount = function() {
    if(this.records.length>0) {
		return this.records[0].length;
	}
	else {
		return 0;
	}	
}

Notebook.prototype.item = function(idx) {
    if(!this.eof() && !this.bof()) {
		return this.records[this.cursor][idx];
	}
	else {
		return null;
	}	
}

Notebook.prototype.record = function() {
    if(!this.eof() && !this.bof()) {
		return this.records[this.cursor];
	}
	else {
		return null;
	}	
}

Notebook.prototype.eof = function() {
	return (this.cursor>=this.records.length);
}

Notebook.prototype.bof = function() {
	return (this.cursor<0);
}

Notebook.prototype.add = function(record) {
	this.records.push(record);
	this.save();
}

Notebook.prototype.addFields = function() {
    var record = new Array();
    for(var i=0;i<arguments.length;i++) {
    	record.push(arguments[i]);
    }

	this.records.push(record);
	this.save();
}

Notebook.prototype.remove = function(idx, value) {
	var result = new Array();
    for(var i=0;i<this.records.length;i++) {
    	if(this.records[i][idx] != value) {
			result.push(this.records[i]);
    	}
    }

	this.records = result;
    this.save();
}

Notebook.prototype.serialize = function() {
	var records = new Array();
	for(var i=0;i<this.records.length;i++) {
		var r = this.records[i];
		var record = new Array();
		for(var t=0;t<r.length;t++) {
			record.push(escape(r[t]));
		}
		records.push(record.join('|'));
	}

	var s = records.join(':');

	return s;
}

Notebook.prototype.find = function(idx, value, fromFirst) {
    if(fromFirst) {
		this.moveFirst();
	}

	var found = 0;
	while(!this.eof() || found) {
		if(this.item(idx) == value) {
			found = 1;
			break;
		}
		else {
			this.moveNext();
		}	
	}

	return found;
}

Notebook.prototype.clear = function() {
	this.records = new Array();
	this.save();
}
