If Barack Obama wins the presidential election, at least part of the reason will be that John McCain failed to recognise a landmark cultural shift.

The one-time bipartisan moderate cast his lot with the Republican party's hard right just as it was losing influence. Rather than battling for independents and conservative Democrats, McCain chose instead to excite the passions of his party's narrowest constituency. In so doing, he ended up running not just against Obama, but against his own history of bipartisan outreach.

I do not intend to write McCain's political obituary. Though Obama leads in many polls by a substantial margin today, the election is still nearly two weeks away. A lot could happen between now and then.

But assuming McCain really does go on to lose, there are three major blunders he made that arise from his attempt to connect with the right's sense of resentment and us-against-them populism: his war against the news media, with whom he had long been so friendly that he once jokingly called them "my base"; his inexplicable choice of Sarah Palin as his running mate and his deeply personal attacks against Barack Obama.

Let me take them one at a time.

1. McCain and the media. In 2000 McCain nearly upset George Bush's march to the nomination by inviting reporters aboard the Straight Talk Express and charming them with anecdotes and access. In 2008 he didn't even give the press a chance, trashing it on the assumption that it would be in the tank for Obama - and possibly in the hopes that he might be able to tap into the anti-media anger of Hillary Clinton supporters.

Perhaps the paradigmatic moment was McCain's bizarre August interview with Time magazine, in which he answered standard-issue questions with undisguised hostility and contempt. No doubt this played well with the right, which has long detested what it sees as an elite liberal media. What McCain seems to have missed is that even if reporters, on the whole, favoured Obama, they still liked him, too. By cutting them off, McCain essentially gave them permission to dump on him at will. And many have.

2. The Palin pick. The Alaska governor is a talented political performer, and McCain's choice worked for about two weeks. But among her numerous deficits as a general-election candidate is the fact that she may be the most extreme religious candidate since William Jennings Bryan.

At a time when the economy is melting down, and when McCain could have been putting, say, Mitt Romney front and centre as an experienced businessman and financial manager, we were learning that Palin had once prayed that God would build a natural-gas pipeline - and had stood by while the minister of her former church spoke of God's special plans for Alaska in a post-Apocalypse world.

You think this is what the folks wielding those people metres on CNN are looking for? Think again.

3. Getting personal. Attacks on an opponent's policies are fine. Even attacks that stretch the truth are hardly cause for consternation. But McCain has gone after Obama in the most vile terms imaginable.

There are many examples from which to choose. I'll pick two.

The first was McCain's claim, earlier in the campaign, that Obama would rather "would rather lose a war in order to win a political campaign". By characterising Obama as deeply unpatriotic, and perhaps even treasonous, McCain played directly into unstated fears about a black candidate with a Muslim-sounding name.

The second was a McCain ad about Obama's support as an Illinois legislator for a sex-education bill that would have taught kindergarteners how to ward off predators. Except that's not what the ad said. Instead, it claimed that the bill would have mandated "comprehensive sex education" for kindergarten pupils - as sleazy a lie as has ever appeared in a major-party candidate's advertisements.

Trouble is, the truism that negative campaigning works didn't seem to hold this time. It may have energised the sorts of people who turn out at Palin rallies, but it appeared to turn off the undecided moderates who will actually choose the next president.

What happened to McCain would be sad if he hadn't done it to himself. You'll sometimes hear an old defender of his try to claim that McCain is better than his campaign. Nonsense. There is no such thing as a candidate who is better than his campaign.

It could be that victory was never a realistic possibility for McCain following eight years of an unpopular Republican president and an economic crisis. But if he couldn't come out of this with the presidency, he could have at least preserved his reputation.

Barring a truly astonishing comeback, McCain is likely to emerge with neither.