diff --git a/node_embed.module b/node_embed.module
index 99e0be2..5fb3379 100644
--- a/node_embed.module
+++ b/node_embed.module
@@ -1,15 +1,17 @@
-<?php
+<?php 
+// $Id: node_embed.module,v 1.6 2011/02/07 17:03:47 jec006 Exp $
+
 /**
- * @file
- * This module defines an input filter for taking an embed code syntax
- * ([[nid: ###]]) and removing the embed code and replacing with a rendered
- * node view at that position in the text field.
+ * This module defines an input filter for taking an embed code syntax ([[nid: ###]])
+ * and removing the embed code and replacing with a rendered node view at that position
+ * in the text field. *
  */
 
 /**
- * Implements hook_filter_info().
+ * Implementation of hook_filter_info()
  */
 function node_embed_filter_info() {
+
   $filters['node_embed'] = array(
     'title' => t('Insert node'),
     'description' => t('By including the syntax [[nid:(node_id)]], this filter will embed the node with given NID'),
@@ -20,138 +22,158 @@ function node_embed_filter_info() {
   );
 
   return $filters;
-}
+
+} // node_embed_filter_info
 
 /**
- * Prepare callback for hook_filter.
+ * Prepare callback for hook_filter
  */
 function node_embed_filter_node_embed_prepare($text, $filter, $format, $langcode, $cache, $cache_id) {
+
   return $text;
-}
+
+} // node_embed_filter_node_embed_prepare
 
 /**
- * Process callback for hook_filter.
+ * Process callback for hook_filter
  */
 function node_embed_filter_node_embed_process($text, $filter, $format, $langcode, $cache, $cache_id) {
-  return preg_replace_callback('/\[\[nid:(\d+)(\s.*)?\]\]/', '_node_embed_replacements', $text);
-}
+
+  return preg_replace_callback('/\[\[nid:(\d+)(\s.*)?\]\]/', '_node_make_replacements', $text);
+
+} // node_embed_filter_node_embed_process
 
 /**
- * Tips callback for hook_filter.
+ * Tips callback for hook_filter
  */
 function node_embed_filter_node_embed_tips($filter, $format, $long) {
+  
   return t('[[nid:123]] - Insert a node content');
-}
 
+} // node_embed_filter_node_embed_tips
+  
 /**
  * Provides the replacement html to be rendered in place of the embed code.
- *
  * Does not handle nested embeds.
  *
- * @param array $matches
- *   numeric node id that has been captured by preg_replace_callback.
- *
- * @return string
- *   The rendered HTML replacing the embed code.
- */
-function _node_embed_replacements($matches) {
+ * @param $matches
+ *    numeric node id that has been captured by preg_replace_callback
+ * @return
+ *    The rendered HTML replacing the embed code
+ */ 
+function _node_make_replacements($matches) {
+
   $node = node_load($matches[1]);
 
   if ($node == FALSE || !node_access('view', $node) || !$node->status) {
+    
     return "[[nid:{$matches[1]}]]";
-  }
+
+  } // if
   else {
+
     $node->node_embed_parameters = array();
 
     if (isset($matches[2]) && trim($matches[2]) != '') {
       parse_str(trim(str_replace('&nbsp;', ' ', $matches[2])), $node->node_embed_parameters);
-    }
+    } // if
 
     if (!isset($node->node_embed_parameters['view_mode'])) {
       $node->node_embed_parameters['view_mode'] = 'node_embed';
-    }
+    } // if
+
+     
+    $node->body['und'][0]['value'] = strstr($node->body['und'][0]['value'], $matches[0]);
+      
+
 
     $view = node_view($node, $node->node_embed_parameters['view_mode'], NULL);
+
     $render = drupal_render($view);
 
     return $render;
-  }
-}
+
+  } // else
+
+} // _node_make_replacements
 
 /**
- * Implements hook_theme_registry_alter().
- *
- * This is where we add our default template for the fckeditor view page
- * template.
+ * Implements hook_theme_registry_alter()
+ * This is where we add our default template for the fckeditor view page template.
  */
 function node_embed_theme_registry_alter(&$theme_registry) {
-
-  // Add 'html--ckeditor-node-embed.tpl.php' template file
-  $theme_registry['html__ckeditor_node_embed'] = array();
+  
+  //Add 'html--ckeditor-node-embed.tpl.php' template file
+  $theme_registry['html__ckeditor_node_embed'] = Array();
   $theme_registry['html__ckeditor_node_embed']['template'] = 'html--ckeditor-node-embed';
   $theme_registry['html__ckeditor_node_embed']['path'] = drupal_get_path('module', 'node_embed') . "/theme";
   $theme_registry['html__ckeditor_node_embed']['render element'] = 'page';
   $theme_registry['html__ckeditor_node_embed']['base hook'] = 'html';
   $theme_registry['html__ckeditor_node_embed']['type'] = 'theme_engine';
   $theme_registry['html__ckeditor_node_embed']['theme path'] = path_to_theme();
-  $theme_registry['html__ckeditor_node_embed']['preprocess functions'] = array();
-  $theme_registry['html__ckeditor_node_embed']['process functions'] = array();
+  $theme_registry['html__ckeditor_node_embed']['preprocess functions'] = Array();
+  $theme_registry['html__ckeditor_node_embed']['process functions'] = Array();
 
-  // Add 'page--ckeditor-node-embed.tpl.php' template file
-  $theme_registry['page__ckeditor_node_embed'] = array();
+  //Add 'page--ckeditor-node-embed.tpl.php' template file
+  $theme_registry['page__ckeditor_node_embed'] = Array();
   $theme_registry['page__ckeditor_node_embed']['template'] = 'page--ckeditor-node-embed';
   $theme_registry['page__ckeditor_node_embed']['path'] = drupal_get_path('module', 'node_embed') . "/theme";
   $theme_registry['page__ckeditor_node_embed']['render element'] = 'page';
   $theme_registry['page__ckeditor_node_embed']['base hook'] = 'page';
   $theme_registry['page__ckeditor_node_embed']['type'] = 'theme_engine';
   $theme_registry['page__ckeditor_node_embed']['theme path'] = path_to_theme();
-  $theme_registry['page__ckeditor_node_embed']['preprocess functions'] = array();
-  $theme_registry['page__ckeditor_node_embed']['process functions'] = array();
-}
+  $theme_registry['page__ckeditor_node_embed']['preprocess functions'] = Array();
+  $theme_registry['page__ckeditor_node_embed']['process functions'] = Array();
+  
+} // node_embed_theme_registry_alter
 
 /**
  * Make compatible with views 2 for default view.
  */
 function node_embed_views_api() {
+
   return array('api' => 3);
-}
+
+} // node_embed_views_api
 
 /**
- * Implements hook_views_pre_render().
+ * Implementation of hook_views_pre_render() {
  */
 function node_embed_views_pre_render(&$view) {
-  if (arg(0) == 'ckeditor-node-embed') {
+
+  if ($view->name == 'ckeditor_node_embed' && $view->current_display == 'page_1') {
+
     node_embed_suppress_admin_menu();
-    drupal_add_js("var oEditor = window.parent.CKEDITOR;
-      var instance = oEditor.currentInstance;
-      var lang = oEditor.lang;
-      var config = oEditor.config;",
-      'inline'
-    );
-    drupal_add_js(drupal_get_path('module', 'node_embed') . '/ckeditor/ck_nodeembed.js');
-  }
-}
+
+  } // if
+
+} // node_embed_views_pre_render
+
 
 /**
- * Implements hook_views_default_views().
+ * Implementation of hook_views_default_views().
  */
 function node_embed_views_default_views() {
-  $views = array();
 
+  $views = array();
+  
   if (module_exists('ckeditor') || module_exists('wysiwyg')) {
-    include drupal_get_path('module', 'node_embed') . '/ckeditor/ckeditor_node_embed.view.inc';
-    if (isset($view)) {
-      $views[$view->name] = $view;
-    }
-  }
+
+    $pathCK = drupal_get_path('module', 'node_embed') . '/ckeditor/ckeditor_node_embed.view.inc';
+    include_once($pathCK);
+    $views[$view->name] = $view;
+
+  } // if
 
   return $views;
-}
+
+} // node_embed_views_default_views
 
 /**
- * Implements hook_views_data_alter().
+ * Implements hook_views_data_alter()
  */
-function node_embed_views_data_alter(&$data) {
+function node_embed_views_data_alter(&$data)  {
+
   $data['views']['node_embed'] = array(
     'title' => t('Node embed add area'),
     'help' => t('Provide links to add nodes.'),
@@ -159,51 +181,87 @@ function node_embed_views_data_alter(&$data) {
       'handler' => 'views_handler_node_embed_add_area',
     ),
   );
-}
+
+} // node_embed_views_data_alter
 
 /**
- * Implements hook_form_alter().
- *
- * Add a validation handler to nodes with node_embed.
+ * Implement hook_form_alter()
+ * add a validation handler to nodes with node_embed.
  */
 function node_embed_form_alter(&$form, &$form_state, $form_id) {
+
   $form['#validate'][] = 'node_embed_validate';
-}
+
+} // node_embed_form_alter
 
 /**
- * Validation for the node_embed filter.
- *
- * We do not allow nodes to embed in themselves.
+ * validation for the node_embed filter.
+ * we do not allow nodes to embed in themselves.
  * results in segment fault.
  */
 function node_embed_validate($node, $form) {
+
+  $embed = array();
+  $buff = array();
+  $embed_number = array();
   if (isset($form['values']['nid'])) {
+
     $nid = $form['values']['nid'];
-    $needle = "[[nid:{$nid}]]";
+
+    $needle = "[[nid:{$nid}]]";    
+
     $num = 0;
     $language = (isset($form['values']['language']) && $form['values']['language']) ? $form['values']['language'] : LANGUAGE_NONE;
 
     while (isset($form['values']['body'][$language][$num])) {
-      $found = strpos($form['values']['body'][$language][$num]['value'], $needle);
 
-      if ($found !== FALSE) {
+      $found = strpos($form['values']['body'][$form['values']['language']][$num]['value'], $needle);
+  
+      if ($found == TRUE) {
         form_set_error('edit-body', t('A node is not allowed to embed in itself.'));
-      }
-
+      } // if
+      // $embed = explode('[[',$form['values']['body'][$form['values']['language']][$num]['value']);
+      
+      // foreach ($embed as $key => $value) {
+      //   if(strstr($value,'nid:')) {
+      //     $buff = explode(']]',$value);
+      //     $buff = $buff[0];
+      //     $buff = substr($buff, 4, 1);
+      //     array_push($embed_number,$buff);
+      //   }
+      //   else {
+      //     unset($embed[$key]);
+      //   }
+      // }
+
+      // foreach ($embed_number as $key => $value) {
+
+      //   $node = node_load($value);
+      //   if(strstr($node->body['und'][0]['value'],$needle)) {
+      //             form_set_error('edit-body', t('Ty jebana cioto'));
+      //   }
+
+      // }
+
+
+      dsm($embed,'embed');    
+      dsm($embed_number,'embed_number');
       $num++;
-    }
-  }
-}
+
+
+    } // while
+
+  } // if
+
+} // node_embed_validate
 
 /**
- * Implementing the Wysiwyg API.
- *
+ * Implementing the Wysiwyg API
  * Register a directory containing Wysiwyg plugins.
  *
- * @param string $type
+ * @param $type
  *   The type of objects being collected: either 'plugins' or 'editors'.
- *
- * @return string
+ * @return
  *   A sub-directory of the implementing module that contains the corresponding
  *   plugin files. This directory must only contain integration files for
  *   Wysiwyg module.
@@ -215,101 +273,62 @@ function node_embed_wysiwyg_include_directory($type) {
       // You can just return $type, if you place your Wysiwyg plugins into a
       // sub-directory named 'plugins'.
       return $type;
-  }
-}
+  } // switch
+
+} // node_embed_wysiwyg_include_directory
 
 /**
- * Implements hook_page_build().
+ *  Implementation of hook_init - attach the needed css files if we're on a form page
  */
-function node_embed_page_build() {
-  drupal_add_css(drupal_get_path('module', 'node_embed') . '/plugins/node_embed/node_embed.css');
-}
+function node_embed_init() {
+
+	drupal_add_css(drupal_get_path('module', 'node_embed') . '/plugins/node_embed/node_embed.css');	
+
+} // node_embed_init
 
 /**
  * Implements hook_entity_info_alter().
  */
 function node_embed_entity_info_alter(&$entity_info) {
+
   if (isset($entity_info['node'])) {
+
     $entity_info['node']['view modes'] += array(
       'node_embed' => array(
         'label' => 'Node embed',
         'custom settings' => FALSE,
       ),
     );
-  }
-}
+
+  } // if
+
+} // node_embed_entity_info_alter
 
 /**
  * Implements template_preproccess_node().
  */
 function node_embed_preprocess_node(&$variables) {
+
   if ($variables['view_mode'] == 'node_embed') {
+
     $node = $variables['node'];
     $variables['theme_hook_suggestions'][] = 'node__node_embed';
     $variables['theme_hook_suggestions'][] = 'node__' . $node->type . '__node_embed';
-  }
-}
+
+  } // if
+
+} // node_embed_preprocess_node
 
 /**
  * Suppress Admin Menu if it is present
  */
 function node_embed_suppress_admin_menu() {
+
   if (module_exists('admin_menu')) {
-    admin_menu_suppress();
-  }
-}
 
-/**
- * Implements hook_admin_paths().
- *
- * Show the admin theme in the wysiwyg popup.
- */
-function node_embed_admin_paths() {
-  $paths = array();
-  if (variable_get('node_admin_theme', FALSE)) {
-    $paths['ckeditor-node-embed'] = TRUE;
-    $paths['ckeditor-node-embed/*'] = TRUE;
-  }
-  return $paths;
-}
+    admin_menu_suppress();
 
-/**
- * Implements of hook_ckeditor_plugin().
- */
-function node_embed_ckeditor_plugin() {
-  return array(
-    'NodeEmbed' => array(
-      'name' => 'NodeEmbed',
-      'desc' => t('Node Embed - embed nodes in content.'),
-      'path' => drupal_get_path('module', 'node_embed') .'/ckeditor/NodeEmbed/',
-    ),
-  );
-}
+  } // if
 
-/**
- *  Implements hook_requirements().
- */
-function node_embed_requirements($phase) {
-  $t = get_t();
-  $requirements = array();
-
-  if (module_exists('ckeditor')) {
-    $requirements['node_embed_iframedialog'] = array(
-      'title' => $t('Node Embed - CKEditor IFrame Dialog Field plugin'),
-      'value' => $t('Exists'),
-      'severity' => REQUIREMENT_OK,
-    );
+} // node_embed_suppress_admin_menu
 
-    $plugin = DRUPAL_ROOT . ckeditor_library_path() . '/ckeditor/plugins/iframedialog/plugin.js';
-    if (!file_exists($plugin)) {
-      $requirements['node_embed_iframedialog']['description'] = $t('Node embed requires the CKEditor IFrame Dialog Field plugin. Please download it from !link and extract it into %path.', array(
-        '!link' => l('http://ckeditor.com/addon/iframedialog', 'http://ckeditor.com/addon/iframedialog'),
-        '%path' => dirname($plugin),
-      ));
-      $requirements['node_embed_iframedialog']['value'] = $t('Not found');
-      $requirements['node_embed_iframedialog']['severity'] = REQUIREMENT_ERROR;
-    }
-  }
-
-  return $requirements;
-}
