Resolved Bug 974264 - Viewport width/height were reversed and using CSS pixels causing an incorrect preview size selection.

Added threshold for optimal preview size scale.

Fixed undefined error.