Index: moodle/lib/ajax/ajaxcourse.js
--- moodle/lib/ajax/ajaxcourse.js Base (1.9.6.4)
+++ moodle/lib/ajax/ajaxcourse.js Locally Modified (Based On 1.9.6.4)
@@ -48,6 +48,7 @@
     this.leftcolumn = null;
     this.rightcolumn = null;
     this.adminBlock = null;
+    this.tempBlock = null;
     this.icons = [];
     this.marker = null;
 
@@ -115,6 +116,7 @@
     }
 
     this.adminBlock = YAHOO.util.Dom.getElementsByClassName('block_adminblock')[0]; 
+    this.tempBlock = YAHOO.util.Dom.getElementsByClassName('tempblockhandler')[0];
     YAHOO.log("admin - "+this.adminBlock.className);
 }
 
Index: moodle/lib/ajax/block_classes.js
--- moodle/lib/ajax/block_classes.js Base (1.17.6.1)
+++ moodle/lib/ajax/block_classes.js Locally Modified (Based On 1.17.6.1)
@@ -58,7 +58,6 @@
     this.init_buttons();
 }
 
-
 block_class.prototype.startDrag = function(x, y) {
     //operates in intersect mode
     YAHOO.util.DDM.mode = YAHOO.util.DDM.INTERSECT;
@@ -157,25 +156,36 @@
 
         if(this.debug && inserttarget != null)YAHOO.log("moving "+this.getEl().id+" before "+inserttarget.getEl().id+" - parentNode="+this.getEl().parentNode.id);
         
-        if(this == inserttarget){
+        if (this.getEl() == inserttarget) {
             if(this.debug)YAHOO.log("Dropping on self, resetting");
             this.endDrag();
             return;
         }
         
         //remove from document        
-        if(this.getEl().parentNode != null)
+        if (this.getEl().parentNode != null) {
           this.getEl().parentNode.removeChild(this.getEl());     
-                
+        }
         //insert into correct place
-        if(inserttarget != null ){
+        if (inserttarget != null && inserttarget.getEl().parentNode != null) {
             inserttarget.getEl().parentNode.insertBefore(this.getEl(),inserttarget.getEl());
+            positiontoinsert = "before";
+            if (inserttarget.getEl().id != 'rinst0' || inserttarget.getEl().id == 'rinst0'){
+                positiontoinsertid = inserttarget.getEl().id;
+            } else if (main.adminBlock.parentNode.nextSibling != null) {
+                positiontoinsertid = main.adminBlock.parentNode.nextSibling.id;
+            } else {
+                positiontoinsertid = inserttarget.getEl().id;
+            }
             
-        }else if(column == main.rightcolumn){//if right side insert before admin block
-            column.getEl().insertBefore(this.getEl(),main.adminBlock);  
-            
-        }else{ 
+        } else if (column == main.rightcolumn && column.getEl() != null ) {//if right side insert before admin block
+            column.getEl().insertBefore(this.getEl(),main.tempBlock.childNode);            
+            positiontoinsert = "after";
+            positiontoinsertid = main.tempBlock.parentNode;
+        } else {
             column.getEl().appendChild(this.getEl());    
+            positiontoinsert = "after";
+            positiontoinsertid = column.getEl().id;
         }
                
         this.reset_regions();
@@ -183,10 +193,8 @@
         //remove block from current array
         if(main.rightcolumn.has_block(this))
               main.rightcolumn.remove_block(this);
-              
         else if(main.leftcolumn.has_block(this))
               main.leftcolumn.remove_block(this);
-        
         //insert into new array
         column.insert_block(this,inserttarget);
         
@@ -272,7 +280,7 @@
 
 block_class.prototype.updatePosition = function(index, columnId) {
     //update the db for the position
-    main.connectQueue_add('POST', 'class=block&field=position', null,
+    main.connectQueue_add('POST', 'class=block&field=position&positiontoinsert='+positiontoinsert +'&positiontoinsertid='+positiontoinsertid, null,
             'value='+index+'&column='+columnId+'&instanceId='+this.instanceId);
 
     if (this.debug) {
@@ -332,7 +340,7 @@
         var found = -1;
         var tempStore = nextStore = null;
         for(var i=0;i<blockcount;i++){
-            if(found > 0){
+            if(found >= 0){
                 tempStore = this.blocks[i];
                 this.blocks[i] = nextStore;
                 nextStore = tempStore;           

Index: moodle/course/rest.php
--- moodle/course/rest.php Base (1.8.6.2)
+++ moodle/course/rest.php Locally Modified (Based On 1.8.6.2)
@@ -20,8 +20,9 @@
 $summary    = optional_param('summary', '', PARAM_RAW);
 $sequence   = optional_param('sequence', '', PARAM_SEQUENCE);
 $visible    = optional_param('visible', 0, PARAM_INT);
+$positiontoinsert = optional_param('positiontoinsert', '', PARAM_ALPHA);
+$positiontoinsertid = optional_param('positiontoinsertid', '', PARAM_RAW);
 
-
 // Authorise the user and verify some incoming data
 if (!$course = get_record('course', 'id', $courseid)) {
     error_log('AJAX commands.php: Course does not exist');
@@ -62,7 +63,79 @@
                         // We want to move the block around. This means changing
                         // the column (position field) and/or block sort order
                         // (weight field).
-                        blocks_move_block($PAGE, $blockinstance, $column, $value);
+                        $undifinedinsertedid = FALSE;
+                        $isaddednewblock = FALSE;
+
+                        if (!empty($positiontoinsertid) && $positiontoinsertid != 'undefined' && strpos($positiontoinsertid, 'column') === FALSE ) {
+                            $isaddednewblock = (substr($positiontoinsertid,0,1) != 'i') ? TRUE : FALSE;
+                            if ($positiontoinsertid == 'linst0' || $positiontoinsertid == 'rinst0') {
+                               $isaddednewblock = FALSE;
+                            }
+
+                            $positiontoinsertid = clean_param($positiontoinsertid, PARAM_SEQUENCE);
+                        } else if($positiontoinsertid == 'undefined') {
+                            $undifinedinsertedid = TRUE;
+                            $positiontoinsertid = 0;
+                        } else {
+                            $positiontoinsertid = 0;
+                        }
+                        
+                        if ($positiontoinsertid > 0) {
+                            
+                            $instsql = 'SELECT * FROM '. $CFG->prefix .'block_instance WHERE '
+                                .' id = \''. $instanceid .'\' AND position = \''. $column .'\' AND pageId = \''. $courseid .'\'';
+                            $instweight = get_record_sql($instsql);
+
+                            $sql = 'SELECT * FROM '. $CFG->prefix .'block_instance WHERE '
+                                .' id = \''. $positiontoinsertid .'\' AND position = \''. $column .'\' AND pageId = \''. $courseid .'\'';
+                            $targetweight = get_record_sql($sql);
+
+                            //$instweight = get_record("block_instance", 'id', $positiontoinsertid, "position",$column, 'pageId', $courseid);
+                            if (!empty($targetweight->weight) && !empty($instweight->weight)) {
+                                if ($positiontoinsert == "before") {
+                                    if ($targetweight->weight < $instweight->weight || ($instanceid == $positiontoinsertid)) {
+                                        $destweight = ($targetweight->weight == 0 || empty($targetweight->weight)) ? 0 : $targetweight->weight ;
+                                    } else {
+                                        $destweight = ($targetweight->weight == 0 || empty($targetweight->weight)) ? 0 : $targetweight->weight -1 ;
+                                    }
+                                } else if ($positiontoinsert == "after") {
+                                    $destweight = $targetweight->weight + 1;                                    
+                                }
+                            } else {
+                                $destweight = ($targetweight->weight == 0 || empty($targetweight->weight)) ? 0 : $targetweight->weight - 1 ;
+                            }                            
+                        } else {
+                            $sql = 'SELECT max(weight) as weight FROM '. $CFG->prefix .'block_instance WHERE '
+                               .'position = \''. $column .'\' AND pageId = \''. $courseid .'\'';
+                            $instweight = get_record_sql($sql);
+
+                            $countrecords = count_records('block_instance', 'position', $column, 'pageId', $courseid);
+                            $recordexists = record_exists('block_instance', 'position', $column, 'pageId', $courseid, 'id', $instanceid);
+
+                            if ($isaddednewblock || $undifinedinsertedid) {
+                                if (!empty($countrecords)) {
+                                    $destweight = ($recordexists) ? $instweight->weight : $instweight->weight + 1 ;
+                                } else {
+                                    $destweight = 0;
+                                }                                
+                            } else {                   
+                                if (!empty($countrecords)) {
+                                    $destweight = ($recordexists) ? $instweight->weight  : $instweight->weight + 1  ;
+                                } else {
+                                    $destweight = 0;
+                                }
+                            }
+                        }
+                       
+                        blocks_move_block($PAGE, $blockinstance, $column, $destweight);
+                                          
+                        $current_blocks = blocks_get_by_page_pinned($PAGE);
+                        global $COURSE;
+                        foreach ($current_blocks as $pos =>$blocks) {
+                            if (count($current_blocks[$pos]) == 0) {
+                                print_side_block('', '', NULL, NULL, '', array('id'=> $pos.'inst0', 'class'=>'tempblockhandle'), '');                                
+                            }
+                        }
                         break;
                 }
                 break;

Index: moodle/lib/blocklib.php
--- moodle/lib/blocklib.php Base (1.129.2.7)
+++ moodle/lib/blocklib.php Locally Modified (Based On 1.129.2.7)
@@ -254,6 +254,7 @@
 // Parameters passed by reference for speed; they are not modified.
 function blocks_print_group(&$page, &$pageblocks, $position) {
     global $COURSE, $CFG, $USER;
+    $isediting = $page->user_is_editing();
 
     if (empty($pageblocks[$position])) {
         $groupblocks = array();
@@ -262,20 +263,18 @@
         $groupblocks = $pageblocks[$position];
         $maxweight = max(array_keys($groupblocks));
     }
+    if (!empty($CFG->ajaxcapable) && $CFG->ajaxcapable && !empty($COURSE->javascriptportal) && $isediting) {    
+        $COURSE->javascriptportal->currentblocksection = $position;
+        $COURSE->javascriptportal->block_add($position.'inst0', FALSE);
+    }
 
-
     foreach ($groupblocks as $instance) {
         if (!empty($instance->pinned)) {
             $maxweight--;
         }
     }
 
-    $isediting = $page->user_is_editing();
-
-
     foreach($groupblocks as $instance) {
-
-
         // $instance may have ->rec and ->obj
         // cached from when we walked $pageblocks
         // in blocks_have_content()
@@ -365,7 +364,11 @@
         $page->user_is_editing() &&
         ($managecourseblocks || $editmymoodle || $myownblogpage || defined('ADMIN_STICKYBLOCKS'))) {
 
+        print_side_block(NULL,NULL, NULL, NULL, NULL, array('id'=> BLOCK_POS_RIGHT.'inst0', 'class'=>'tempblockhandler'));
         blocks_print_adminblock($page, $pageblocks);
+    }else if ($page->user_is_editing() &&
+        ($managecourseblocks || $editmymoodle || $myownblogpage || defined('ADMIN_STICKYBLOCKS'))){
+        print_side_block(NULL,NULL, NULL, NULL, NULL, array('id'=> BLOCK_POS_LEFT.'inst0', 'class'=>'tempblockhandler'));
     }
 }
 
@@ -558,27 +561,32 @@
             blocks_delete_instance($instance, $pinned);
         break;
         case 'moveup':
-            if(empty($instance))  {
+            if (empty($instance))  {
                 error('Invalid block instance for '. $blockaction);
             }
 
-            if($instance->weight == 0) {
+            if ($instance->weight == 0) {
                 // The block is the first one, so a move "up" probably means it changes position
                 // Where is the instance going to be moved?
                 $newpos = $page->blocks_move_position($instance, BLOCK_MOVE_UP);
                 $newweight = (empty($pageblocks[$newpos]) ? 0 : max(array_keys($pageblocks[$newpos])) + 1);
 
                 blocks_execute_repositioning($instance, $newpos, $newweight, $pinned);
-            }
-            else {
+            } else {
                 // The block is just moving upwards in the same position.
                 // This configuration will make sure that even if somehow the weights
                 // become not continuous, block move operations will eventually bring
                 // the situation back to normal without printing any warnings.
-                if(!empty($pageblocks[$instance->position][$instance->weight - 1])) {
-                    $other = $pageblocks[$instance->position][$instance->weight - 1];
+                if (!empty($pageblocks[$instance->position][$instance->weight - 1])) {
+                    //define instance's position in the array
+                    foreach ($pageblocks[$instance->position] as $instancekeysindex => $index ){
+                        if ($pageblocks[$instance->position][$instancekeysindex]->blockid == $instance->blockid){
+                            $instanceindex = $instancekeysindex;
                 }
-                if(!empty($other)) {
+                    }
+                    $other = $pageblocks[$instance->position][$instanceindex - 1];
+                }
+                if (!empty($other)) {
                     ++$other->weight;
                     if (!empty($pinned)) {
                         update_record('block_pinned', $other);
@@ -595,11 +603,10 @@
             }
         break;
         case 'movedown':
-            if(empty($instance))  {
+            if (empty($instance))  {
                 error('Invalid block instance for '. $blockaction);
             }
-
-            if($instance->weight == max(array_keys($pageblocks[$instance->position]))) {
+            if ($instance->weight == max(array_keys($pageblocks[$instance->position]))) {
                 // The block is the last one, so a move "down" probably means it changes position
                 // Where is the instance going to be moved?
                 $newpos = $page->blocks_move_position($instance, BLOCK_MOVE_DOWN);
@@ -612,10 +619,16 @@
                 // This configuration will make sure that even if somehow the weights
                 // become not continuous, block move operations will eventually bring
                 // the situation back to normal without printing any warnings.
-                if(!empty($pageblocks[$instance->position][$instance->weight + 1])) {
-                    $other = $pageblocks[$instance->position][$instance->weight + 1];
+                if (!empty($pageblocks[$instance->position][$instance->weight + 1])) {
+                    //define instance's position in the array
+                    foreach ($pageblocks[$instance->position] as $instancekeysindex => $index ){
+                        if ($pageblocks[$instance->position][$instancekeysindex]->blockid == $instance->blockid){
+                            $instanceindex = $instancekeysindex;
                 }
-                if(!empty($other)) {
+                    }
+                    $other = $pageblocks[$instance->position][$instanceindex + 1];
+                }                
+                if (!empty($other)) {
                     --$other->weight;
                     if (!empty($pinned)) {
                         update_record('block_pinned', $other);
@@ -635,11 +648,15 @@
             if(empty($instance))  {
                 error('Invalid block instance for '. $blockaction);
             }
-
             // Where is the instance going to be moved?
             $newpos = $page->blocks_move_position($instance, BLOCK_MOVE_LEFT);
-            $newweight = (empty($pageblocks[$newpos]) ? 0 : max(array_keys($pageblocks[$newpos])) + 1);
+            $newweight = 0;
 
+            if (!empty($pinned) && !empty($pageblocks[$newpos]) ){
+                $newweight = $pageblocks[$newpos][max(array_keys($pageblocks[$newpos])) ]->weight + 1;            
+            } else if(!empty($pageblocks[$newpos]) && (!array_key_exists('pinned', $pageblocks[$newpos][max(array_keys($pageblocks[$newpos]))])) ){
+                $newweight = $pageblocks[$newpos][max(array_keys($pageblocks[$newpos])) ]->weight + 1;                
+            }            
             blocks_execute_repositioning($instance, $newpos, $newweight, $pinned);
         break;
         case 'moveright':
@@ -649,8 +666,13 @@
 
             // Where is the instance going to be moved?
             $newpos    = $page->blocks_move_position($instance, BLOCK_MOVE_RIGHT);
-            $newweight = (empty($pageblocks[$newpos]) ? 0 : max(array_keys($pageblocks[$newpos])) + 1);
+            $newweight = 0; 
 
+            if (!empty($pinned) && !empty($pageblocks[$newpos]) ){
+                $newweight = $pageblocks[$newpos][max(array_keys($pageblocks[$newpos])) ]->weight + 1;
+            }else if(!empty($pageblocks[$newpos]) && (!array_key_exists('pinned', $pageblocks[$newpos][max(array_keys($pageblocks[$newpos]))])) ){
+                $newweight = $pageblocks[$newpos][max(array_keys($pageblocks[$newpos])) ]->weight + 1;
+            }
             blocks_execute_repositioning($instance, $newpos, $newweight, $pinned);
         break;
         case 'add':
@@ -746,7 +768,7 @@
 
     // Close the weight gap we 'll leave behind
     if (!empty($pinned)) {
-        $sql = 'UPDATE '. $CFG->prefix .'block_instance SET weight = weight - 1 '.
+        $sql = 'UPDATE '. $CFG->prefix .'block_pinned SET weight = weight - 1 '.
                         'WHERE pagetype = \''. $instance->pagetype.
                         '\' AND position = \'' .$instance->position.
                         '\' AND weight > '. $instance->weight;
@@ -799,7 +821,7 @@
     // First we close the gap that will be left behind when we take out the
     // block from it's current column.
     if ($pinned) {
-        $closegapsql = "UPDATE {$CFG->prefix}block_instance
+        $closegapsql = "UPDATE {$CFG->prefix}block_pinned
                            SET weight = weight - 1
                          WHERE weight > '$instance->weight'
                            AND position = '$instance->position'
@@ -818,7 +840,7 @@
 
     // Now let's make space for the block being moved.
     if ($pinned) {
-        $opengapsql = "UPDATE {$CFG->prefix}block_instance
+        $opengapsql = "UPDATE {$CFG->prefix}block_pinned
                            SET weight = weight + 1
                          WHERE weight >= '$destweight'
                            AND position = '$destpos'



--- styles_layout.css	2009/12/17 08:20:35	1.516.2.81
+++ styles_layout.css	2010/01/20 06:37:51	1.516.2.82
@@ -1452,6 +1452,12 @@
  padding:3px;
 }
 
+.tempblockhandler, .tempblockhandler .content{
+    height: 3px;
+    margin-bottom: -3px;
+    visibility: hidden;
+}
+
 /***
  *** Blogs
  ***
