// Javascript class that handles events to simulate a button
function button(target, backgroundElement, hoverFill, mouseDownFill, clickHandler) {
	this.target = target;
	this.backgroundElement = backgroundElement;
	this.hoverFill = hoverFill;
	this.mouseDownFill = mouseDownFill;
	this.clickHandler = clickHandler;
	
	// Register eventhandlers
	this.setCallback(this.target, "mouseEnter", this.handleMouseEnter);
	this.setCallback(this.target, "mouseLeave", this.handleMouseLeave);
	this.setCallback(this.target, "mouseLeftButtonUp", this.handleMouseUp);
    this.setCallback(this.target, "mouseLeftButtonDown", this.handleMouseDown);
}

button.prototype.handleMouseEnter = function(sender, eventArgs) {
    this.oldFill = this.backgroundElement.fill;
    this.backgroundElement.fill = this.hoverFill;
}

button.prototype.handleMouseLeave = function(sender, eventArgs) {
    this.backgroundElement.fill = this.oldFill;
}

button.prototype.handleMouseUp = function(sender, eventArgs) {
	this.backgroundElement.fill = this.hoverFill;
	
	if (this.clickHandler) {
	    this.clickHandler(sender, eventArgs);
	}
}

button.prototype.handleMouseDown = function(sender, eventArgs) {
    this.backgroundElement.fill = this.mouseDownFill;
}

button.prototype.setCallback = function(target, eventName, callback) {
	if (!window.methodIndex)
		window.methodIndex = 0;
	
	var callbackName = "uniqueCallback" + (window.methodIndex++);
	var controller = this;
	var func = function() {
		callback.apply(controller, arguments);
	}
	
	eval(callbackName + " = func;");
	target[eventName] = "javascript:" + callbackName;
}
