Ahmadinejad's visit produced little in terms of substantive policy; the signing of a multibillion-dollar natural gas pipeline deal was put off. But Ahmadinejad got something just as valuable: a chance to spin his own image, court popularity and bash the United States and Israel.

I've long been fond of the Blue Mosque because it is where, many years ago, I attended my first Friday prayers. Last Friday, though, I felt uncomfortable in the prayer hall, where I found myself in front of God but next to Ahmadinejad, who turned the ritual into a political show.

Departing from established practice of having visiting Muslim heads of state pray in a smaller mosque in Istanbul, the government allowed Ahmadinejad to pray in the Blue Mosque, Turkey's symbol of tolerant Ottoman Islam. With permission from Turkish authorities, he also allowed Iranian television to videotape him during the entire prayer, in violation of Islamic tradition, which requires quiet and intimate communion between God and the faithful. There was so much commotion around Ahmadinejad that the imam had to chide the congregants. Then, as he left the mosque, Ahmadinejad got out of his car to encourage a crowd of about 300 to chant, "Death to Israel! Death to America!"

Even without this behavior, any visit from a leader representing an authoritarian, anti-Western autocracy would have created controversy in Turkey just a few years ago. Not today. The ruling Justice and Development Party, or AKP, government not only opened the Blue Mosque to Ahmadinejad but accommodated his refusal to pay respects at the mausoleum of Kemal Ataturk, the founder of modern, secular Turkey -- a major violation of protocol for an official visit.

In 1996, when Iran's president, Hashemi Rafsanjani, refused to go to Ataturk's mausoleum, snubbing Turkey's identity as a secular pro-Western state, it led to a public outcry and sharp criticism of Iran. Relations soured. When the Iranian ambassador suggested a few months later that Turkey should follow Sharia law, he was forced to leave the country.

This time, though, the AKP government has taken a different stance, playing down the diplomatic insult. It moved the meeting from the capital, Ankara, to Istanbul and labeled it a "working" meeting rather than an official visit. Yet all sorts of AKP officials flocked to Istanbul to meet with the Iranian president.

Turkish Foreign Minister Ali Babacan asked the Turkish public to ignore the snub and instead "focus on the big picture." It is the "big picture," though, that is most disconcerting. By extending an invitation to Ahmadinejad, the first such move by any NATO or European Union member country, Turkey has broken ranks with the West. The West can no longer take Turkey for granted as a staunch ally against Tehran.

In the past, Turkey stood with the West, especially after the 1979 Islamist revolution in Iran. Also, Tehran gave refuge to the Kurdistan Workers Party, or PKK, which carried out terror attacks in Turkey from bases in Iran. Since the Iraq war began, however, Iran has shifted tactics to win Turkey's heart. While the U.S. delayed taking action, Iran actually bombed PKK camps in northern Iraq.

Meanwhile, since the AKP assumed power in Turkey in 2002, bilateral visits with Iran have boomed; Ahmadinejad's trip crowns dozens of visits by high-level officials. Trade has boomed as well, increasing from $1.2 billion in 2002 to $8 billion today. And even though the two countries didn't formalize the deal last week, plans are still going forward for a $3.5-billion Turkish investment in Iranian gas fields -- this at a time when the West is adopting financial sanctions against Iran to cripple Tehran's ability to make a nuclear bomb. If there were any doubts about a Turkish-Iranian rapprochement, they were laid to rest last week: During Ahmadinejad's visit, the two countries agreed to make 2009 an "Iran-Turkey year of culture" -- marked by regular cultural and political programs and exchanges -- to bring the two countries closer.

Ahmadinejad's visit also speaks volumes about the future of Turkish-U.S. ties regarding Iran. According to a recent opinion poll in Turkey, when asked what the country should do in the event of a U.S. attack against Iran, only 4% of respondents said Turkey should support the U.S., while 33% wanted to back Iran and 63% chose neutrality.

As I shared the canopy of the Blue Mosque's divine dome with Ahmadinejad, I could not help but ponder how far Turkish foreign policy has shifted since 2002. Before, Turkey picked allies based on shared values -- democracy, Western identity, secular politics and the principle of open society -- that appeared to reflect the Turkish soul. Iran has not become a pro-Western, secular democracy since 1996, nor have Tehran's mullahs accepted gender equality or the idea of a free society. Yet Ankara has had a change of heart toward Tehran. Years from now, Ahmadinejad's visit to Istanbul will be remembered as the tipping point at which the West lost Turkey, and Turkey lost its soul.