/**
 *	What's the point of this, you may ask!? Well, it will make life easier once
 *	COM, plug-in and XPCOM versions (for IE, NS4 and NS6 respectively) are
 *	being used.
 *
 *	@param element the id of the ActiveX object or plug-in
 */
function Joystick(element) {
	this.control = document[element];
}

Joystick.CENTRE = 32768;

Joystick.prototype.getDeviceCount = Joystick_getDeviceCount;
Joystick.prototype.clickToPick = Joystick_clickToPick;

Joystick.prototype.poll = Joystick_poll;
Joystick.prototype.getX = Joystick_getX;
Joystick.prototype.getY = Joystick_getY;
Joystick.prototype.getButtons = Joystick_getButtons;

/**
 *	Cycles through the available controllers, leaving the device that
 *	performed the click as the current joystick for this instance.
 *
 *	@return true if a button was clicked
 */
function Joystick_clickToPick() {
	if (this.control) {
		for (var n = 0; n < 16; n++) {
			if (this.control.setDevice(n)
				&& this.control.buttons != 0) {
				window.status = "device: " + n;
				return true;
			}
		}
	}
	return false;
}

/**
 *	Returns the number of available game devices.
 */
function Joystick_getDeviceCount() {
	var numDevs = 0;
	if (this.control) {
		// so we can restore the current device
		var oldDevice = this.control.getDevice();
		for (var n = 0; n < 16; n++) {
			if (this.control.setDevice(n)) {
				numDevs++
			}
		}
		this.control.setDevice(oldDevice);
	}
	return numDevs;
}

/*
 *	The getXXX() methods are more useful to Java than JavaScript.
 */

function Joystick_poll() {
	this.control.poll();
}

function Joystick_getX() {
	return this.control.x;
}

function Joystick_getY() {
	return this.control.y;
}

function Joystick_getButtons() {
	return this.control.buttons;
}

