Moving the calculateLegendWidth() function up to fix some firefox bugs