What you are describing sounds like a classic loose or failing electrical connection.
I'd check out the wires going to components on the engine like the MAF, IAC, O2s, ignition module, battery and the spark plug wires etc. As you check the connections pull the plugs appart and look at the pins and sockets, its possible that a one has broken or there's corrosion.
If you have access to one hook up a code scanner and see if the computer has any codes, since they might point you to the problem.