function HJSTemplateRegistry(){
	// constructors
	this.registry = [];

	this.newID = function(){
		d = new Date();
		var uniquestring = "HJS"+d.getUTCHours()+d.getUTCMinutes()+d.getUTCSeconds()+d.getUTCMilliseconds();
		return (uniquestring);
		
	}
	
	// just a ghetto sanity check - deprecated
	this.isValid = function(){
		return true;
		if (this.templateID.length != this.templateCode.length){
			alert("HJSTemplateRegistry mismatch! Please check your code");
			return false;
		} else {return true}
	}
	
	this.register = function(id){
		if (this.isValid){
			var temptext = $("[template='"+id+"']").html();
			// TODO: this may not be necessary, I don't know, but we need to write up a better regexp
			// i hate regexp
			temptext = temptext.replace(/<!--/,"");
			if (temptext == null){
				alert("DEV ERROR: template code needs to be commented out!");
			}
			temptext = temptext.replace(/-->/,"");
			
			var elementArray = $("[template='"+id+"']").get();
//			var attr = document.createAttribute("htmplHASHID");
			for(var i = 0; i < elementArray.length; i++){
				if (!elementArray[i].id){
					elementArray[i].id = this.newID();
				}
//				attr.value = "HASHID"+this.newID();
//				elementArray[i].setAttributeNode(attr);
			}
			
			var datagram = {
				"templateID" : id,
				"code" : temptext,
//				"htmplHASHID" : attr,
				"elementArray" : elementArray
			}
			this.registry.push(datagram);
			
		}
	}
	this.rawregister = function(id,html){
		if (!id && !html){
			this.templateID.push(id);
			this.templateCode.push(html);
		}
	}
	this.loopregister = function(loopname,id){
		if (this.isValid){
			var temptext = $("[templateloop='"+loopname+"'] > [template='"+id+"']").html();
			// alert(temptext);
			// TODO: this may not be necessary, I don't know, but we need to write up a better regexp
			// i hate regexp
			temptext = temptext.replace(/<!--/,"");
			if (temptext == null){
				alert("DEV ERROR: LOOPREGISTER - template code needs to be commented out!");
			}
			temptext = temptext.replace(/-->/,"");

			var elementArray = $("[templateloop='"+loopname+"']").get();
			for(var i = 0; i < elementArray.length; i++){
				if (!elementArray[i].id){
					elementArray[i].id = this.newID();
				}
			}

			var datagram = {
				"templateID" : loopname+id,
				"code" : temptext,
				"elementArray" : elementArray
			}
			this.registry.push(datagram);
		}
	}
	/* 
	 * this returns the registry index, used to get template code, element ID
	 */
	this.getRegistryIndexbyTemplateID = function(id){
		for(var i = 0; i < this.registry.length; i++){
			if (this.registry[i].templateID == id){
				return(i);
			}			
		}
	}
	this.getelementArraybyTemplateID = function(id){
		return(this.registry[this.getRegistryIndexbyTemplateID(id)].elementArray);
	}
	this.getelementArraybyRegistryIndex = function(index){
		return(this.registry[index].elementArray);
	}

	this.getCodebyRegistryIndex = function(index){
		return(this.registry[index].code);
	}
	
	this.getCodebyTemplateID = function(id){
		return(this.registry[this.getRegistryIndexbyTemplateID(id)].code);
	}
	this.getElementIDbyTemplateID = function(id){//NOTE: ghetto copy of above to get element reference
		return(this.registry[this.getRegistryIndexbyTemplateID(id)].elementid);
	}

	// TODO: not being used atm
	this.initTemplate = function(id){
		var code = this.getCodebyTemplateID(id);
		alert(code);
	}

	this.clear = function(id){
		$("[template='"+id+"']").empty();	
		$("[templatedraw='"+id+"']").empty();
		$("[templateloop='"+id+"']").empty();
	}
	this.clearloop = function(id){
		$("[templateloop='"+id+"']").empty();
	}
	this.clearlocation = function(id){
		$("[templatelocation='"+id+"']").empty();
	}

	// the brain 
	this.draw = function(json){
		var id = json.template;
		var length = json.variable_names.length;
		var registryIndex = this.getRegistryIndexbyTemplateID(id);
		var listoelements = this.getelementArraybyRegistryIndex(registryIndex);
		var tempcode = this.getCodebyRegistryIndex(registryIndex);
		//alert("drawing to:"+delement.id);
		for (var di = 0; di < length; di++){
			var reg = new RegExp(json.variable_names[di]);
			while (reg.test(tempcode)) {
				//alert(tempcode);
				tempcode = tempcode.replace(json.variable_names[di], json.data[di]);
			}
		}
		for(var i = 0; i < listoelements.length; i++){
			listoelements[i].innerHTML = tempcode;
		}
		return tempcode;
	}
	this.drawwide = function(json){
		var html = this.draw(json);
		var id = json.template;
		$("[templatedraw='"+id+"']").html(html);
	}
	// similar to draw, but using a specified attribute "templatelocation='123123'"
	this.drawlocation = function(json,location){
		var id = json.template;
		var length = json.variable_names.length;
		var tempcode = this.getCodebyTemplateID(id);
		for (i = 0; i < length; i++){
			var reg = new RegExp(json.variable_names[i]);
			while (reg.test(tempcode)){
//				alert(tempcode);
				tempcode = tempcode.replace(json.variable_names[i],json.data[i]);
			}
		}
		$("[templatelocation='"+location+"']").html(tempcode);
	}
	// a loop equivalent
	this.drawloop = function(json){
		var id = json.templateloop;
		var registryIndex = this.getRegistryIndexbyTemplateID(json.templateloop+json.loop[0].template_name);
		var listoelements = this.getelementArraybyRegistryIndex(registryIndex);
		listoelements[0].innerHTML = "";
		var looplength = json.loop.length;
		var html = "";
		for (var n = 0; n < looplength; n++){
			var tempcode = this.getCodebyTemplateID(json.templateloop+json.loop[n].template_name);
			for (var l = 0; l < json.loop[n].variable_names.length; l++){
				var reg = new RegExp(json.loop[n].variable_names[l]);
				while (reg.test(tempcode)) {
					tempcode = tempcode.replace(json.loop[n].variable_names[l], json.loop[n].data[l]);
				}
			}
			html += tempcode; 
		}
		for(var i = 0; i < listoelements.length; i++){
			listoelements[i].innerHTML = html;
		}
//		listoelements[0].innerHTML = html;
		return html;
	}

	this.drawloopwide = function(json){
		$("[templateloop='"+id+"']").html(this.drawloop(html));
	}
}

