Until just a few weeks ago, Sri Lanka's upcoming election seemed a mere formality. Nothing, it seemed, could keep President Mahinda Rajapaksa from rolling to a third term in office.

He was the president hailed as a king after crushing the Tamil Tiger rebels in 2009 and ending the island nation's 25-year civil war. He is a charismatic campaigner with vast campaign funds. He has turned the government into an extended family business, with politically powerful brothers, sons and nephews who can all help his candidacy.

But times change. Quickly.

An internal revolt now threatens Rajapaksa's hold on power. Health Minister Maithripala Sirisena, a close Rajapaksa aide and No. 2 in the president's Freedom Party, defected in a secretly choreographed news conference in late November, announcing he would run as an opposition candidate in Thursday's election.

Power, he said, has become too concentrated, and corruption epidemic.

"One family has captured the country's economy, wealth, administration, and the management of the political party," Sirisena told reporters. He says at campaign rallies that Rajapaksa began to believe the public support after the end of the war and "perhaps he thought he could become a real king."

A confident Rajapaksa had called the election two years ahead of schedule, hoping to win a third six-year term before voters' memories faded of the defeat of the Tigers. The next day, though, Sirisena — who had publicly called for Rajapaksa to run again — made his own surprise announcement.

That set off a wave of political turmoil and energized a long-dispirited opposition that had not been looking forward to the election.

Jumping between parties and backroom deals are part of the political landscape in Sri Lanka, but this has surpassed the normal with 21 lawmakers and ministers fleeing the government so far to back Sirisena. He has won support from the opposition United National Party, the main ethnic minority parties, many professional groups and some powerful Buddhist monks. Rajapaksa's government has come under heavy criticism for turning a blind eye to recent anti-Muslim violence, and the largest Muslim party has also defected.

But while it is Rajapaksa's biggest electoral challenge since he came to power in 2005, he still has immense advantages, from popularity among the majority Sinhala ethnic group — which makes up more than 70 percent of Sri Lanka's 21 million people — to control of state media. Plus, the economy has been growing since the end of the civil war, although there are complaints that it has not reached the grassroots.

"The president may be down from where he was, but he is certainly not out," said Jehan Perera, a political analyst with the National Peace Council.

"He is a two-time winner of presidential elections and is a fighter."

Rajapaksa denies all accusations of corruption, and insists he will again win on Jan. 8.

"I am not worried. I will be the president even after the 9th," he has repeated in campaign speeches around the Indian Ocean nation.

While there is no reliable polling, cracks have appeared in his core constituency — the Buddhist Sinhala majority — amid repeated accusations of amassing excessive power and corruption.

Sirisena has accused the Rajapaksas of amassing wealth from their pet highway projects and implied they were receiving benefits from tobacco and pharmaceutical companies in return for arranging favorable laws.

The president's relatives, meanwhile, are everywhere. One brother is Cabinet minister for economic development, another is the speaker of Parliament and a third is the defense secretary, with control over the armed forces. His older son is a member of Parliament and a nephew is a provincial chief minister.

That left few powerful jobs and fewer resources from the national budget for non-Rajapaksas in the party, alienating many senior politicians who felt shunted aside.

Kolitha Dissanayake, a businessman from the western town of Gampaha and a longtime Rajapaksa voter, said the talk of family rule and corruption had changed his mind.

"It is these revelations that have hit the people," he said.

Until Sirisena's revolt, Rajapaksa appeared set for a political walkover, with the opposition struggling to find an appealing candidate. Rajapaksa even joked that he was shadow boxing without a viable opponent.

Rajapaksa's popularity stems from his 2009 victory, although bloody, over the Tigers, who had staged guerrilla war for decades for an independent state for the minority Tamils, and for the rebuilding of the country's infrastructure after the violence was over.

His stature skyrocketed among the Buddhist Sinhalese after the war's end. Immense billboards were erected in the capital equating him with ancient warrior kings. The song "Long Live Great King" played regularly on television and radio, and became a popular mobile phone ringtone.

Rajapaksa was re-elected in 2010 on that wave, defeating his former army commander, Gen. Sarath Fonseka, who had also claimed credit for the victory.

After his victory, Rajapaksa had Fonseka jailed on charges of corruption and of planning his political career while still in a military uniform. He was stripped of his rank and medals. That sparked unease in the Sinhala community, where Fonseka was a war hero and hugely popular.

Rajapaksa then used his overwhelming parliamentary majority to scrap a constitutional two-term limit for president, and to give the president the power to appoint judges, top bureaucrats, police officials and military chiefs. He orchestrated the impeachment of the country's chief justice and replaced her with a trusted adviser.

Human rights and opposition groups accuse him of using the military to suppress legitimate public protests, protecting drug smugglers and protecting politicians from his party accused of rape and child abuse.

Rajapaksa's use of the military in 2013 to quell a public protest demanding clean drinking water resulted in the deaths of three people, causing much public anger.

Sirisena promises to clip the presidency's powers, making the office accountable to the courts and Parliament.

The main Tamil party has also thrown its support to Sirisena, saying Rajapaksa had made few efforts to heal the wounds of the civil war. About 300,000 people were displaced in the war's final phase and many remain in poverty without proper housing and under constant military surveillance. Relatives are still looking for those who surrendered to the military in the final days of the fighting.

In campaign rallies, Rajapaksa projects confidence.

During a recent stump speech, a supporter in the crowd told him not to worry. "I am not scared, comrade," he replied.

___

Associated Press writer Bharatha Mallawarachi contributed to this story.