The mosque was first built in 705 AD. The first mosque was destroyed in an earthquake in 748 AD and had to be built again. We do not know when it was built again, but it was probably in 771 AD, and this mosque was destroyed soon after they finished building it. The third mosque was built around 780 AD. In 1033 AD there was another earthquake, and the mosque had to be built again.