Theory of the ternary layered system Ti-Al-N