--- nodeorder.module.orig	2009-05-26 20:42:00.000000000 +0200
+++ nodeorder.module	2009-10-07 23:35:29.000000000 +0200
@@ -497,6 +497,7 @@ function nodeorder_move_in_category($dir
   
   // Get the current weight for the node
   $weight = db_result(db_query("SELECT weight_in_tid FROM {term_node} WHERE nid = %d AND tid = %d", $node->nid, $tid));
+  $lang_filter = ($node->language)? sprintf(" AND n.language = '%s'", $node->language) : "";
   
   if ($up) {
     $weights = nodeorder_get_term_min_max($tid, FALSE);
@@ -506,7 +507,7 @@ function nodeorder_move_in_category($dir
       return;
     }
   
-    $sql = 'SELECT DISTINCT(n.nid), n.vid, tn.weight_in_tid FROM {node} n INNER JOIN {term_node} tn ON n.vid = tn.vid WHERE tn.tid = %d AND n.status = 1 AND tn.weight_in_tid <= %d ORDER BY tn.weight_in_tid DESC';
+    $sql = 'SELECT DISTINCT(n.nid), n.vid, tn.weight_in_tid FROM {node} n INNER JOIN {term_node} tn ON n.vid = tn.vid WHERE tn.tid = %d AND n.status = 1 AND tn.weight_in_tid <= %d' .$lang_filter. ' ORDER BY tn.weight_in_tid DESC';
     $direction = 'up';
   }
   else {
@@ -517,7 +518,7 @@ function nodeorder_move_in_category($dir
       return;
     }
   
-    $sql = 'SELECT DISTINCT(n.nid), n.vid, tn.weight_in_tid FROM {node} n INNER JOIN {term_node} tn ON n.vid = tn.vid WHERE tn.tid = %d AND n.status = 1 AND tn.weight_in_tid >= %d ORDER BY tn.weight_in_tid';
+    $sql = 'SELECT DISTINCT(n.nid), n.vid, tn.weight_in_tid FROM {node} n INNER JOIN {term_node} tn ON n.vid = tn.vid WHERE tn.tid = %d AND n.status = 1 AND tn.weight_in_tid >= %d' .$lang_filter. ' ORDER BY tn.weight_in_tid';
     $direction = 'down';
   }
   
@@ -537,6 +538,9 @@ function nodeorder_move_in_category($dir
   
   db_query($sql, $node1->weight_in_tid, $node2->nid, $tid);
   db_query($sql, $node2->weight_in_tid, $node1->nid, $tid);
+  // i18n sync
+  nodeorder_i18nsync_weights($node1->weight_in_tid, $node2->nid, $tid);
+  nodeorder_i18nsync_weights($node2->weight_in_tid, $node1->nid, $tid);
 
   $term = taxonomy_get_term($tid);
   drupal_set_message(t("<em>%title</em> was moved $direction in %category...", array('%title' => $node->title, '%category' => $term->name)));
@@ -678,15 +682,20 @@ function nodeorder_term_can_be_ordered($
  * Implementation of hook_nodeapi().
  */
 function nodeorder_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
+  global $i18nsync; // This variable will be true when a sync operation is in progress.
+  
   switch ($op) {
     case 'presave':
       if (nodeorder_can_be_ordered($node)) {    
         if (!isset($node->nodeorder)) {
           $node->nodeorder = array();
   
+          // When synchronization is in progress, get the weights from source translation
+          $nid = (_nodeorder_i18nsync_avail($node) && $i18nsync)? $node->tnid : $node->nid;
+          
           // When a node gets loaded, store an element called 'nodeorder' that contains
           // an associative array of tid to weight_in_tid...
-          $result = db_query('SELECT tid, weight_in_tid FROM {term_node} WHERE nid = %d', $node->nid);
+          $result = db_query('SELECT tid, weight_in_tid FROM {term_node} WHERE nid = %d', $nid);
           while ($term_node = db_fetch_object($result)) {
             $node->nodeorder[$term_node->tid] = $term_node->weight_in_tid;
           }
@@ -719,11 +728,13 @@ function nodeorder_nodeapi(&$node, $op,
       // are in orderable vocabularies...
       if (nodeorder_can_be_ordered($node)) {
         $tids = nodeorder_orderable_tids($node);
-  
-        if (count($tids) > 0) {
+        $newtids = array_diff($tids, array_keys($node->nodeorder));
+		
+        // update new terms only
+        if (count($newtids) > 0 && !$i18nsync) {
           $sql = "UPDATE {term_node} SET weight_in_tid = %d WHERE tid = %d AND nid = %d";
   
-          foreach ($tids as $i => $tid) {
+          foreach ($newtids as $i => $tid) {
             db_lock_table('term_node');
             $weights = nodeorder_get_term_min_max($tid, FALSE); // get the cached weights
             db_query($sql, $weights["max"] + 1, $tid, $node->nid);
@@ -884,3 +895,40 @@ function nodeorder_help($path, $arg) {
       return $output;
   }
 }
+
+/**
+ * Synchronize 'weight_in_tid' values for all node translations for one 'tid'
+ */
+function nodeorder_i18nsync_weights($weight, $nid, $tid) {
+	$node = node_load($nid);
+
+	if (_nodeorder_i18nsync_avail($node) && ($translations = translation_node_get_translations($node->tnid))) {
+		$nids = array();
+		foreach ($translations as $trnode) {
+			// do not process current node
+          	if ($node->nid != $trnode->nid) {
+           		$nids[] = $trnode->nid;            		
+           	}
+       	}
+       	//dpm($nids);
+       	if (!empty($nids)) {
+       		$sql = "UPDATE {term_node} SET weight_in_tid = %d WHERE tid = %d AND nid IN (%s)";
+       		$nidslist = implode(',', $nids);
+       		db_query($sql, $weight, $tid, $nidslist);
+       	}
+	}
+}
+
+/**
+ * Check if i18nsync is available for the node and taxonomy
+ */
+function _nodeorder_i18nsync_avail($node) {
+	if (module_exists('i18nsync') && module_exists('i18ntaxonomy')) {
+		// could be also checked against (i18ntaxonomy_vocabulary($vid) == I18N_TAXONOMY_LOCALIZE)
+		if (!empty($node->tnid) && ($fields = i18nsync_node_fields($node->type))) {
+			// taxonomy is selected to be synchronized
+			if (in_array('taxonomy', $fields)) return TRUE;
+		}
+	}
+	return FALSE;
+}
