* Turkey holds municipal elections nationwide on Sunday
* Polls are test of Erdogan's support after scandals, leaks
* Power struggle with Muslim cleric may intensify after poll
By Ralph Boulton and Humeyra Pamuk
ISTANBUL/ANKARA, March 30 Prime Minister Tayyip Erdogan turns to the ballot box that has favoured him over a decade on Sunday in his battle to ward off graft allegations and stem a stream of damaging security leaks he blames on "traitors" embedded in the Turkish state.
The municipal elections have become a crisis referendum on the rule of Erdogan and his Islamist-rooted AK Party and he has been crisscrossing the nation of 77 million during weeks of hectic campaigning to rally his conservative core voters.
"They are all traitors," Erdogan said of his opponents at a rally in Istanbul, Turkey's commercial capital, on Saturday. "Let them do what they want. Go to the ballot box tomorrow and teach all of them a lesson ... Let's give them an Ottoman slap."
Erdogan has purged some 7,000 people from the judiciary and police since anti-graft raids in December targeting businessmen close to Erdogan and sons of ministers. Erdogan blames the probe on a secretive Islamic cleric, a former ally, who he says is using supporters in the police to try to topple the government.
The AK Party, which swept to power in 2002 on a platform of eradicating the graft that blights Turkish life, hopes on Sunday to equal or better its overall 2009 vote of 38.8 percent and markets have steadied this week in expectation of such a result.
A vote of less than 36 percent, not considered likely, would be a huge blow for Erdogan and unleash AKP power struggles. A vote of more than 45 percent, some fear, could herald a period of harsh reckoning with opponents in politics and state bodies.
Sinan Ulgen, head of Istanbul's Edam think-tank, said hopes the polls may bring stability and clarity may prove unfounded.
"We have arrived at the stage where the opposition now challenges Erdogan's legitimacy to rule not on the basis of electoral support, of him losing popular support, but arguing that he is no longer eligible and fit to rule until he fully answers allegations against him of corruption," he said.
"This is a new era for Turkey."
Uncertainty has taken its toll on the stock market and on the Turkish lira, which has lost four percent of its value this year. Many foreign and domestic investors are awaiting the elections and their aftermath before making decisions.
"ALLIANCE OF EVIL"
AK's chief opponent, the Republican People's Party (CHP), portrays Erdogan as a corrupt dictator ready to hang on to power by any means. Capture of the capital Ankara or Istanbul would allow them to claim some form of victory.
Erdogan formed the AKP in 2001, attracting nationalists and centre-right economic reformers as well as religious conservatives who form his base. Since his 2011 poll victory he has in his statements moved more towards these core supporters.
"Erdogan is a strong leader. He does not buckle in the face of hardship," said Aliye, 42, at an Istanbul rally. "Now he is under attack, the state is under attack."
The graft scandal, also involving anonymous internet postings of tapped state communications implicating Erdogan in corrupt actions he denies, was all but eclipsed in recent days by the leaking of a recording of a top-level security meeting.
In the recording, the intelligence chief, foreign minister and military commanders discuss possible armed intervention in Syria. A senior government official described the leak as one of the biggest crises in Turkish history, presenting as it does the threat of further sensitive disclosures.
The leak seemed particularly to target MIT intelligence chief Hakan Fidan, possibly Erdogan's closest confidante.
It is unclear who recorded the meeting and posted it on YouTube - though officials point a finger at Islamic cleric Fethullah Gulen's Hizmet network - and that is the alarming aspect of it. There is a sense in Ankara that government has only tentative control of state bodies and part of the security apparatus while power struggles play out.
Hizmet denies orchestrating the leak scandal and manoeuvring to control the state apparatus; but those close to the network say they fear a heavy crackdown after the elections.
Erdogan, who has described Hizmet as a terrorist organisation in an "alliance of evil" with major opposition parties, seems likely to act quickly to tackle the leaks and first indications possibly emerged on Saturday.
Police briefly detained overnight a prominent writer and journalist, Onder Aytac, known to be close to the Hizmet movement, on suspicion of having information about the bugging of the foreign ministry Syria meeting, Hurriyet newspaper said.
CNN Turk television meanwhile reported Erdogan's lawyers as asking prosecutors to take precautionary measures to stop both Aytac and Emre Uslu, a newspaper columnist, academic and former senior anti-terrorism police official, from fleeing abroad.
The state prosecutor could not be reached for comment.
(Additional reporting by Can Sezer, Alexandra Hudson, Gulsen Solaker; Editing by Gareth Jones)