On Sunday, Turkey will head to the polls in parliamentary elections. Although issues such as the economy, a new constitution, and the conflict in the Kurdish-majority southeast have featured most prominently in the campaigning, Turkey's foreign policy has emerged as a central rallying point for the Justice and Development Party (AKP). But the populist streak that has given a boost to the party's support over the past years has also had consequences for relations with longtime allies. In distancing itself from the United States, the European Union, and Israel, the Turkish government has done considerable damage to its relations with the West.

This may be changing, however: Amid the political turmoil sweeping the Middle East, there are signs that the populist and anti-Western strand in Turkey's foreign policy may have run its course. Once election season is over, Turkey is likely to rediscover the importance of engaging with the United States and the European Union.

Well into the 1990s, Turkey did not really have a foreign policy. Instead, it had an orientation, or what analysts have in mind when they speak of "Turkey's turn from the West" or "Turkey's shift eastward." With the country's powerful military running the show, the lines were fixed. Turkey was a stalwart NATO ally, a Western outpost, a country happily divorced from its Ottoman past and, by extension, from the entire Arab world. It was also a country removed from its citizens. Aside from cases when public opinion had to be mobilized -- renewed fighting in Cyprus, say, or tensions with Greece -- foreign policy was rarely up for open discussion.

Over the past decade, however, the government of Prime Minister Recep Tayyip Erdogan has considerably trimmed the military's influence over political life, foreign policy included. To meet EU accession criteria, Erdogan's AKP stripped the army of its majority on the National Security Council, for years the most important body in charge of foreign policy. Building on the work of its predecessors, the AKP government replaced a foreign policy based on security with one focused on engagement, soft power, and trade, in the process diffusing tensions with neighboring countries such as Iran, Iraq, and Syria (known as the "zero problems" policy).

Other changes have come from the bottom up. As the domestic political arena expanded and democratization took root -- thanks in no small part to the EU accession process -- the media, business interests, NGOs, religious groups, and other parts of civil society have taken on a much more visible stand on issues that had traditionally been the remit of state elites and the military establishment. Civil society groups, for example, pushed aside traditional concerns of national security to pave the way for Turkey's reconciliation with Greece in the late 1990s and helped shift the domestic debate on Greek Cyprus in 2003 and 2004.

Never before in Turkey's modern history has foreign policy been so directly wedded to domestic politics: The architects of Turkey's foreign policy used to answer to the generals; these days, policymakers answer to the public. And never before has a Turkish government staked so much of its reputation on its international accomplishments, real or hypothetical.

On the campaign trail, the AKP's senior members have consistently championed the party's foreign outlook. At a speech in April, Foreign Minister Ahmet Davutoglu said, "Our ideal is to make this country a pioneer in the world, like our predecessors who carried out their goal of order for the whole world." Erdogan has made similarly proud and sweeping rhetorical gestures, telling supporters last month that "those who want democracy, those who want freedom, those who want to be rid of tyranny, oppression, and exploitation, now look to Turkey." A few days later, he added, "Now it is Turkey that sets the agenda. It's Turkey's word that everyone awaits."

The AKP, expected to win Sunday's vote by a landslide, has reason to trumpet its foreign policy accomplishments. Under the "zero problems" policy, Turkey's relations with its Middle Eastern neighbors are better than at any time since the founding of the republic. Regional trade is booming: Turkey's exports to the Middle East more than doubled between 2002 and 2010 as a share of total exports, now reaching 20 percent. (The share of exports to Europe has dropped over the same period by about 10 percentage points, to about 45 percent.) Turkish diplomats, meanwhile, have taken on a more active role in the region. Some Turkish initiatives have been successful (the effort to free four New York Times journalists captured in Libya); some were said to have almost succeeded (the 2008 talks between Syria and Israel); some have been dismissed by allies (including the proposed nuclear swap deal with Iran in 2011); and some have been stillborn (a road map for Libya that failed to call for Muammar al-Qaddafi's departure). But all have been universally seen as evidence of Turkey's growing clout and ambitions.

As Ziya Önis, a professor at Koç University, told me, the government "has used its assertive foreign policy and its popularity in the Arab world" to build popular support. Indeed, 65 percent of those Turks responding to a recent poll by the Turkish think tank TESEV back the AKP's foreign policy; around 80 percent of those surveyed said that they believe that Turkey can be a model -- cultural, political, and economic -- for the countries of the Middle East.

Yet the populism inherent in the AKP's foreign policy has a hidden danger: Fuelling anti-Western and anti-Israeli sentiment may win Erdogan a few nationalist or Islamist votes, but it is also costing him some valuable friends, from European politicians to the U.S. congressional representatives.

The infamous showdown between Erdogan and Israeli President Shimon Peres at the 2009 World Economic Forum in Davos is a case in point. Erdogan accused Peres of "knowing well how to kill," and then stormed off the stage and boarded a plane to Istanbul. He returned to a hero's welcome. The highly publicized spat earned the AKP a badly needed boost ahead of local elections in March 2009. Erdogan's anti-Israeli rants reached new heights last May, when Israeli troops stormed the Mavi Marmara, a Turkish ship carrying aid to Gaza, and killed eight Turkish activists and one U.S. citizen of Turkish origin. The West's initial sympathy for Turkey quickly melted away when Erdogan accused Israeli of "state terror," refused to label Hamas a terrorist organization, and claimed that the world "now perceived the swastika and the Star of David together."

The anti-Israeli rhetoric has certainly damaged Turkey's profile in Washington. In March 2010, Robert Wexler, the former chairman of Congress' Turkey caucus, noted that Erdogan's "outlandish" and "bizarre" comments on Israel were doing the Turkish government "far greater discredit in America than you can imagine." Over the following months, especially in the wake of Turkey's response to the Mavi Marmara incident, a number of U.S. congressmen withdrew their support for Ankara on important policy issues. For example, Anthony Weiner (D-N.Y.) argued that Turkey's membership in NATO should be "called into question," while Shelley Berkley (D-Nev.), threatened to speak "actively" against Turkey's bid to join the European Union.

In late May, on the eve of the coming elections and just before the first anniversary of the storming of the Mavi Marmara, Davutoglu dismissed pleas by the United Nations and others to prevent a new flotilla from departing for Gaza. (Fifteen ships, including the Mavi Marmara, are planning to do so in late June.) Instead, he warned that Turkey would give the "necessary response" to any "act of provocation" by Israel. Erdogan, meanwhile, has accused his main opponent, Kemal Kiliçdaroglu, the head of the Republican People's Party, of being insufficiently tough on Israel. Kiliçdaroglu had criticized Erdogan's government for letting the 2010 flotilla go ahead and putting Turkish lives in harm's way. If Kiliçdaroglu were brave enough, said Erdogan, he would "criticize the Mediterranean pirates instead of being a sycophant." (Kiliçdaroglu has replied in kind, criticizing Erdogan for accepting a prize from a U.S.-based Jewish group.)

Europe has also been the target of much criticism this campaign season. With support for joining the European Union among Turks plummeting from 71 percent in 2004 to 47 percent last year, according to a Eurobarometer poll, there are few votes to be won by campaigning in favor of EU accession. The AKP has only aggravated the situation with combative rhetoric. As much as it has done to advance EU reforms, and as much it may still affirm its commitment to working toward membership, the AKP has made a habit of accusing the European Union of double standards, suggesting that all European opposition to Turkish accession is a symptom of Western Islamophobia. In doing so, it is in danger of boxing itself into a corner: Inadvertently or not, it is stoking popular expectations that Turkey should walk away from accession talks.

Under the AKP, the Turkish government has inserted identity politics, particularly religion, into foreign policy. (It is revealing that Erdogan refers to Europeans as "partners" or "friends" but to Arabs and Iranians as "brothers.") Two years ago, Erdogan proclaimed Sudanese President Omar al-Bashir, who was indicted by the International Criminal Court for war crimes in Darfur, innocent of genocide -- Muslims, he argued, "are incapable of such a thing." At the same time, Ankara, quick to condemn any use of force by Israelis, has been much more indulgent toward its Arab allies, Syria and Libya included.

Turkey has struggled to formulate a coherent response to the uprisings shaking the Arab world. Erdogan was one of the first world leaders to call on Egyptian President Hosni Mubarak to step down, yet one of the last to ask the same of Libya's Qaddafi. In Syria, meanwhile, the Turkish government remains unable to let go of Bashar al-Assad: It has condemned the violence but not the perpetrator. This paralysis reveals an unintended consequence of both deepening ties with autocratic regimes in the Arab world and establishing credibility in the eyes of Arab populations. As Davutoglu himself told journalists in May, "We have felt the pressure of being entrapped between the two successes."

On the heels of the "Arab spring," Joost Lagendijk, a senior adviser at the Istanbul Policy Center and a former EU parliamentarian, told me that foreign policy has become "a bit of a problem" for the Turks. "Zero problems" has turned out to be an illusion, he said. "Turkey is not in control of everything ... Turkey does not always have the answer, does not always know where to go."

Such a realization in Ankara may lead to a substantial reassessment of Turkish foreign policy right after the elections. Turkey already appears more cautious. As Lagendijk said, the Turks "have seen that there are limits to what they can do on their own." With the Middle East in flames and the limits of its leverage in the region laid bare, Turkey may have no choice but to reengage with the European Union and the United States. The policy of "zero problems" has not really worked out too well with Qaddafi and Assad. It might be time to try it out with the West.