Geometry, electronic structure and polymer-dopant interaction in conjugated polymers: Thermochromism and thermal undoping in poly( alkylthiophenes)