Originaly built by Umayyad Abd-ur-Rahman in 756 AD as part of the adjacent mosque, the Patio de los Naranjos in Córdoba, Spain, is composed of a fountain and a grid of orange trees. Rainwater from the roof drains into at the cistern below the plaza, supplying water to the fountain in the patio and then irrigating the orange trees through 14 long distribution chains. A wooden block is used to control the flow of water into each channel until the sufficient amount of water needed to irrigate each tree has been delivered. Excess water then returns to the cistern.
Read more here