
/*
 * Copyright (C) 2010/2011 Im-At-Home BV
 * All Rights Reserved
 * No copying, duplication or replication of code permitted without
 * express permission of Im-At-Home BV
 * 
 */

Hashtable = function(retNoString) {
	if ((retNoString==null) || (retNoString=='undefined')) retNoString = false;
	this.init(retNoString);
};
		
$.extend(Hashtable.prototype, {	
	array : new Array(),
	retString : false,
	__keys : null,
	init : function(retNoString) {
		this.retString = retNoString;
		this.clear();
	},	
	count : function() {
		return this.array.length;
	},
	clear : function() {
		this.array = null; // force garbage collection...
		this.array = new Array();
		this.__keys = null;		
	},		
	indexof : function(key) {
		return this.keys().indexOf(key);
	},	
	itemat : function(index) {
		if ((index >= 0) && (index < this.array.length)) {
			return this.array[index].value;
		}
		return null;
	},
	rawat : function(index) {
		if ((index >= 0) && (index < this.array.length)) {
			return this.array[index];
		}
		return null;		
	},
	add : function(key, value) {
		var index = this.indexof(key);
		if (index==-1) {
			this.array.push({'key':key,'value':value});
			this.__keys = null;
		} else {
			this.array[index].value = value;
		}
		return true;
	},
	get : function(key, def) {
		var keys = this.keys();
		if ((keys != null) && (keys.length>0)) { 
			index = keys.indexOf(key);
			if (index != -1) return this.array[index].value;			
		}
		if ((def != 'undefined') && (def != null) && (def != '')) return def;		
		return (this.retString) ? '??'+key+'??' : null;
	},
	remove : function(key) {
		index = this.indexof(key);
		if (index != -1) {
			this.array.splice(index,1);
			this.__keys = null;			
			return true;
		}
		return false;
	},
	keys : function() {
		if (this.__keys != null) return this.__keys;
		var out = new Array();
		var arraysize = this.array.length;
		if (arraysize==0) return out;
		var counter = 0;
		do {
			out.push(this.array[counter].key);
		} while (++counter < arraysize);
		this.__keys = out;
		return this.__keys;
	},
	list : function() {
		return this.array;
	}			
});

