Calculating direction of acceleration from MEMS sensor

Hi all,

I am working on an embedded software project using a 3 axis MEMS accelerometer. The accelerometer outputs an X, Y and Z value in mG's. So when level the output is X=0, Y=0, Z=1000, on its side the output is X=0, Y=1000, Z=0 and on its end the output is X=1000, y=0, Z=0. ie showing gravity. The outputs can be positive or negative.

I want to be able to measure acceleration ignoring the effect of gravity. So for example if you held the device on a piece of string and spun it around parallel to the floor it would only show acceleration in the X and Y directions and nothing on the Z axis.

This sounds reasonably simple... just ignore the Z reading. But what if you spun it around parallel to the wall?

You can measure the total acceleration by A= sqrt(X^2 + Y^2 + Z^2) but that includes the effect of gravity and does not give the direction. It is a while since I studied maths so any help would be greatly appreciated.