/****************************************
 * Usage: new InputMod(object to modify);
 * 
 * DOMchildren of this object have class
 * extra class at end = edit-*
 * includes title, area, date, tags
 * 
 * tags uses tagmaker object
 * date uses datemaker object
 * area uses supereditor
 * title uses text field
 * 
 * id becomes the data's id except for num
 *****************************************/


function InputMod( obj ) {
	this.obj = obj;
	this.inputObjs = Array();
	this.origObjs = Array();
	
	this.init = function() {
		this.tagList = this.obj.getElementsByTagName("*");
		for (var i = 0; i < this.tagList.length; i++) {
			var tagClass = this.tagList[i].className;
			if (tagClass.indexOf("edit-") != -1)
				this.makeInput(this.tagList[i]);
		}
	}
	
	this.makeInput = function ( tagObj ) {
		var options = tagObj.className.split(" ");
		for (i in options) {
			if (options[i].substr(0,5) == "edit-") {
				var type = options[i].substr(5);
				
				switch(type) {
					case "Area": this.makeEditor(tagObj); break;
					case "Date": tagObj.innerHTML = "Now"; this.makeTextField(tagObj, type); break;
					case "Tags": this.makeTextField(tagObj, type); break;
					case "Hidden": this.makeHidden(tagObj); break;
					default: this.makeTextField(tagObj, type); break;
				}
			}
		}
	}
	this.makeHidden = function (obj) {
		var datID = (obj.id.indexOf("-") != -1)?obj.id.substr(0,obj.id.indexOf("-")) : obj.id;
		var inputElement = document.createElement("input");
		inputElement.type = "hidden";
		inputElement.value = obj.innerHTML;
		
		this.inputObjs[datID] = inputElement;
		obj.parentNode.replaceChild(inputElement, obj);
	}
	this.makeTextField = function (obj, title) {
		var content = obj.innerHTML;
		var className = obj.className;
		var inputElement = document.createElement("input");
		inputElement.type = "text";
		if (content.replace(/^\s+|\s+$/g, '') == "") { //If content is empty
			inputElement.value = title;
			inputElement.className = className + " grayTitle";
			inputElement.onclick = function () {
				inputElement.className = className;
				if (inputElement.value == "")
					inputElement.value = "";
			}
			inputElement.onblur = function() {
				if (inputElement.value == "") {
					inputElement.value = title;
					inputElement.className = className + " grayTitle";
				}
			}
		} else {
			inputElement.value = content;
			inputElement.className = className;
		}
		var datID = (obj.id.indexOf("-") != -1)?obj.id.substr(0,obj.id.indexOf("-")) : obj.id;
		this.inputObjs[datID] = inputElement;
		this.origObjs[datID] = obj;
		obj.parentNode.replaceChild(inputElement, obj);
	}
	
	this.makeEditor = function ( obj ) {
		var datID = (obj.id.indexOf("-") != -1)?obj.id.substr(0,obj.id.indexOf("-")) : obj.id;
		this.inputObjs[datID] = new SuperEditor(obj, "Verdana", 10);
	}
	
	this.getData = function () {
		var data = Array();
		for (i in this.inputObjs) {
			if (typeof(this.inputObjs[i].getContent) == "function") {
				data[i] = this.inputObjs[i].getContent();
			} else {
				data[i] = this.inputObjs[i].value;
			}
		}
		return data;
	}
	
	this.clear = function () {
		var data = this.getData();
		for (i in data) {
			if (typeof(this.inputObjs[i].getContent) == "function") {
				this.inputObjs[i].clear();
			} else if (typeof(this.origObjs[i]) != "undefined") {
				this.origObjs[i].innerHTML = data[i];
				this.inputObjs[i].parentNode.replaceChild(this.origObjs[i], this.inputObjs[i]);
			}
		}
	}
	
	this.init();
}

