Check for presence of global variable jQuery first, before checking for jQuery.ui, like so ...
else if (typeof jQuery == 'undefined' || typeof jQuery.ui == 'undefined'){ ... }
shorthand version that should be work as well is
else if (jQuery && jQuery.ui){ ... }