slideshow.js: fitting the image correctly, refactoring, fixing preload i...

I just reapplied my changes to the current master. This is a hack to avoid the effort of rebasing #1106.

It is is fixing #1073, #1074 and some other issues.