diff --git a/popups.info b/popups.info
index 40c433e..5cefc2a 100644
--- a/popups.info
+++ b/popups.info
@@ -1,5 +1,4 @@
 name = Popups API
 description = General dialog creation utilities
 package = User interface
-core = 6.x
-
+core = 7.x
diff --git a/popups.install b/popups.install
index 3160b71..00b7b35 100644
--- a/popups.install
+++ b/popups.install
@@ -10,5 +10,5 @@
  * Ensures popups runs after everything else, since it short circuits in hook_init.
  */
 function popups_install() {
-  db_query("UPDATE {system} SET weight = %d WHERE name = 'popups'", 9999);
+  db_update('system')->fields(array('weight' => 9999))->condition('name', 'popups')->execute();
 }
\ No newline at end of file
diff --git a/popups.js b/popups.js
index 57be2ab..bb4503c 100644
--- a/popups.js
+++ b/popups.js
@@ -1,4 +1,4 @@
-
+(function ($) {
 /**
  * Popup Modal Dialog API
  *
@@ -521,7 +521,6 @@ Popups.addOverlay = function() {
     // Doing absolute positioning, so make overlay's size equal the entire body.
     var $doc = $(document);
     $overlay.width($doc.width()).height($doc.height());
-    $overlay.click(function(){Popups.close();});
     $('body').prepend($overlay);
   }
 };
@@ -824,8 +823,12 @@ Popups.openPath = function(element, options, parent) {
       // Broadcast an event that the path was opened.
       $(document).trigger('popups_open_path_done', [element, href, popup]);
     },
-    complete: function() {
-      $('body').css("cursor", "auto"); // Return the cursor to normal state.
+    complete: function (response, status) {
+    	$('body').css("cursor", "auto"); // Return the cursor to normal state.
+    	if (status == 'error' || status == 'parsererror') {
+          return ajax.error(response, ajax.url);
+        }
+    	
     }
   };
 
@@ -1079,49 +1082,51 @@ Popups.testContentSelector = function() {
  *   The jQuery object to apply the behaviors to.
  */
 
-Drupal.behaviors.popups = function(context) {
-  Popups.saveSettings();
-
-  var $body = $('body'),
-    $popit;
-
-  if (!$body.hasClass('popups-processed')) {
-    $body.addClass('popups-processed');
-    $(document).bind('keydown', Popups.keyHandle);
-    $popit = $('#popit');
-    if ($popit.length) {
-      $popit.remove();
-      Popups.message($popit.html());
-    }
-
-    // Make note of all the CSS and JS on the page so when we load a popup we
-    // don't try to add them a second time.
-    $('link[rel="stylesheet"][href]').each(function(i, v) {
-      Popups.originalCSS[$(this).attr('href').replace(/^(\/.+)\?\w$/, '$1')] = 1;
-    });
-    if (Drupal.settings.popups && Drupal.settings.popups.originalCSS) {
-      $.extend(Popups.originalCSS, Drupal.settings.popups.originalCSS);
-    }
-    $('script[src]').each(function(i, v) {
-      Popups.originalJS[$(this).attr('src').replace(/^(\/.+)\?\w$/, '$1')] = 1;
-    });
-    if (Drupal.settings.popups && Drupal.settings.popups.originalJS) {
-      $.extend(Popups.originalJS, Drupal.settings.popups.originalJS);
-    }
-  }
-
-  // Add the popups-link-in-dialog behavior to links defined in Drupal.settings.popups.links array.
-  // Get these from current Drupal.settings, not Popups.originalSettings, as each page has it's own hooks.
-  if (Drupal.settings.popups && Drupal.settings.popups.links) {
-    $.each(Drupal.settings.popups.links, function (link, options) {
-      Popups.attach(context, link, Popups.options(options));
-    });
-  }
-
-  Popups.attach(context, '.popups', Popups.options({updateMethod: 'none'}));
-  Popups.attach(context, '.popups-form', Popups.options({updateMethod: 'ajax'})); // ajax reload.
-  Popups.attach(context, '.popups-form-reload', Popups.options({updateMethod: 'reload'})); // whole page reload.
-  Popups.attach(context, '.popups-form-noupdate', Popups.options({updateMethod: 'none'}));  // no reload at all.
+Drupal.behaviors.popups = {
+  attach: function (context, settings) {
+	  Popups.saveSettings();
+	
+	  var $body = $('body'),
+	    $popit;
+	
+	  if (!$body.hasClass('popups-processed')) {
+	    $body.addClass('popups-processed');
+	    $(document).bind('keydown', Popups.keyHandle);
+	    $popit = $('#popit');
+	    if ($popit.length) {
+	      $popit.remove();
+	      Popups.message($popit.html());
+	    }
+	
+	    // Make note of all the CSS and JS on the page so when we load a popup we
+	    // don't try to add them a second time.
+	    $('link[rel="stylesheet"][href]').each(function(i, v) {
+	      Popups.originalCSS[$(this).attr('href').replace(/^(\/.+)\?\w$/, '$1')] = 1;
+	    });
+	    if (Drupal.settings.popups && Drupal.settings.popups.originalCSS) {
+	      $.extend(Popups.originalCSS, Drupal.settings.popups.originalCSS);
+	    }
+	    $('script[src]').each(function(i, v) {
+	      Popups.originalJS[$(this).attr('src').replace(/^(\/.+)\?\w$/, '$1')] = 1;
+	    });
+	    if (Drupal.settings.popups && Drupal.settings.popups.originalJS) {
+	      $.extend(Popups.originalJS, Drupal.settings.popups.originalJS);
+	    }
+	  }
+	
+	  // Add the popups-link-in-dialog behavior to links defined in Drupal.settings.popups.links array.
+	  // Get these from current Drupal.settings, not Popups.originalSettings, as each page has it's own hooks.
+	  if (Drupal.settings.popups && Drupal.settings.popups.links) {
+	    $.each(Drupal.settings.popups.links, function (link, options) {
+	      Popups.attach(context, link, Popups.options(options));
+	    });
+	  }
+	
+	  Popups.attach(context, '.popups', Popups.options({updateMethod: 'none'}));
+	  Popups.attach(context, '.popups-form', Popups.options({updateMethod: 'ajax'})); // ajax reload.
+	  Popups.attach(context, '.popups-form-reload', Popups.options({updateMethod: 'reload'})); // whole page reload.
+	  Popups.attach(context, '.popups-form-noupdate', Popups.options({updateMethod: 'none'}));  // no reload at all.
+	}
 };
 
 // ****************************************************************************
@@ -1173,3 +1178,4 @@ Drupal.theme.prototype.popupTemplate = function(popupId) {
 };
 /*jslint devel: true, onevar: false, browser: true, evil: true, undef: true, maxerr: 50, indent: 2 */
 
+})(jQuery);
\ No newline at end of file
diff --git a/popups.module b/popups.module
index a3211b3..637f27a 100644
--- a/popups.module
+++ b/popups.module
@@ -66,8 +66,10 @@ function popups_init() {
   // Move the page_override flag back out of the session.
   if (isset($_SESSION['page_override'])) {
     // This call will not return on form submission.
-    $content = menu_execute_active_handler();
-
+    $content = menu_execute_active_handler(NULL, FALSE);
+    if (is_array($content)) {
+      $content = drupal_render($content);
+    }
     // The call did return, so it wasn't a form request,
     // so we are returning a result, so clear the session flag.
     $override = $_SESSION['page_override'];
@@ -99,7 +101,7 @@ function popups_form_alter(&$form, $form_state, $form_id) {
   }
 
   // Alter the theme configuration pages, to add a per-theme-content selector.
-  $theme = arg(4);
+  $theme = arg(3);
   if ($form_id == 'system_theme_settings' && $theme) {
     $form['popups'] = array(
       '#type' => 'fieldset',
@@ -133,10 +135,11 @@ function popups_form_alter(&$form, $form_state, $form_id) {
 function popups_render_as_json($content) {
   // Call theme_page so modules like jquery_update can do their thing. We don't
   // really care about the mark up though.
-  $ignore = theme('page', $content);
-
+//TODO: check we this line break the code
+//  theme_html($variables); 
+  //$ignore = theme('page', $content);
   $path = $_GET['q']; // Get current path from params.
-  return drupal_json(array(
+  return drupal_json_output(array(
     'title' => drupal_get_title(),
     'messages' => theme('status_messages'),
     'path' => $path,
@@ -150,33 +153,28 @@ function popups_render_as_json($content) {
  * Get the added JS in a format that is readable by popups.js.
  */
 function popups_get_js() {
-  $js = array_merge_recursive(drupal_add_js(), drupal_add_js(NULL, NULL, 'footer'));
+//TODO: check if we need the second drupal_add_js.
+  $js = array_merge_recursive(drupal_get_js(), drupal_get_js('footer'));
   $query_string = '?'. substr(variable_get('css_js_query_string', '0'), 0, 1);
-
   $popup_js = array();
-
-  foreach ($js as $type => $data) {
+  foreach ($js as $path => $data) {
     if (!$data) continue;
-    switch ($type) {
+    switch ($data['type']) {
       case 'setting':
         // Why not just array_merge_recursive($data);
-        $popup_js['setting'] = call_user_func_array('array_merge_recursive', $data);
+        $popup_js['setting'] = call_user_func_array('array_merge_recursive', $data['data']);  
         break;
       case 'inline':
-        foreach ($data as $info) {
-          $popup_js['inline'][] = '<script type="text/javascript"' . ($info['defer'] ? ' defer="defer"' : '') . '>' . $info['code'] . "</script>\n";
-        }
+        $popup_js['inline'][] = '<script type="text/javascript"' . ((isset($data['defer']) && $data['defer']) ? ' defer="defer"' : '') . '>' . $data['data'] . "</script>\n";
         break;
       default:
-        foreach ($data as $path => $info) {
-          $popup_js[$type][$path] = '<script type="text/javascript"'. ($info['defer'] ? ' defer="defer"' : '') .' src="'. base_path() . $path . $query_string ."\"></script>\n";
-        }
+         $popup_js[$data['type']][$path] = '<script type="text/javascript"'. ((isset($data['defer']) && $data['defer']) ? ' defer="defer"' : '') .' src="'. base_path() . $path . $query_string ."\"></script>\n";
         break;
     }
   }
 
-  unset($popup_js['core']['misc/jquery.js']);
-  unset($popup_js['core']['misc/drupal.js']);
+  unset($popup_js['file']['misc/jquery.js']);
+  unset($popup_js['file']['misc/drupal.js']);
 
   if (module_exists('jquery_update')) {
     foreach (jquery_update_get_replacements() as $type => $replacements) {
@@ -198,48 +196,53 @@ function popups_get_js() {
  * Get the added CSSS in a format that is readable by popups.js.
  */
 function popups_get_css() {
-  $css = drupal_add_css();
+  $css = drupal_get_css();
   $popup_css = array();
 
   $query_string = '?'. substr(variable_get('css_js_query_string', '0'), 0, 1);
 
   // Only process styles added to "all".
   $media = 'all';
-  foreach ($css[$media] as $type => $files) {
-    if ($type == 'module') {
+  $theme_styles = array();
+  foreach ($css as $data) {
+    if ($data['media'] != $media) {
+      continue;
+    }
+    if ($data['group'] == CSS_THEME) {
       // Setup theme overrides for module styles.
-      $theme_styles = array();
-      foreach (array_keys($css[$media]['theme']) as $theme_style) {
-        $theme_styles[] = basename($theme_style);
-      }
+      $theme_styles[] = basename($data['data']);
     }
-    foreach ($css[$media][$type] as $file => $preprocess) {
-      // If the theme supplies its own style using the name of the module style, skip its inclusion.
-      // This includes any RTL styles associated with its main LTR counterpart.
-      if ($type == 'module' && in_array(str_replace('-rtl.css', '.css', basename($file)), $theme_styles)) {
-        // Unset the file to prevent its inclusion when CSS aggregation is enabled.
-        unset($css[$media][$type][$file]);
-        continue;
-      }
-      // Only include the stylesheet if it exists.
-      if (file_exists($file)) {
+  }
+  foreach ($css as $data) {
+    if ($data['media'] != $media) {
+      continue;
+    }
+    // If the theme supplies its own style using the name of the module style, skip its inclusion.
+    // This includes any RTL styles associated with its main LTR counterpart.
+    if ($data['group'] != CSS_THEME && in_array(str_replace('-rtl.css', '.css', basename($data['data'])), $theme_styles)) {
+      continue;
+    }
+    // Only include the stylesheet if it exists.
+    if (file_exists($file)) {
+      switch ($data['group']) {
         // If a CSS file is not to be preprocessed and it's a module CSS file, it needs to *always* appear at the *top*,
         // regardless of whether preprocessing is on or off.
-        if ($type == 'module') {
-          $popup_css['module'][$file] = '<link type="text/css" rel="stylesheet" media="'. $media .'" href="'. base_path() . $file . $query_string .'" />'."\n";
-        }
+        case CSS_DEFAULT:
+        case CSS_SYSTEM:
+          $key = 'module';
+          break;
         // If a CSS file is not to be preprocessed and it's a theme CSS file, it needs to *always* appear at the *bottom*,
         // regardless of whether preprocessing is on or off.
-        elseif ($type == 'theme') {
-          $popup_css['theme'][$file] = '<link type="text/css" rel="stylesheet" media="'. $media .'" href="'. base_path() . $file . $query_string .'" />'."\n";
-        }
-        else {
-          $popup_css['unknown'][$file] = '<link type="text/css" rel="stylesheet" media="'. $media .'" href="'. base_path() . $file . $query_string .'" />'."\n";
-        }
+        case CSS_THEME:
+          $key = 'theme';
+          break;
+        default:
+          $key = 'unknown';
+          break;
       }
+      $popup_css[$key][$data['data']] = '<link type="text/css" rel="stylesheet" media="'. $media .'" href="'. base_path() . $data['data'] . $query_string .'" />'."\n";
     }
   }
-
   return $popup_css;
 }
 
@@ -300,7 +303,7 @@ function popups_add_popups($rules=NULL) {
       }
     }
     if ($added) {
-      drupal_add_js( $settings, 'setting' );
+      drupal_add_js($settings, array('type' => 'setting'));
     }
   }
   if (!$added) {
@@ -378,7 +381,7 @@ function popups_skins($reset = FALSE) {
 function popups_popups_skins() {
   $skins = array();
   $skins_directory = drupal_get_path('module', 'popups') .'/skins';
-  $files = file_scan_directory($skins_directory, '\.css$');
+  $files = file_scan_directory($skins_directory, '/\.css$/');
 
   foreach ($files as $file) {
     $name = drupal_ucfirst($file->name);
diff --git a/popups_test.info b/popups_test.info
index 602cd9d..f3adef4 100644
--- a/popups_test.info
+++ b/popups_test.info
@@ -1,6 +1,5 @@
 name = Popups: Test Page
 description = Test the Popups API.
 package = User interface
-core = 6.x
+core = 7.x
 dependencies[] = popups
-
