/**
 * @author pfigueiredo
 */
KInput.prototype = new KWidget;
KInput.prototype.constructor = KInput;
function KInput(parent, id, type) {
	KWidget.call(this, parent);
	if ((this.parent) && (this.parent.layout)) {
		this.panel = this.parent.layout.createElement();
	}
	else {
		this.panel = document.createElement('div');
	}
	this.label = null;
	this.input = null;
	this.id = id;
	this.type = type;
	this.editMode = true;
}

KInput.prototype.draw = function() {
	var inputStyle = 'K' + this.type.toUpperCase().charAt(0) + this.type.substring(1);

	this.panel.className += ' KInputPanel';

	var labelContainer = document.createElement('div');
	labelContainer.className = 'KLabelContainer';
	this.label = document.createElement('label');
	this.label.setAttribute('for', this.id);
	this.label.className = 'KLabel ' + inputStyle + 'Label';
	labelContainer.appendChild(this.label);
	this.panel.appendChild(labelContainer);

	var inputContainer = document.createElement('div');
	inputContainer.className = 'KInputContainer ' + inputStyle + 'Container';
	if (/select|textarea/.test(this.type)) {
		this.input = document.createElement(this.type);
	}
	else {
		this.input = document.createElement('input');
		this.input.setAttribute('type', this.type);
	}
	this.input.setAttribute('id', this.id);
	this.input.className = 'KInput ' + inputStyle;
	KInput.prototype.addEvents.call(this);
	inputContainer.appendChild(this.input);
	this.panel.appendChild(inputContainer);
}

KInput.prototype.write = function(){
	KWidget.prototype.write.call(this);
	if (this.onLoad) {
		this.onLoad(null, this);
	}
}

KInput.prototype.setID = function(id) {
	this.id = id;
	this.input.setAttribute('id', this.id);
	this.label.setAttribute('for', this.id);
}

KInput.prototype.getLabel = function() {
	return this.label.innerHTML;
}

KInput.prototype.getValue = function() {
	return this.input.value;
}

KInput.prototype.setLabel = function(label) {
	this.label.innerHTML = label;
}

KInput.prototype.setValue = function(value) {
	this.input.setAttribute('value', value);
}

KInput.prototype.addEvents = function(){
	var thisID = this;

	this.input.onabort = function(e) {
		var input = null;
		if (!e) {
			var e = window.event;
		}
		if (e.target) {
			input = e.target;
		}
		else if (e.srcElement) {
			input = e.srcElement;
		}
		thisID.onAbort(e, input);
	}
	this.input.onblur = function(e) {
		var input = null;
		if (!e) {
			var e = window.event;
		}
		if (e.target) {
			input = e.target;
		}
		else if (e.srcElement) {
			input = e.srcElement;
		}
		thisID.onBlur(e, input);
	}
	this.input.onchange = function(e) {
		var input = null;
		if (!e) {
			var e = window.event;
		}
		if (e.target) {
			input = e.target;
		}
		else if (e.srcElement) {
			input = e.srcElement;
		}
		thisID.onChange(e, input);
	}
	this.input.onclick = function(e) {
		var input = null;
		if (!e) {
			var e = window.event;
		}
		if (e.target) {
			input = e.target;
		}
		else if (e.srcElement) {
			input = e.srcElement;
		}
		thisID.onClick(e, input);
	}
	this.input.ondblclick = function(e) {
		var input = null;
		if (!e) {
			var e = window.event;
		}
		if (e.target) {
			input = e.target;
		}
		else if (e.srcElement) {
			input = e.srcElement;
		}
		thisID.onDblClick(e, input);
	}
	this.input.onerror = function(e) {
		var input = null;
		if (!e) {
			var e = window.event;
		}
		if (e.target) {
			input = e.target;
		}
		else if (e.srcElement) {
			input = e.srcElement;
		}
		thisID.onError(e, input);
	}
	this.input.onfocus = function(e) {
		var input = null;
		if (!e) {
			var e = window.event;
		}
		if (e.target) {
			input = e.target;
		}
		else if (e.srcElement) {
			input = e.srcElement;
		}
		thisID.onFocus(e, input);
	}
	this.input.onkeydown = function(e) {
		var input = null;
		if (!e) {
			var e = window.event;
		}
		if (e.target) {
			input = e.target;
		}
		else if (e.srcElement) {
			input = e.srcElement;
		}
		thisID.onKeyDown(e, input);
	}
	this.input.onkeypress = function(e) {
		var input = null;
		if (!e) {
			var e = window.event;
		}
		if (e.target) {
			input = e.target;
		}
		else if (e.srcElement) {
			input = e.srcElement;
		}
		thisID.onKeyPress(e, input);
	}
	this.input.onkeyup = function(e) {
		var input = null;
		if (!e) {
			var e = window.event;
		}
		if (e.target) {
			input = e.target;
		}
		else if (e.srcElement) {
			input = e.srcElement;
		}
		thisID.onKeyUp(e, input);
	}
	this.input.onload = function(e) {
		var input = null;
		if (!e) {
			var e = window.event;
		}
		if (e.target) {
			input = e.target;
		}
		else if (e.srcElement) {
			input = e.srcElement;
		}
		thisID.onLoad(e, input);
	}
	this.input.onmousedown = function(e) {
		var input = null;
		if (!e) {
			var e = window.event;
		}
		if (e.target) {
			input = e.target;
		}
		else if (e.srcElement) {
			input = e.srcElement;
		}
		thisID.onMouseDown(e, input);
	}
	this.input.onmousemove = function(e) {
		var input = null;
		if (!e) {
			var e = window.event;
		}
		if (e.target) {
			input = e.target;
		}
		else if (e.srcElement) {
			input = e.srcElement;
		}
		thisID.onMouseMove(e, input);
	}
	this.input.onmouseout = function(e) {
		var input = null;
		if (!e) {
			var e = window.event;
		}
		if (e.target) {
			input = e.target;
		}
		else if (e.srcElement) {
			input = e.srcElement;
		}
		thisID.onMouseOut(e, input);
	}
	this.input.onmouseover = function(e) {
		var input = null;
		if (!e) {
			var e = window.event;
		}
		if (e.target) {
			input = e.target;
		}
		else if (e.srcElement) {
			input = e.srcElement;
		}
		thisID.onMouseOver(e, input);
	}
	this.input.onmouseup = function(e) {
		var input = null;
		if (!e) {
			var e = window.event;
		}
		if (e.target) {
			input = e.target;
		}
		else if (e.srcElement) {
			input = e.srcElement;
		}
		thisID.onMouseUp(e, input);
	}
	this.input.onreset = function(e) {
		var input = null;
		if (!e) {
			var e = window.event;
		}
		if (e.target) {
			input = e.target;
		}
		else if (e.srcElement) {
			input = e.srcElement;
		}
		thisID.onReset(e, input);
	}
	this.input.onresize = function(e) {
		var input = null;
		if (!e) {
			var e = window.event;
		}
		if (e.target) {
			input = e.target;
		}
		else if (e.srcElement) {
			input = e.srcElement;
		}
		thisID.onResize(e, input);
	}
	this.input.onselect = function(e) {
		var input = null;
		if (!e) {
			var e = window.event;
		}
		if (e.target) {
			input = e.target;
		}
		else if (e.srcElement) {
			input = e.srcElement;
		}
		thisID.onSelect(e, input);
	}
	this.input.onsubmit = function(e) {
		var input = null;
		if (!e) {
			var e = window.event;
		}
		if (e.target) {
			input = e.target;
		}
		else if (e.srcElement) {
			input = e.srcElement;
		}
		thisID.onSubmit(e, input);
	}
	this.input.onunload = function(e) {
		var input = null;
		if (!e) {
			var e = window.event;
		}
		if (e.target) {
			input = e.target;
		}
		else if (e.srcElement) {
			input = e.srcElement;
		}
		thisID.onUnload(e, input);
	}
}
