Decoherence and entanglement in tripartite systems: Cavity QED