Please don't call $(document).width() in a loop but cache it beforehand as it can be expensive.