AT THE end of December 2008 Israel unleashed “Operation Cast Lead”, its bombing campaign against Gaza. Its aim was to counter rocket attacks on Israel by Hamas, which controls Gaza. Three weeks later some 1,400 Palestinians had died. “I personally warned Ehud Barak [Israel's defence minister] that we would react very seriously if Israel did anything in Gaza,” recalls Ali Babacan, then Turkish foreign minister and now deputy prime minister.

The Gaza war proved to be a turning point in Turkey's relations with Israel. A day before Israel attacked, Turkish intermediaries felt they were on the verge of clinching a peace deal between Israel and Syria. “The Israelis misled us,” fumes a Turkish diplomat. Turkey's fury became public soon afterwards, when Recep Tayyip Erdogan, the prime minister, stormed off a panel he was sharing with the Israeli president, Shimon Peres, at the Davos World Economic Forum. “You Israelis know how to kill,” he shouted.

In Turkish eyes Israel proved Mr Erdogan right on May 31st when its commandos raided the Mavi Marmara, a Turkish ship leading a civilian flotilla trying to carry aid to Gaza past Israel's blockade. Nine Turks were killed in the attack. Israel claims it acted in self-defence. It also accuses the organiser of the flotilla, a Turkish charity known as IHH, of being a front for global jihadists. Turkey denies this and has called for a public apology from Israel and a UN-led investigation. Unless Israel agrees, the Turks may sever diplomatic ties altogether.

Some Western countries attribute the apparent eastward realignment of Turkish foreign policy in recent years to the Islamist roots of Mr Erdogan's ruling Justice and Development (AK) party. This week's Turkish vote in the UN Security Council against sanctions on Iran for its nuclear programme has reinforced this view. Some fear the West has “lost” Turkey (a view echoed this week by Robert Gates, the American defence secretary, who blamed the European Union for not doing more to encourage Turkish membership).

The Mavi Marmara incident is seen as further proof of such a Turkish shift. Critics claim Turkey could have done more to stop the flotilla. Turkey's protests that it could not interfere with an initiative of a non-governmental organisation ring hollow. The authorities have often cited “security concerns” to stop Kurdish campaigners (and Turkish troops harass stone-throwing Kurdish youths as much as the Israelis do Palestinians). As for AK's claims that it was not involved, what about the presence of Murat Mercan, an AK deputy, on board an earlier land convoy to Gaza?

For the past 90 years Turkey has in fact neglected the Arab lands of the former Ottoman empire and focused on the West. Turkey concentrated on joining NATO, which it achieved in 1952, and, more recently, trying to get into the EU. This is why Turkey forged such strong ties with Israel. For the secular Turkish elite, friendship with the Jewish state was an antidote to Islamist radicalism. Security ties with Israel, crowned by a military co-operation agreement, also proved useful in Turkey's fight against Kurdish PKK militants. Squeezed between Turkey and Israel, Syria had to boot out the PKK leader, Abdullah Ocalan, who now languishes in a Turkish jail.

Turkish foreign policy may be becoming more assertive, yet EU membership remains the priority. Mr Erdogan secured the opening of membership talks in October 2005, and they continue, if fitfully. The AK government has done more than all its predecessors to reform Turkey. As EU-inspired reforms kick in, Turkey's meddlesome generals have come to matter less. And in place of the old Turkish complaint about living in a bad neighbourhood has come a new policy of “zero problems with the neighbours”, masterminded by the foreign minister, Ahmet Davutoglu.

Mr Davutoglu likes to insist that this policy chimes perfectly with Western goals. This is clearly true in Iraq. America and Turkey both want the place to remain stable as American troops begin to pull out. Mr Davutoglu has played a key role (and, incidentally, blunted Iranian influence) in forging alliances between Shia and Sunni factions. He has also courted Turkey's former foes, the Iraqi Kurds.

Elsewhere, however, Turkey is at odds with America, and not only over Israel. Iran was a sore point even before this week's UN vote. Turkey depends heavily on Iranian gas, and fears that any conflict with Iran might ripple widely. “We are no longer puppets,” declares Mehmet Simsek, Turkey's finance minister. Better relations with neighbours have opened up markets for Turkish goods. A strong economy amid the global financial crisis (the OECD reckons GDP will grow by almost 7% this year) underpins a new confidence.

There is another element to Turkey's new foreign policy. Apparent rejection by the EU and the American occupation of Iraq have left Turks in a resentful mood. Mr Erdogan is wary of providing ammunition to Turkey's (admittedly marginal) Islamist radicals by appearing to pander further to the West. Indeed, Mr Erdogan's fierce anti-Israeli rhetoric may be partly calculated to counter the appeal of Saadet, an overtly Islamist party that accuses AK of “enriching” its members at the expense of “the cause.”

On Israel, though, the government may have gone too far. Even Fethullah Gulen, Turkey's most influential Muslim cleric, has lambasted the flotilla organisers. Mr Erdogan's fiery support for Hamas and his salvos against Israeli “state terrorism” may encourage the radicals rather than appease them. During the recent protests against Israel some demonstrators brandished posters of Hitler, which hardly makes Turkey seem an attractive prospect for the EU.

What next? For all its bluster, the government seems reluctant to sink relations with Israel altogether. A UN-sponsored commission on the flotilla incident could perhaps buy time (see article). Mr Babacan argues that relations with Israel will “never be the same”. Yet the cost to Turkey of cutting ties with Israel would be high. Many Western diplomats argue that it was a mistake for the Turks to get so closely involved in efforts to lift Israel's blockade of Gaza. It may go down well in the Arab street, where Mr Erdogan is now a hero, but Turkey is not about to be able to solve the Israel-Palestine problem on its own.

The prevailing wisdom in AK is that its activism in the region makes it more valuable to the EU. As for America, the hope is that because it needs Turkey in Iraq and Afghanistan, it will put up with a spot of bother. Israel and Iran will severely test these theories.