* Struggle over premier post leaves Libya in limbo
* Political factions backed by ex-rebel brigades
* Negotiations also feed into oil sector chaos
By Patrick Markey and Ahmed Elumami
TRIPOLI, May 29 Libya stumbled deeper into chaos on Thursday uncertain over who runs the country after rival prime ministers both claimed legitimacy in a confrontation threatening to turn into violence among rival factions.
Even by Libya's tumultuous standards, the North African oil producing state has veered closer to its most dangerous crisis in the three years since a NATO-backed uprising helped rebels put an end to Muammar Gaddafi's one-man rule.
After a contested vote in parliament three weeks ago, businessman Ahmed Maiteeq was appointed as Libya's third prime minister in two months with backing from Islamists and independents in the splintered General National Congress (GNC).
On Wednesday, his predecessor acting Prime Minister Abdullah al-Thinni refused to hand over power after questioning the legality of Maiteeq's appointment by a parliament that many Libyans blame for their slow democratic advance.
That political standoff is part of a broader and potentially more explosive confrontation among the rival Islamist, anti-Islamist and regional factions vying to shape Libya's future.
The two prime ministers were waiting for further decisions from the GNC or a high court ruling on the election while a special commission mediated between the two parties on Thursday, officials and advisors said.
Addressing the country late on Wednesday, Thinni, a former defense minister who weeks ago announced his resignation because of an attempted attack on his family, dramatised the risks of a failure of negotiations.
"Our government warns of the dangers facing our homeland with political differences that may lead to a split in the country, a resort to arms and even foreign intervention," he said in a broadcast statement.
Four decades of Gaddafi rule and the three chaotic years that have followed his demise have left Libya with few state institutions that enjoy legitimacy and without a national army to impose any form of stability.
Brigades of former rebel fighters, many with quasi-official status as state security forces, have stepped into politics, loosely allying themselves with rival blocks to become power brokers.
The risk of a broader armed confrontation rose this month when a former Libyan army officer, Khalifa Haftar, began a self-declared campaign against extremists he accuses Islamist parties in the GNC of allowing to flourish.
Irregular forces loyal to Haftar -- a mixture of militias, regular army and air force units -- have bombed Islamist militant bases in the eastern city of Benghazi twice since then, most recently on Wednesday.
Haftar, a former Gaddafi ally who defected in the 1980s, spent years in U.S. exile and returned for the 2011 revolt, also claimed an attack by gunmen on parliament. He rejected Maiteeq and told lawmakers to hand over power.
Rival Islamist armed militia, most allied with the Justice and Construction Party, the political arm of the Muslim Brotherhood, warned Haftar off, accusing him of plotting a coup to overturn Libya's fragile transition.
Amid the standoff, negotiations over the premier's post are emerging.
Ahmed Lamine, Thinni's spokesman, said he would wait for a ruling on a high court appeal made by lawmakers on the legality of Maiteeq's election. A five-member commission of former officials and scholars is also trying to mediate.
"What is going on is between the ex-prime minister Thinni and the GNC. We are ready preparing our plans and meetings and waiting to move into our offices," a Maiteeq spokesman said.
An early election is set for June 25 to elect a new House of Representatives as a way to defuse the crisis.
But time is short with Tripoli tense, Benghazi under threat of armed conflict and the budget still awaiting parliamentary debate and a government stamp of approval.
"This is dangerous in that, who signs the cheques? Who is running the country? We are getting to that point now," said one Western diplomat. "There is a risk of real confusion over who is in charge."
NATO air strikes to halt Gaddafi's forces and help the rebels were meant to be a "light touch" intervention after the bloody years in Iraq and Afghanistan where a prolonged U.S.-led military presence led to heavy casualties.
But without boots on the ground, Western governments have become increasingly alarmed that Libya's growing instability could spread through North Africa.
U.S., European forces have promised to train up several thousand Libyan troops, but those efforts have been slowed by turmoil, leaving irregular forces, often armed with anti-aircraft canons, tanks and Grad rockets, as key players.
Two heavily armed, loose rival confederations -- Zintanis in the west and Misratans based in the port town of Misrata -- and their allies, both claim the mantle of revolutionary leadership and have often challenged the state.
Zintan and its fiercely anti-Islamist Tripoli allies, the Qaaqaa and Sawaiq brigades, are loosely allied with National Forces Alliance movement while more Islamist-leaning Misrata brigades have loosely backed the Justice and Construction Party.
But Libya's standoff is more complex, with loyalties in armed groups crossing lines of regions, tribes, old Gaddafi forces and new revolutionary brigades all laying claim to more spoils of the war.
Haftar, the renegade general, has drawn anti-Islamist militia brigades onto his side with his anti-extremist campaign with many Libyans sick of their hardline message.
But the potential for mayhem is clear should negotiations over the parliament and the prime minister's post fail.
The attack claimed by Haftar on Congress triggered two days of chaotic melees, Grad rocket fire and armed clashes around Tripoli involving rival militias and regular forces.
Nowhere is fallout from Libya's political and militia crisis clearer though than in oil sector.
Since summer, a former rebel, Ibrahim Jathran, has occupied four vital oil ports with thousands of his troops -- all but shutting off crude exports -- to demand greater autonomy for his self-declared Cyrenica government.
Jathran negotiated a deal with Thinni's government to bring his blockade of ports steadily to an end and Libya's battered oil exports back closer to 1.4 million barrels per day. Now with Maiteeq poised to take over, that deal maybe off the table.
"The implications for the hydrocarbons sector are plain. For explorers, there is no delta," North African analyst Geoff Porter said. "The restoration of Libyan production and exports is now much further off than it was two weeks ago." (Reporting by Patrick Markey; Editing by Paul Taylot)