Collective spin systems in dispersive optical cavity QED: Quantum phase transitions and entanglement