diff --git a/superfish.module b/superfish.module
index 9bf96ba..747ad22 100644
--- a/superfish.module
+++ b/superfish.module
@@ -482,7 +482,7 @@ function superfish_block_save($delta = 0, $edit = array()) {
 function superfish_contents($delta = 0) {
   global $language;
   list($menu_name, $mlid) = explode(':', variable_get('superfish_menu_' . $delta, 'main-menu:0'));
-  
+
   $sfsettings = $sfoptions = $sfplugins = array();
   $sfsettings['depth'] = variable_get('superfish_depth_' . $delta, '-1');
   $sfsettings['type'] = variable_get('superfish_type_' . $delta, 'horizontal');
@@ -513,59 +513,72 @@ function superfish_contents($delta = 0) {
   $sfsettings['megamenu_exclude'] = variable_get('superfish_mcexclude_' . $delta, '');
   $sfsettings['megamenu_exclude'] = (strpos($sfsettings['megamenu_exclude'], ',')) ? array_filter(explode(',', $sfsettings['megamenu_exclude'])) : $sfsettings['megamenu_exclude'];
 
-  $sfoptions['pathclass'] = ($sfsettings['type'] == 'navbar') ? "pathClass: '" . variable_get('superfish_pathclass_' . $delta, 'active-trail') . "'" : '';
-  $sfoptions['pathlevels'] = (variable_get('superfish_pathlevels_' . $delta, 1) != 1) ? "pathLevels: '" . variable_get('superfish_pathlevels_' . $delta, 1) . "'" : '';
-  $sfoptions['delay'] = (variable_get('superfish_delay_' . $delta, 800) != 800) ? "delay: " . variable_get('superfish_delay_' . $delta, 800) : '';
+  if ($sfsettings['type'] == 'navbar') {
+    $sfoptions['pathClass'] = variable_get('superfish_pathclass_' . $delta, 'active-trail');
+  }
+  if (variable_get('superfish_pathlevels_' . $delta, 1) != 1) {
+    $sfoptions['pathLevels'] = variable_get('superfish_pathlevels_' . $delta, 1);
+  }
+  if (variable_get('superfish_delay_' . $delta, 800) != 800) {
+    $sfoptions['delay'] = variable_get('superfish_delay_' . $delta, 800);
+  }
   $slide = variable_get('superfish_slide_' . $delta, 'none');
   if ($slide == 'none') {
-    $sfoptions['animation'] = "animation: {opacity:'show'}";
+    $sfoptions['animation'] = array('opacity' => 'show');
   }
   else {
-    $sfoptions['animation'] = "animation: {opacity:'show',";
-    $sfoptions['animation'] .= ($slide == 'vertical') ? "height:'show'" : '';
-    $sfoptions['animation'] .= ($slide == 'horizontal') ? "width:'show'" : '';
-    $sfoptions['animation'] .= ($slide == 'diagonal') ? "height:'show',width:'show'" : '';
-    $sfoptions['animation'] .= "}";
+    $sfoptions['animation'] = array(
+      'opacity' => 'show',
+    );
+    if ($slide == 'vertical' || $slide == 'diagonal') {
+      $sfoptions['animation']['height'] = 'show';
+    }
+    if ($slide == 'horizontal' || $slide == 'diagonal') {
+      $sfoptions['animation']['width'] = 'show';
+    }
   }
+
   $speed = variable_get('superfish_speed_' . $delta, 'normal');
-  $sfoptions['speed'] = "speed: " . ((is_numeric($speed)) ? $speed : (($speed == ('slow' || 'normal' || 'fast')) ? "'" . $speed . "'" : ''));
-  $sfoptions['arrow'] = (variable_get('superfish_arrow_' . $delta, 'false') == 1) ? "autoArrows: true" : "autoArrows: false";
-  $sfoptions['shadow'] = (variable_get('superfish_shadow_' . $delta, 'true') == 1) ? "dropShadows: true" : "dropShadows: false";
-  $sfoptions['hoverintent'] = (variable_get('superfish_hid_' . $delta, 1) == 0) ? "disableHI: true" : '';
-  $sfoptions = implode(",\n", array_filter($sfoptions));
+  $sfoptions['speed'] = is_numeric($speed) ? intval($speed) : $speed;
+  $sfoptions['autoArrows'] = variable_get('superfish_arrow_' . $delta, 'false') == 1;
+  $sfoptions['dropShadows'] = variable_get('superfish_shadow_' . $delta, 'true') == 1;
+  $sfoptions['disableHI'] = variable_get('superfish_hid_' . $delta, 1) == 0;
 
-  $sfplugins['touchscreen'] = (variable_get('superfish_touch_' . $delta, 0) == 1) ? ' .sftouchscreen()' : '';
-  $tsua = variable_get('superfish_touchua_' . $delta, 0);
-  $tsual = variable_get('superfish_touchual_' . $delta, '');
-  if ($tsua == 1 && !empty($tsual)) {
-    $tsuac = array();
-    if (strpos($tsual, '*')) {
-      $tsual = explode('*', $tsual);
-      foreach ($tsual as $ua) {
-        $tsuac[] = (strpos(drupal_strtolower($_SERVER['HTTP_USER_AGENT']), drupal_strtolower($ua))) ? 1 : 0;
+  if (variable_get('superfish_touch_' . $delta, 0) == 1) {
+    $sfplugins['touchscreen'] = true;
+    $tsua = variable_get('superfish_touchua_' . $delta, 0);
+    $tsual = variable_get('superfish_touchual_' . $delta, '');
+    if ($tsua == 1 && !empty($tsual)) {
+      $tsuac = array();
+      if (strpos($tsual, '*')) {
+        $tsual = explode('*', $tsual);
+        foreach ($tsual as $ua) {
+          $tsuac[] = (strpos(drupal_strtolower($_SERVER['HTTP_USER_AGENT']), drupal_strtolower($ua))) ? 1 : 0;
+        }
+      }
+      else {
+        $tsuac[] = (strpos(drupal_strtolower($_SERVER['HTTP_USER_AGENT']), drupal_strtolower($tsual))) ? 1 : 0;
+      }
+      if (!in_array(1, $tsuac)) {
+        unset($sfplugins['touchscreen']);
       }
     }
-    else {
-      $tsuac[] = (strpos(drupal_strtolower($_SERVER['HTTP_USER_AGENT']), drupal_strtolower($tsual))) ? 1 : 0;
-    }
-    $sfplugins['touchscreen'] = (in_array(1, $tsuac)) ? $sfplugins['touchscreen'] : '';
   }
-  $sfplugins['supposition'] = (variable_get('superfish_spp_' . $delta, 0) == 1) ? ".supposition()" : '';
-  $sfplugins['bgiframe'] = (variable_get('superfish_bgf_' . $delta, 0) == 1) ? ".find('ul').bgIframe({opacity:false})" : '';
-  $sfplugins = implode('', array_filter($sfplugins));
+  if (variable_get('superfish_spp_' . $delta, 0) == 1) {
+    $sfplugins['supposition'] = true;
+  }
+  if (variable_get('superfish_bgf_' . $delta, 0) == 1) {
+    $sfplugins['bgiframe'] = true;
+  }
 
   if (variable_get('superfish_supersubs_' . $delta, 1) == 1) {
     $minwidth = variable_get('superfish_minwidth_' . $delta, '12');
     $maxwidth = variable_get('superfish_maxwidth_' . $delta, '27');
-    $supersubs = '.supersubs({minWidth: ' . $minwidth . ', maxWidth: ' . $maxwidth . ', extraWidth: 1})';
-  }
-  else {
-    $supersubs = '';
+    $sfplugins['supersubs']['minWidth'] = is_numeric($minwidth) ? intval($minwidth) : 12;
+    $sfplugins['supersubs']['maxWidth'] = is_numeric($maxwidth) ? intval($maxwidth) : 27;
+    $sfplugins['supersubs']['extraWidth'] = 1;
   }
 
-  // Finally our Javascript
-  $javascript = "jQuery(function(){\n" . "jQuery('#superfish-" . $delta . "')" . $supersubs . ".superfish({\n" . $sfoptions . "})" . $sfplugins . ";\n});";
-
   $vars = array(
     'id' => $delta,
     'menu_name' => $menu_name,
@@ -573,8 +586,14 @@ function superfish_contents($delta = 0) {
     'sfsettings' => $sfsettings
   );
   if ($output = theme('superfish', $vars)) {
+    // Prepare the JavaScript settings.
+    $js['superfish'][$delta]['sf'] = isset($sfoptions) ? $sfoptions : array();
+    if (!empty($sfplugins)) {
+      $js['superfish'][$delta]['plugins'] = $sfplugins;
+    }
     // Adding required Javascript
-    drupal_add_js($javascript, array('type' => 'inline', 'weight' => 100));
+    drupal_add_js($js, 'setting');
+    drupal_add_js(drupal_get_path('module', 'superfish') . '/superfish.js');
 
     // Adding required CSS files
     if ($sfsettings['style'] != 'none') {
@@ -582,6 +601,7 @@ function superfish_contents($delta = 0) {
         drupal_add_css(libraries_get_path('superfish') . '/style/' . $sfsettings['style'] . '.css');
       }
     }
+
     $extracss = variable_get('superfish_pathcss_' . $delta, '');
     if ($extracss) {
       if (strpos($extracss, ',')) {
@@ -595,7 +615,7 @@ function superfish_contents($delta = 0) {
       }
     }
   }
-  
+
   return $output;
 }
 
@@ -1026,4 +1046,4 @@ function theme_superfish_build($variables) {
   $output['parent_children'] = $parent_children;
   $output['single_children'] = $single_children;
   return $output;
-}
\ No newline at end of file
+}
