Ferrari have struggled to provide Alonso with a pace-setting car in his five years at the team

Fernando Alonso is on the verge of sealing a deal to join McLaren.

The two-time champion, who is to leave Ferrari two years before the end of his contract, had been deciding between McLaren and taking a year off.

But a deal for Alonso to return to the team he left after one acrimonious season in 2007 is now all but done.

Within McLaren the internal debate has switched to whether to retain Jenson Button or Kevin Magnussen alongside him - the Dane is emerging as favourite.

Negotiations between Alonso and McLaren over the fine detail of his new contract are not completely finalised but insiders say they have gone so far that it would now be a major shock if the 33-year-old did not sign.

Alonso is to leave Ferrari at the end of the season

The Spaniard had originally wanted a one-year deal, to keep himself free for 2016, but he is now thought likely to to ink a deal for 2015 and 2016 with a further year as an option, a so-called 'two-plus-one deal' as is common with many F1 driver contracts.

A move to McLaren, who are starting a new engine partnership with Honda next season, has long been considered Alonso's most likely option since it became clear he was leaving Ferrari.

He admitted at the US Grand Prix that his next move would be "probably not a big surprise".

Alonso added: "I have a very ambitious plan in my head for my future, which I think is the best for Ferrari as well.

"If that happens, people will be very excited, as I am. I am extremely happy. All the things in the last two or three months follow exactly the plan I had."

Hamilton powers to victory in Austin

Alonso had been considering waiting to see whether an opportunity with world champions Mercedes might open up at the end of 2015 if negotiations with Lewis Hamilton over a new deal hit trouble, but appears to have decided against doing so while sitting on the sidelines.

Mercedes and Hamilton have both made it clear that their wish is to work together beyond the end of the world championship leader's current contract, which expires next year.

However, talks could hit trouble over Hamilton's salary - some insiders believe Mercedes will try to renegotiate it down from its current level of $31m (£19.4m) plus bonuses.

Signing a 'two-plus-one' deal with McLaren does not necessarily preclude Alonso leaving at the end of next year - as his situation with Ferrari this year has proved. But if the team show progress next season after two poor years he would be expected to stay.

Ferrari have not yet officially announced that Alonso is leaving, nor their signing of world champion Sebastian Vettel as his replacement.

Lewis Hamilton is considered one of the world's most marketable sports figures

But Ferrari's former president Luca Di Montezemolo told Italian television last month that Alonso was moving on.

Before his departure last month, Di Montezemolo granted Alonso a release from his contract at the driver's request after five years with the team. Alonso has twice narrowly missed out on the world title but Ferrari have failed to deliver him a pace-setting car.

McLaren have been keeping Button and Magnussen waiting while they sought to finalise their deal with Alonso, who they have been pursuing for more than a year.

They have been weighing up whether to plump for Button's experience and proven quality or Magnussen's youthful promise.

The two have been evenly matched for pure pace this year - Magnussen has shaded Button nine-eight in qualifying in the 17 races so far this year - but Button has been the more convincing performer in races. He leads Magnussen 94-53 on points and is three places ahead in the championship.

McLaren have not finally decided what to do, but they are increasingly convinced that they should stick with Magnussen, who at 22 is just beginning his career, despite being concerned at the Dane's struggles to get on top of tyre management in races.

McLaren are deciding whether to stick with the experience of Button or the youth of Magnussen

If the decision goes against Button, the 34-year-old is poised for a move to endurance racing. And he may even take the decision out of McLaren's hands by making the first move.

The 2009 world champion has held talks with Porsche, who signed former F1 driver Mark Webber, a close friend of Button, last season.

Button had previously expressed a distaste for the Le Mans 24 Hours, the centrepiece and blue riband event of the world endurance championship, but he said at the US Grand Prix that a move to sportscars should not be ruled out.

"Anything is possible in life if you have the right situation, and that has to be there," he said.