Former Brazilian President Luiz Inácio Lula da Silva has pulled out of next month's presidential election to allow his running mate to stand in his place.

Lula had been the frontrunner despite being in jail for a corruption conviction and despite the top electoral court barring his candidacy.

With the deadline for the registration of candidates only hours away, he threw his weight behind his Workers' Party colleague Fernando Haddad.

The election is on 7 October.

It has been a tumultuous election campaign dominated by Lula's legal battles and the stabbing last week of Jair Bolsonaro, the far-right candidate who is second in the polls behind Lula.

Mr Bolsonaro was attacked by a lone assailant during a campaign rally and is still in hospital..

What has happened with Lula?
With the clock ticking down on a deadline for candidates to be registered, senior members of the Workers' Party gathered in front of the police headquarters where Lula, 72, is serving a 12-year prison sentence for corruption.

The party's founder, Luiz Eduardo Greenhalgh, then read out a letter written by Lula in his cell.

In it, Lula, who governed from January 2003 until December 2010, said that he had been "unjustly imprisoned" before asking "all who would vote for me to vote for our friend Fernando Haddad for president".

Why was Lula barred?
Lula was barred from running for the presidency under a 2010 law dubbed "Clean Slate". It prohibits those who have a criminal conviction which has been upheld on appeal from running for public office.

In July 2017, Lula was found guilty of accepting an upgrade to a beachfront flat as a bribe from an engineering firm involved in a major corruption scheme.

Lula has always denied any wrongdoing and appealed against the verdict. The appeal was rejected and Lula was given 24 hours to turn himself in.

From his cell, Lula has continued the legal fight against his conviction and the ban which prevents him from running for public office.

Why did he finally give up?
The decision was made less than two weeks after Brazil's top electoral court ruled that he was "ineligible" to run for the presidency.

Lula's legal team has appealed against that decision and the Supreme Court is still due to rule on it.

But after a Workers' Party request to extend the deadline to register presidential candidates from the 11 September to 17 September was rejected, its leadership felt it had to act.

Not registering Mr Haddad meant that if Lula's appeal were to fail, the party could have been left without a presidential candidate altogether.

After Lula backed Mr Haddad in his letter, the party registered him as its new candidate with just hours to spare.

What next?
The Workers' Party hopes people who were going to vote for Lula will now vote for Mr Haddad.

Lula left office with record approval ratings in 2010 and despite his imprisonment, almost 40% of people asked by polling firm Datafolha said they would vote for him.

Mr Haddad, on the other hand, is not very well known outside of São Paulo, where he was mayor from 2013 to 2017.

In his letter, Lula said that Mr Haddad "will be my representative in this battle". "Our name is now Haddad!" he wrote.

In its first TV spot since Mr Haddad became its presidential candidate, the party also stressed the links between the two men.

Both Lula and Mr Haddad appear for the same amount of time and rather than talking about himself, Mr Haddad praises Lula and his achievements.

Who is Fernando Haddad?
He was the minister of education during Lula's presidency and is thought to enjoy his trust. He was Lula's vice-presidential running mate until now.

From 2013 to 2017, the 55-year-old also served as mayor of São Paulo, Brazil's most populous city and faced mass demonstrations against bus fare rises.

The Workers' Party hopes that Mr Haddad will get through to the run-off scheduled for 28 October.

But Mr Haddad is facing legal problems of his own. Prosecutors allege that during his campaign for mayor his team received a loan from a construction firm which stood to benefit from contracts once he was elected. He has denied any wrongdoing.

What do the polls say?
The most recent poll carried out by research company Ibope between 8 and 11 September suggests the following results for the top five candidates:

Jair Bolsonaro (far right, PSL): 26%

Ciro Gomes (centre left, PDT): 11%

Marina Silva (green, Rede): 9%

Geraldo Alckmin (centre right, PSDB): 9%

Fernando Haddad (Workers' Party, PT): 8%

It is expected that Mr Haddad's share will rise now he is the Workers' Party's official candidate.