A Stochastic Model of the Ryanodine Receptor Featuring Coupled Gating and Competitive Binding of Luminal and Cytosolic Ca2+ and Mg2+