/* 	
	------------------------------------------------------------------------------
	/framework/classes/js/button.js
	
	Een button
	
  ------------------------------------------------------------------------------
*/

function Button(_id, _value) {

	// standard properties
	this.id = _id;	
	this.value = _value;
	
	this.formElementId = "F_" + this.id;
	this.formElement = document.getElementById(this.formElementId);
	this.parentElement = null;
	
	// appearance
	this.width = 0;
	this.type = "button";
	this.style = "";
	this.className = "";
	
	// events
	this.onclick = "";
	this.onkeyup = "";
	
	// extra properties
	this.error = "";
	this.tag = "";
	this.enabled = true;
	
	// methods
	this.registerEvents = Button_registerEvents;
	this.addEvent = Button_addEvent;
	this.getEventName = Button_getEventName;
	
	this.getError = Button_getError;
	this.setError = Button_setError;
	
	this.showBusy = Button_showBusy;
	this.hideBusy = Button_hideBusy;
	
	this.show = Button_show;
	this.hide = Button_hide;
	
	
	this.getType = Button_getType;
	this.getValue = Button_getValue;
	
	this.render = Button_render;
	this.destroy = Button_destroy;

	// extra events
	this.beforeEvent = Button_beforeEvent;
	this.afterEvent = Button_afterEvent;
	
	this.focus = Button_focus;
	// effects
	this.highlight = Button_highlight;
	this.setEnabled = Button_setEnabled;
	this.setDisabled = Button_setDisabled;
	
	// cssclass zetten
	this.setCssClass = Button_setCssClass;
}

function Button_setCssClass(newClass) {
	this.formElement.className = newClass;
}


function Button_setEnabled() {
	debug.write("Setting " + this.id + " to enabled");
	this.formElement.disabled = false;
	this.enabled = true;
}

function Button_setDisabled() {
	debug.write("Setting " + this.id + " to disabled");
	this.formElement.disabled = true;
	this.enabled = false;
}


function Button_focus() {
	if (this.formElement) {
		this.formElement.focus();	
	}
	
}

function Button_highlight() {
	new Effect.Highlight(this.formElementId);	
}

function Button_beforeEvent(ev) {
	//this.highlight();
}

function Button_afterEvent(ev) {
}

function Button_destroy() {
	debug.write("Destroyin' myself: " + this.id);
	
	if (this.formElement) {
		if (this.formElement.parentNode) {
			debug.write("really destroying");
			this.formElement.parentNode.removeChild(this.formElement);
		}
	}
	this.formElement = null;
}

function Button_render() {
	debug.write("rendering " + this.id + " in " + this.parentElement + " with value " + this.value + " and tag " + this.tag);
	
	
	// link button
	if (this.type == "text") {
		//debug.write("Creating normal element");
		this.formElement = document.createElement("a");
		this.formElement.id = "F_" + this.id;
		/*if ((document.all) && (!window.opera))
			this.formElement.style.styleFloat = "left";
		else
			this.formElement.style.cssFloat = "left";
		*/
		
		if (this.width != "") 
			this.formElement.style.width = this.width + "px";
		
		this.formElement.style.display = "none";
			
		this.formElement.style.cursor = "pointer";
		if (this.className != "") 
			this.formElement.className = this.className;	
		else
			this.formElement.className = "fw_button";
		//this.formElement.setAttribute("autocomplete","off");
		
		//this.formElement.type = this.type;
		this.formElement.innerHTML = this.value + " ";
	}
	else if (this.type == "image") {
		this.formElement = document.createElement("img");
		this.formElementId = "F_" + this.id;
		this.formElement.id = this.formElementId;
		this.formElement.src = this.image;
		if (this.className != "") 
			this.formElement.className = this.className;	
		this.formElement.style.display = "none";
		if (isDefined(this.value)) {
			this.formElement.innerHTML = this.value;
		}
	}
	else {
		// gewone button
		this.formElement = document.createElement("button");
		//this.formElement.type = "button"; // werkt niet in IE
		this.formElementId = "F_" + this.id;
		this.formElement.id = this.formElementId;
		if (this.width != "") 
			this.formElement.style.width = this.width + "px";
			
		this.formElement.style.display = "none";
		this.formElement.className = "fw_button";
		if (isDefined(this.value)) {
			this.formElement.innerHTML = this.value;
		}
		
	}
	
	//alert(this.parentElement + " - " + this.formElement);
	this.parentElement.appendChild(this.formElement);
	
	this.registerEvents();
}



function Button_getType() {
	return this.type;
}

function Button_getValue() {
	return this.value;
}



function Button_hide() {
	if (this.formElement) {
		this.visible = false;
		this.formElement.style.display = "none";
	}
}

function Button_show() {
	if (this.formElement) {
		this.visible = true;
		// Tijdelijke edit door Maarten, moet nog getest worden! 
		//this.formElement.style.display = "block";
		this.formElement.style.display = "";
	}
}

function Button_showBusy() {
	if (this.busyElement) {
		// nothing to be done
	}
	else {
		// create busyElement
		this.busyElement = document.createElement("img");
		this.busyElement.className = "fw_busyelement";
		this.busyElement.style.position = "absolute";
		this.busyElement.src = layoutroot + "/img/fw_icon_busy.gif";
		// plaats bepalen
		
		x = findPosX(this.formElement) + this.formElement.offsetWidth + 3;
		y = findPosY(this.formElement) + (this.formElement.offsetHeight/2) - 1; 
		try {
			this.busyElement.style.top = y + "px";
			this.busyElement.style.left = x + "px";
		}
		catch(err) {
			this.busyElement.style.top = y;
			this.busyElement.style.left = x;
		}
		this.busyElement.style.zIndex = "105";
		document.body.appendChild(this.busyElement);
	}
	
	this.busyElement.style.display = "block";
}
function Button_hideBusy() {
	if (this.busyElement) {
		this.busyElement.style.display = "none";
	}
	
}
function Button_getError() {
	return this.error;	
}

function Button_setError(msg) {
	this.error = msg;
	
	/*if (this.errorelement) {
		this.errorelement.alt = this.error;
		this.errorelement.title = this.error;
		
		if (msg.length > 0) {
			this.errorelement.style.display = "block";
		}
		else {
			this.errorelement.style.display = "none";
		}
	}*/
}



function Button_getEventName(whichevent) {
	//alert("eval dit " + eval("this." + whichevent));
	return (eval("this.on" + whichevent));
	
}

function Button_registerEvents() {
	if (this.formElement) {
		if (this.onclick.length > 0) {
			this.addEvent('click');
		}
	}
	
}
	
function Button_addEvent(eventtype) {
	
	//W3C
	if(this.formElement.addEventListener) {
		//alert('W3C event added');
		this.formElement.addEventListener(eventtype, handle_event, false);
	}

	//Microsoft
	else if(this.formElement.attachEvent){
		//alert("MS event added");
		this.formElement.attachEvent("on" + eventtype, handle_event);
	}
}



