A second-half brace from Josh McPake helped St Johnstone come from behind to record a 4-2 victory away to Ayr and strengthen their lead at the top of the Scottish Championship.