/*******************************
*
* elcRMap.js
* 
* rollover image class
*
* Author: DoublePrime
* Version: 0.1
*
* Comments
* A structure implementing a recursive hash map
* A key into the top level map may reference either
* a general object or a nested recursive map
*
* Usage:
* var myRMap = new elcRMap();
* myRMap.store( 'key1.key2.key3.key4','some value');
* creates nested maps with keys key1 .. key3 and 
* associates the key value pair key4/some value in the
* recursive map associated with key3.
*
* var str = myRMap.fetch('key1.key2.key3.key4');
* should return 'some value'
*
* myRMap.clear()
* empties the recursive map recursively
*
*******************************/

function elcRMap(){
	this.Items = new Array();
}

elcRMap.prototype.store = function(sKey,oValue){
	var sPrefix, sSubKey;
	var entry;
	if(sKey.indexOf(".") != -1){
		sPrefix = sKey.substring(0,sKey.indexOf("."));
		sSubKey = sKey.substring((sKey.indexOf(".") + 1), sKey.length);
	}else{
		sPrefix = sKey;
		sSubKey = '';
	}

	if(sSubKey == ''){
		// reached terminus, add the value (object) to this Map
		entry = new String(sPrefix);
		entry.obj = oValue;
		this.Items[this.Items.length] = entry;
		return;
	}else{
		// check to see if we've already seen this key and have an rMap for it
		for(var i=0; i<this.Items.length; i++){
			if(this.Items[i].toString() == sPrefix){
				if(typeof this.Items[i].obj == "object" && this.Items[i].obj.constructor == elcRMap){
					this.Items[i].obj.store(sSubKey,oValue);
				}else{
					//override existing data: sorry :-<
					this.Items[i].obj = new elcRMap();
					this.Items[i].obj.store(sSubKey,oValue);
				}
				return;
			}
		}
		// sPrefix was not in this hash, add a new recursive map
		entry = new String(sPrefix);
		entry.obj = new elcRMap();
		this.Items[this.Items.length] = entry;
		entry.obj.store(sSubKey,oValue);
	}
}

elcRMap.prototype.fetch = function(sKey){
	var sPrefix, sSubKey;
	if(sKey.indexOf(".") != -1){
		sPrefix = sKey.substring(0,sKey.indexOf("."));
		sSubKey = sKey.substring((sKey.indexOf(".") + 1), sKey.length);
	}else{
		sPrefix = sKey;
		sSubKey = '';
	}
	if (sSubKey == ''){
		for (var i=0; i<this.Items.length;i++){
			if (this.Items[i].toString() == sPrefix){
				return this.Items[i].obj;
			}
		}
	}else{
		for (var i=0; i<this.Items.length;i++){
			if (this.Items[i].toString() == sPrefix){
				if(typeof this.Items[i].obj == "object" && this.Items[i].obj.constructor == elcRMap){
					return this.Items[i].obj.fetch(sSubKey);
				}else{
					// should never be reached
					alert('Error: expecting a recursive map but couldn\'t find one');
				}
			}
		}
	}
	return null;
}

elcRMap.prototype.exists = function(sKey){
	var sPrefix, sSubKey;
	if(sKey.indexOf(".") != -1){
		sPrefix = sKey.substring(0,sKey.indexOf("."));
		sSubKey = sKey.substring((sKey.indexOf(".") + 1), sKey.length);
	}else{
		sPrefix = sKey;
		sSubKey = '';
	}
	if (sSubKey == ''){
		for (var i=0; i<this.Items.length;i++){
			if (this.Items[i].toString() == sPrefix){
				return true;
			}
		}
	}else{
		for (var i=0; i<this.Items.length;i++){
			if (this.Items[i].toString() == sPrefix){
				if(typeof this.Items[i].obj == "object" && this.Items[i].obj.constructor == elcRMap){
					return true;
				}else{
					// should never be reached
					alert('Error: expecting a recursive map but couldn\'t find one');
				}
			}
		}
	}
	return false;
}

elcRMap.prototype.clear = function(){
	for (var i=0; i<this.Items.length;i++){
		if(typeof this.Items[i].obj == "object" && this.Items[i].obj.constructor == elcRMap){
			this.Items[i].obj.clear();
		}
	}
	this.Items.length=0;
}

elcRMap.prototype.remove = function(sKey){
	alert("hash remove not yet implemented");
}

		
