intelligent Javascript text truncation