<?xml version="1.0" encoding="UTF-8"?>
<rmml>

<composition name="rnmc_stdDraglist" class="rnmc_stdMediaGroup">
<script><![CDATA[

// ======================================================================
//       настройки
// ======================================================================
//  порядок выстраивания объектов (vertical, horizontal)
  this.order = AUTOATTACH_BOTTOM_LEFT; //AUTOATTACH_RIGHT_TOP;
//  момент проверки правильности устаноки объекта (after, immediately)
  this.checkMode = DRAGGING_CHECK_IMMEDIATELY; //DRAGGING_CHECK_AFTER;

//  чувствительность по умолчанию
  this.sensitivity = 20;

//  скорость движения
  this.flowdelta = 15;
  
//  порядок выстраивания
  this.showOrder = null;

//  смещение элементов друг от друга
  this.offsetX = 0;
  this.offsetY = 0;
  
//  max щирина и высота из элементов объекта
  this.maxWidth = 0;
  this.maxHeight = 0;
  
//  массив переставляемых элементов объекта
  this.draglist = null;

//  границы рабочей области
/*  this.minDraggingBoundX = 0;
  this.maxDraggingBoundX = Player.module.scenes.width;
  this.minDraggingBoundY = 0;
  this.maxDraggingBoundY = Player.module.scenes.height;*/

  function initFromNode(srcNode, useDelayLoad){
    var xmlNode = null;
    var xmlAttributeMap = null;
    var xmlAttribute = null;
    this.buttons = new Array();  
    this.draglist = new Array();

    xmlAttributeMap = srcNode.attributes;
    if (xmlAttributeMap){
      xmlAttribute = xmlAttributeMap.getNamedItem("id");
      if (xmlAttribute != null && xmlAttribute.specified){
        this.id = xmlAttribute.value;
      }
      xmlAttribute = xmlAttributeMap.getNamedItem("visible");
      if (xmlAttribute != null && xmlAttribute.specified){
        if(xmlAttribute.value.toLowerCase() == "true")
          this.visibility = OBJECT_VISIBILITY_TRUE;
        else if(xmlAttribute.value.toLowerCase() == "false")
          this.visibility = OBJECT_VISIBILITY_FALSE;
      }
    }
    
    xmlNode = srcNode.firstChild;

    while(xmlNode){

      if(xmlNode.nodeName == "param"){

      // разбор стандартных настроек
        this.parseParamNode(xmlNode);

      // разбор специфичных настроек
        xmlAttributeMap = xmlNode.attributes;
        if (xmlAttributeMap){
        //  режим проверки
          xmlAttribute =  xmlAttributeMap.getNamedItem("checkMode");
          if (xmlAttribute != null && xmlAttribute.specified){
            if(xmlAttribute.value.toLowerCase() == "after")
              this.checkMode = DRAGGING_CHECK_AFTER;
          };
        //  алгоритм подсчета результатов работы с объектом
          xmlAttribute =  xmlAttributeMap.getNamedItem("countingStyle");
          if (xmlAttribute != null && xmlAttribute.specified){
            switch(xmlAttribute.value.toLowerCase()){
              case "no":
                this.countingStyle = COUNTING_RESULTS_NO;
                break;
              case "discrete":
              // 100% OR 0%
                this.countingStyle = COUNTING_RESULTS_DISCRETE;
                break;
              case "smooth":
              // right / RIGHT
                this.countingStyle = COUNTING_RESULTS_SMOOTH;
                break;
              case "smooth_err":
              // right = 0 -> 0%
              // right = RIGHT + WRONG -> 0% и сообщение "Напрасно Вы все скопом..."
              // (right - wrong + WRONG) / (RIGHT + WRONG)              
                this.countingStyle = COUNTING_RESULTS_SMOOTH_ERR;
                break;
              case "attempts":
              // right / (right + wrong)
              // wrong - включает в себя errors
                this.countingStyle = COUNTING_RESULTS_ATTEMPTS;
            }
              
              
              
          };
        //  горизонтальное либо вертикальное выстраивание
          xmlAttribute =  xmlAttributeMap.getNamedItem("order");
          if (xmlAttribute != null && xmlAttribute.specified){
            if(xmlAttribute.value.toLowerCase() == "horizontal"){
              this.order = AUTOATTACH_RIGHT_TOP;
          };
          xmlAttribute =  xmlAttributeMap.getNamedItem("offsetX");
              if (xmlAttribute != null && xmlAttribute.specified)
                this.offsetX = parseInt(xmlAttribute.value);
          xmlAttribute =  xmlAttributeMap.getNamedItem("offsetY");
          if (xmlAttribute != null && xmlAttribute.specified)
            this.offsetY = parseInt(xmlAttribute.value);
          }
        }
      } else if(xmlNode.nodeName == "actions"){
        this.parseActionsNode(xmlNode);

        this.onCompleteActions =
          scene.Utils.parseActionsNode(xmlNode, "onComplete");
        this.onWrongCompleteActions =
          scene.Utils.parseActionsNode(xmlNode, "onWrongComplete");
        this.onPlacedRightActions =
          scene.Utils.parseActionsNode(xmlNode, "onPlacedRight");
        this.onPlacedWrongActions =
          scene.Utils.parseActionsNode(xmlNode, "onPlacedWrong");
      } else if(xmlNode.nodeName == "effects"){
        this.parseEffectsNode(xmlNode);
      } else if(xmlNode.nodeName == "objectives"){
        this.parseObjectivesNode(xmlNode);
      } else if(xmlNode.nodeName == "conditions"){
        var conditionNode = xmlNode.firstChild;
        while(conditionNode){
          if(conditionNode.nodeName == "condition"){
            this.parseConditionNode(conditionNode);
          }
          conditionNode = conditionNode.nextSibling;
        }
        this.parseConditionNode(xmlNode);
      } else if(xmlNode.nodeName == "properties"){
        this.parsePropertiesNode(xmlNode);
      }
      
      xmlNode = xmlNode.nextSibling;
    }

    this.loadChildsObject(srcNode)

    for(var i = 0; i < this.childObjects.length; i++){
    //  создаем массив кнопок, т.к. они не таскаются по определению
      if(this.childObjects[i].type == "rnmc_stdButton")
        this.buttons.push(this.childObjects[i]);
      else{
      //  создаем массив переставляемых элементов        
        if(this.objectWidth > 0)
          if(this.childObjects[i].type == "rnmc_stdText" &&
          (this.childObjects[i].objectWidth > this.objectWidth || this.childObjects[i].objectWidth == 0))
            this.childObjects[i].objectWidth = this.objectWidth;
        this.draglist.push(this.childObjects[i]);
      }
    }    

    return true;
  }

  function showObject(){
    if(this.buttons)
      for(var i = 0; i < this.buttons.length; i++)
        if(this.buttons[i].visibility != OBJECT_VISIBILITY_FALSE)
          this.buttons[i].showObject();
    
    if(this.draglist){          
      //  в случайном порядке
      var tempSequence = new Array();    
      for(var i=0; i<this.draglist.length; i++)
        tempSequence.push(i);    
      this.showOrder = scene.Utils.permutateArray(tempSequence);
      tempSequence = null;
      
      for(var i=0; i<this.showOrder.length; i++){
        if(i>0){
          this.draglist[this.showOrder[i]].autoAttachTarget = this.draglist[this.showOrder[i-1]].id;
        }
        this.draglist[this.showOrder[i]].index = i;
        this.draglist[this.showOrder[i]].autoAttachOffsetX = this.offsetX;
        this.draglist[this.showOrder[i]].autoAttachOffsetY = this.offsetY;
        this.draglist[this.showOrder[i]].autoAttachStyle = this.order;
        this.draglist[this.showOrder[i]].draggingEnabled = true;
        this.draglist[this.showOrder[i]].showObject();
        
        if(i==0)
          this.draglist[this.showOrder[0]].setPosition(this.objectX, this.objectY);
        
        this.draglist[this.showOrder[i]].listX = this.draglist[this.showOrder[i]].objectContent.x;
        this.draglist[this.showOrder[i]].listY = this.draglist[this.showOrder[i]].objectContent.y;
        
        if(this.maxWidth < this.draglist[this.showOrder[i]].objectContent.width)
          this.maxWidth = this.draglist[this.showOrder[i]].objectContent.width;
        if(this.maxHeight < this.draglist[this.showOrder[i]].objectContent.height)
          this.maxHeight = this.draglist[this.showOrder[i]].objectContent.height;
        
        if (this.draglist[this.showOrder[i]].onPressHandlerOriginal == undefined){
          this.draglist[this.showOrder[i]].onPressHandlerOriginal = this.draglist[this.showOrder[i]].onPressHandler;
          this.draglist[this.showOrder[i]].onPressHandler = function() {            
            this.depth -= 500;
            this.onPressHandlerOriginal();
          }
        }
        
        if (this.draglist[this.showOrder[i]].onReleaseHandlerOriginal == undefined){          
          this.draglist[this.showOrder[i]].onReleaseHandlerOriginal = this.draglist[this.showOrder[i]].onReleaseHandler;
          this.draglist[this.showOrder[i]].onReleaseHandler = function() {
            if(this.objectCaptured){
              this.objectCaptured = false;
              placeObject(this);
            }
            this.onReleaseHandlerOriginal();
          }
        }

        if (this.draglist[this.showOrder[i]].onReleaseOutsideHandlerOriginal == undefined){
          this.draglist[this.showOrder[i]].onReleaseOutsideHandlerOriginal = this.draglist[this.showOrder[i]].onReleaseOutsideHandler;
          this.draglist[this.showOrder[i]].onReleaseOutsideHandler = function() {
            if(this.objectCaptured){
              this.objectCaptured = false;
              placeObject(this);
            }
            this.onReleaseOutsideHandlerOriginal();
          }
        }        
      }  
    }        
    this.nError = 0;
    
    rnmc_stdMediaObject_showObject();
  }

  function hideObject(){
    if(this.buttons)
      for(var i = 0; i < this.buttons.length; i++)
        this.buttons[i].hideObject();

    if(this.draglist){
      for(var i = 0; i < this.draglist.length; i++){
        this.draglist[i].draggingEnabled = false;
        
        this.draglist[i].hideObject();
        
        this.draglist[i].onPressHandler = this.draglist[i].onPressHandlerOriginal;
        delete this.draglist[i].onPressHandlerOriginal;
        
        this.draglist[i].onReleaseHandler = this.draglist[i].onReleaseHandlerOriginal;
        delete this.draglist[i].onReleaseHandlerOriginal;
        
        this.draglist[i].onReleaseOutsideHandler = this.draglist[i].onReleaseOutsideHandlerOriginal;
        delete this.draglist[i].onReleaseOutsideHandlerOriginal;
      }
    }
    rnmc_stdMediaObject_hideObject();    
  }

//  сброс
  function resetObject(){
//trace("#FF0000resetObject()");
  //  устанавливаем изначальное состояние объекта
    if(this.draglist){          
      //  в случайном порядке
      var tempSequence = new Array();    
      for(var i=0; i<this.draglist.length; i++)
        tempSequence.push(i);    
      this.showOrder = scene.Utils.permutateArray(tempSequence);
      tempSequence = null;
      
      for(var i=0; i<this.showOrder.length; i++){
        if(i>0){
          this.draglist[this.showOrder[i]].autoAttachTarget = this.draglist[this.showOrder[i-1]].id;
        }
        this.draglist[this.showOrder[i]].index = i;
        this.draglist[this.showOrder[i]].autoAttachOffsetX = this.offsetX;
        this.draglist[this.showOrder[i]].autoAttachOffsetY = this.offsetY;
        this.draglist[this.showOrder[i]].autoAttachStyle = this.order;
        this.draglist[this.showOrder[i]].draggingEnabled = true;
        this.draglist[this.showOrder[i]].showObject();
        
        if(i==0)
          this.draglist[this.showOrder[0]].setPosition(this.objectX, this.objectY);
        
        this.draglist[this.showOrder[i]].listX = this.draglist[this.showOrder[i]].objectContent.x;
        this.draglist[this.showOrder[i]].listY = this.draglist[this.showOrder[i]].objectContent.y;
      }
    }    
    this.nError = 0;
	rnmc_stdMediaObject_resetObject();
  }

  function dispatchAction(singleAction){
    switch (singleAction[0]) {
      case "checkObject":
        this.doAction(singleAction);
        break;
      case "resetObject":
        this.doAction(singleAction);
        break;
      default:
        rnmc_stdMediaGroup_dispatchAction(singleAction);
        break;
    }
  }


//  выполняет специфичные для объекта rnmc_stdIntegration действия
  function doAction(singleAction){
//trace("integration: " + this.id + " - doAction: " + singleAction);  
    switch(singleAction[0]){  
    case "checkObject":
      this.checkObject();
    case "resetObject":
      this.resetObject();
    default:
      rnmc_stdMediaGroup_doAction(singleAction);
      break;
    };
  }

  function applyDefaultActions(dragObj, forEvent){
    switch(forEvent){
    case "onPlacedRight":
      if(this.onPlacedRightActions){
        for(var i = 0; i < this.onPlacedRightActions.length; i++){
          this.dispatchAction(this.onPlacedRightActions[i]);
        }
      }
      if(dragObj.onPlacedRightActions){
        for(var i = 0; i < dragObj.actionsList[EVENT_ON_PLACEDRIGHT].length; i++){
          this.dispatchAction(dragObj.actionsList[EVENT_ON_PLACEDRIGHT][i]);
        }
      }
      break;
    case "onPlacedWrong":
      if(this.onPlacedWrongActions){
        for(var i = 0; i < this.onPlacedWrongActions.length; i++){
          this.dispatchAction(this.onPlacedWrongActions[i]);
        }
      }
      if(dragObj.onPlacedWrongActions){
        for(var i = 0; i < dragObj.actionsList[EVENT_ON_PLACEDWRONG].length; i++){
          this.dispatchAction(dragObj.actionsList[EVENT_ON_PLACEDWRONG][i]);
        }
      }
      break;
    case "onComplete":
      if(this.onCompleteActions){
        for(var i = 0; i < this.onCompleteActions.length; i++){
          this.dispatchAction(this.onCompleteActions[i]);
        }
      }
    case "onWrongComplete":
      if(this.onWrongCompleteActions){
        for(var i = 0; i < this.onWrongCompleteActions.length; i++){
          this.dispatchAction(this.onWrongCompleteActions[i]);
        }
      }
      break;
    }
  }

  function moveTo(dragObj, xDest, yDest){
    if(dragObj.newEffect)
      dragObj.newEffect.updateSequencer.playing = false
    else{
      dragObj.newEffect = new Composition();
      dragObj.newEffect._class = "rnmc_stdEffect";
      dragObj.newEffect.parent = dragObj;
      dragObj.newEffect.effectDelta = this.flowdelta;
      dragObj.newEffect.effectType = EFFECT_MOVE;
    }
    dragObj.newEffect.effectRunning = true;    
    dragObj.newEffect.targetObject = dragObj.objectContent;    
    dragObj.newEffect.objectFinishX = xDest;
    dragObj.newEffect.objectFinishY = yDest;  
    dragObj.newEffect.updateSequencer.playing = true;
  }

  function checkObject(){
    var complete = 0;
    for( var i=0; i<this.draglist.length; i++){      
      if(this.draglist[i] == this.draglist[this.draglist[i].index])
        complete++
      else
        break;
    }
    if(complete == this.draglist.length)
      this.applyDefaultActions(0, "onComplete")
    else
      this.applyDefaultActions(0, "onWrongComplete")
  }
  
  function shiftDown(dragObj, dragObjY, i){
    this.moveTo(dragObj, this.draglist[this.showOrder[i]].listX, dragObjY);
    var otherY = dragObj.listY;
    for(var j=dragObj.index+1; j<=i; j++){
      if(j>dragObj.index+1)
        otherY += this.draglist[this.showOrder[j-1]].objectContent.height + this.offsetY;
      this.moveTo(this.draglist[this.showOrder[j]], this.draglist[this.showOrder[j-1]].listX, otherY);
      this.draglist[this.showOrder[j]].index--;                    
      this.draglist[this.showOrder[j]].listY = otherY;
    }
    for(var j=i; j>dragObj.index; j--)
      this.draglist[this.showOrder[j]].listX = this.draglist[this.showOrder[j-1]].listX;
    if(dragObj.index == 0)
      this.draglist[this.showOrder[dragObj.index+1]].autoAttachTarget = null
    else
      this.draglist[this.showOrder[dragObj.index+1]].autoAttachTarget = this.draglist[this.showOrder[dragObj.index-1]].id;  
    dragObj.autoAttachTarget = this.draglist[this.showOrder[i]].id;
    dragObj.listX = this.draglist[this.showOrder[i]].listX + this.offsetX;;
    dragObj.listY = dragObjY;
    var dragOrder = this.showOrder[dragObj.index];
    for(var j=dragObj.index; j<i; j++)
      this.showOrder[j] = this.showOrder[j+1];                  
    this.showOrder[i] = dragOrder;
    dragObj.index = i;
  }
  
  function shiftUp(dragObj, dragObjY, i){
    this.moveTo(dragObj, this.draglist[this.showOrder[i]].listX, dragObjY);
    var otherY = dragObjY + dragObj.objectContent.height + this.offsetY;
    for(var j=i; j<=dragObj.index-1; j++){
      if(j > i)
        otherY += this.draglist[this.showOrder[j-1]].objectContent.height + this.offsetY;
      this.moveTo(this.draglist[this.showOrder[j]], this.draglist[this.showOrder[j+1]].listX, otherY);                  
      this.draglist[this.showOrder[j]].index++;
      this.draglist[this.showOrder[j]].listX = this.draglist[this.showOrder[j+1]].listX;
      this.draglist[this.showOrder[j]].listY = otherY;
    }
    this.draglist[this.showOrder[i]].autoAttachTarget = dragObj.id;              
    if(dragObj.index < this.draglist.length-1)
      this.draglist[this.showOrder[dragObj.index+1]].autoAttachTarget = this.draglist[this.showOrder[dragObj.index-1]].id;                
    dragObj.listX = this.draglist[this.showOrder[i]].listX - this.offsetX;
    dragObj.listY = dragObjY;                
    var dragOrder = this.showOrder[dragObj.index];
    for(var j=dragObj.index; j>i; j--){
      this.showOrder[j] = this.showOrder[j-1];
    }              
    this.showOrder[i] = dragOrder;
    dragObj.index = i;
  }
  
  function shiftRight(dragObj, dragObjX, i){
    this.moveTo(dragObj, dragObjX, this.draglist[this.showOrder[i]].listY);
    var otherX = dragObj.listX;
    for(var j=dragObj.index+1; j<=i; j++){
      if(j>dragObj.index+1)
        otherX += this.draglist[this.showOrder[j-1]].objectContent.width + this.offsetX;
      this.moveTo(this.draglist[this.showOrder[j]], otherX, this.draglist[this.showOrder[j-1]].listY);
      this.draglist[this.showOrder[j]].index--;                    
      this.draglist[this.showOrder[j]].listX = otherX;
    }
    for(var j=i; j>dragObj.index; j--)
      this.draglist[this.showOrder[j]].listY = this.draglist[this.showOrder[j-1]].listY;
    if(dragObj.index == 0)
      this.draglist[this.showOrder[dragObj.index+1]].autoAttachTarget = null
    else
      this.draglist[this.showOrder[dragObj.index+1]].autoAttachTarget = this.draglist[this.showOrder[dragObj.index-1]].id;  
    dragObj.autoAttachTarget = this.draglist[this.showOrder[i]].id;
    dragObj.listX = dragObjX;
    dragObj.listY = this.draglist[this.showOrder[i]].listY + this.offsetY;;    
    var dragOrder = this.showOrder[dragObj.index];
    for(var j=dragObj.index; j<i; j++)
      this.showOrder[j] = this.showOrder[j+1];                  
    this.showOrder[i] = dragOrder;
    dragObj.index = i;
  }
  
  function shiftLeft(dragObj, dragObjX, i){
    this.moveTo(dragObj, dragObjX, this.draglist[this.showOrder[i]].listY);
    var otherX = dragObjX + dragObj.objectContent.width + this.offsetX;
    for(var j=i; j<=dragObj.index-1; j++){
      if(j > i)
        otherX += this.draglist[this.showOrder[j-1]].objectContent.width + this.offsetX;
      this.moveTo(this.draglist[this.showOrder[j]], otherX, this.draglist[this.showOrder[j+1]].listY);                  
      this.draglist[this.showOrder[j]].index++;
      this.draglist[this.showOrder[j]].listX = otherX;
      this.draglist[this.showOrder[j]].listY = this.draglist[this.showOrder[j+1]].listY;      
    }
    this.draglist[this.showOrder[i]].autoAttachTarget = dragObj.id;              
    if(dragObj.index < this.draglist.length-1)
      this.draglist[this.showOrder[dragObj.index+1]].autoAttachTarget = this.draglist[this.showOrder[dragObj.index-1]].id;                
    dragObj.listX = dragObjX;
    dragObj.listY = this.draglist[this.showOrder[i]].listY - this.offsetY;    
    var dragOrder = this.showOrder[dragObj.index];
    for(var j=dragObj.index; j>i; j--){
      this.showOrder[j] = this.showOrder[j-1];
    }              
    this.showOrder[i] = dragOrder;
    dragObj.index = i;
  }
  
  function placeObject(dragObj){
//trace("testIsInPlace");
    if(this.checkMode == DRAGGING_CHECK_AFTER){
      if(this.order == AUTOATTACH_BOTTOM_LEFT){      
      //  если элемент полдностью за пределами списка - возврат на исходную позицию
        if(dragObj.objectContent.x > this.objectX + this.maxWidth + this.sensitivity + this.offsetX * (this.draglist.length - 1) ||
        dragObj.objectContent.x + dragObj.objectContent.width < this.objectX - this.sensitivity)        
          this.moveTo(dragObj, dragObj.listX, dragObj.listY)
        else{
          var centerY = dragObj.objectContent.y + dragObj.objectContent.height/2;
          var dragObjY = dragObj.listY;
          if(dragObj.objectContent.y > dragObj.listY + (dragObj.objectContent.height + this.offsetY)/2){        
            if(dragObj.index < this.showOrder.length-1){
              for(var i=dragObj.index+1; i<this.showOrder.length; i++){
                if(centerY > this.draglist[this.showOrder[i]].listY - this.offsetY/2)
                  dragObjY += this.draglist[this.showOrder[i]].objectContent.height + this.offsetY
                else{
                  this.shiftDown(dragObj, dragObjY, i-1);
                  this.draglist[this.showOrder[dragObj.index+1]].autoAttachTarget = dragObj.id;
                  break;
                }
                if(i == this.showOrder.length-1){
                  this.shiftDown(dragObj, dragObjY, i);
                }
              }
            } else
              this.moveTo(dragObj, dragObj.listX, dragObj.listY);            
          } else if(dragObj.objectContent.y < dragObj.listY - (dragObj.objectContent.height + this.offsetY)/2){          
            if(dragObj.index > 0){            
              for(var i=dragObj.index-1; i>=0; i--){
                if(centerY < this.draglist[this.showOrder[i]].listY + this.draglist[this.showOrder[i]].objectContent.height + this.offsetY/2)
                  dragObjY -= this.draglist[this.showOrder[i]].objectContent.height + this.offsetY
                else{
                  this.shiftUp(dragObj, dragObjY, i+1);
                  break;
                }
                if(i == 0){
                  this.shiftUp(dragObj, dragObjY, i);
                }
              }
            } else
              this.moveTo(dragObj, dragObj.listX, dragObj.listY);
          } else
            this.moveTo(dragObj, dragObj.listX, dragObj.listY);
        }        
      } else{
      //  горизонтальная расстановка
        if(dragObj.objectContent.y > this.objectY + this.maxHeight + this.sensitivity + this.offsetY * (this.draglist.length - 1) ||
        dragObj.objectContent.y + dragObj.objectContent.height < this.objectY - this.sensitivity)        
          this.moveTo(dragObj, dragObj.listX, dragObj.listY)
        else{
          var centerX = dragObj.objectContent.x + dragObj.objectContent.width/2;
          var dragObjX = dragObj.listX;
          if(dragObj.objectContent.x > dragObj.listX + (dragObj.objectContent.width + this.offsetX)/2){        
            if(dragObj.index < this.showOrder.length-1){
              for(var i=dragObj.index+1; i<this.showOrder.length; i++){
                if(centerX > this.draglist[this.showOrder[i]].listX - this.offsetX/2)
                  dragObjX += this.draglist[this.showOrder[i]].objectContent.width + this.offsetX
                else{
                  this.shiftRight(dragObj, dragObjX, i-1);
                  this.draglist[this.showOrder[dragObj.index+1]].autoAttachTarget = dragObj.id;
                  break;
                }
                if(i == this.showOrder.length-1){
                  this.shiftRight(dragObj, dragObjX, i);
                }
              }
            } else
              this.moveTo(dragObj, dragObj.listX, dragObj.listY);            
          } else if(dragObj.objectContent.x < dragObj.listX - (dragObj.objectContent.width + this.offsetX)/2){          
            if(dragObj.index > 0){            
              for(var i=dragObj.index-1; i>=0; i--){
                if(centerX < this.draglist[this.showOrder[i]].listX + this.draglist[this.showOrder[i]].objectContent.width + this.offsetX/2)
                  dragObjX -= this.draglist[this.showOrder[i]].objectContent.width + this.offsetX
                else{
                  this.shiftLeft(dragObj, dragObjX, i+1);
                  break;
                }
                if(i == 0){
                  this.shiftLeft(dragObj, dragObjX, i);
                }
              }
            } else
              this.moveTo(dragObj, dragObj.listX, dragObj.listY);
          } else
            this.moveTo(dragObj, dragObj.listX, dragObj.listY);
        }
      }      
      if(dragObj == this.draglist[dragObj.index])
        this.applyDefaultActions(dragObj, "onPlacedRight")
      else
        this.applyDefaultActions(dragObj, "onPlacedWrong");
    } else{
    //  проверка сразу
      if(this.order == AUTOATTACH_BOTTOM_LEFT){      
      //  если элемент полдностью за пределами списка - возврат на исходную позицию
        if(dragObj.objectContent.x > this.objectX + this.maxWidth + this.sensitivity + this.offsetX * (this.draglist.length - 1) ||
        dragObj.objectContent.x + dragObj.objectContent.width < this.objectX - this.sensitivity)        
          this.moveTo(dragObj, dragObj.listX, dragObj.listY)
        else{
          var centerY = dragObj.objectContent.y + dragObj.objectContent.height/2;
          var dragObjY = dragObj.listY;
          if(dragObj.objectContent.y > dragObj.listY + (dragObj.objectContent.height + this.offsetY)/2){        
            if(dragObj.index < this.showOrder.length-1){
              for(var i=dragObj.index+1; i<this.showOrder.length; i++){
                if(centerY > this.draglist[this.showOrder[i]].listY - this.offsetY/2)
                  dragObjY += this.draglist[this.showOrder[i]].objectContent.height + this.offsetY
                else{
                  if(dragObj == this.draglist[i-1]){
                    this.applyDefaultActions(dragObj, "onPlacedRight");
                    this.shiftDown(dragObj, dragObjY, i-1);
                    this.draglist[this.showOrder[dragObj.index+1]].autoAttachTarget = dragObj.id;
                  } else{
                    this.nError++;
                    this.applyDefaultActions(dragObj, "onPlacedWrong");
                    this.moveTo(dragObj, dragObj.listX, dragObj.listY);
                  }
                  break;
                }
                if(i == this.showOrder.length-1){
                  if(dragObj == this.draglist[i]){
                    this.applyDefaultActions(dragObj, "onPlacedRight");
                    this.shiftDown(dragObj, dragObjY, i);
                  } else{
                    this.nError++;
                    this.applyDefaultActions(dragObj, "onPlacedWrong");
                    this.moveTo(dragObj, dragObj.listX, dragObj.listY);
                  }
                }
              }
            } else
              this.moveTo(dragObj, dragObj.listX, dragObj.listY);            
          } else if(dragObj.objectContent.y < dragObj.listY - (dragObj.objectContent.height + this.offsetY)/2){          
            if(dragObj.index > 0){            
              for(var i=dragObj.index-1; i>=0; i--){
                if(centerY < this.draglist[this.showOrder[i]].listY + this.draglist[this.showOrder[i]].objectContent.height + this.offsetY/2)
                  dragObjY -= this.draglist[this.showOrder[i]].objectContent.height + this.offsetY
                else{
                  if(dragObj == this.draglist[i+1]){
                    this.applyDefaultActions(dragObj, "onPlacedRight");
                    this.shiftUp(dragObj, dragObjY, i+1);
                  } else{
                    this.nError++;
                    this.applyDefaultActions(dragObj, "onPlacedWrong");
                    this.moveTo(dragObj, dragObj.listX, dragObj.listY);
                  }
                  break;
                }
                if(i == 0){
                  if(dragObj == this.draglist[i]){
                    this.applyDefaultActions(dragObj, "onPlacedRight");
                    shiftUp(dragObj, dragObjY, i);
                  } else{
                    this.nError++;
                    this.applyDefaultActions(dragObj, "onPlacedWrong");
                    this.moveTo(dragObj, dragObj.listX, dragObj.listY);
                  }
                }                
              }
            } else
              this.moveTo(dragObj, dragObj.listX, dragObj.listY);
          } else
            this.moveTo(dragObj, dragObj.listX, dragObj.listY);        
        }
      } else{
      //  горизонтальная расстановка
        if(dragObj.objectContent.y > this.objectY + this.maxHeight + this.sensitivity + this.offsetY * (this.draglist.length - 1) ||
        dragObj.objectContent.y + dragObj.objectContent.height < this.objectY - this.sensitivity)        
          this.moveTo(dragObj, dragObj.listX, dragObj.listY)
        else{
          var centerX = dragObj.objectContent.x + dragObj.objectContent.width/2;
          var dragObjX = dragObj.listX;
          if(dragObj.objectContent.x > dragObj.listX + (dragObj.objectContent.width + this.offsetX)/2){        
            if(dragObj.index < this.showOrder.length-1){
              for(var i=dragObj.index+1; i<this.showOrder.length; i++){
                if(centerX > this.draglist[this.showOrder[i]].listX - this.offsetX/2)
                  dragObjX += this.draglist[this.showOrder[i]].objectContent.width + this.offsetX
                else{
                  if(dragObj == this.draglist[i-1]){
                    this.applyDefaultActions(dragObj, "onPlacedRight");
                    this.shiftRight(dragObj, dragObjX, i-1);
                    this.draglist[this.showOrder[dragObj.index+1]].autoAttachTarget = dragObj.id;
                  } else{
                    this.nError++;
                    this.applyDefaultActions(dragObj, "onPlacedWrong");
                    this.moveTo(dragObj, dragObj.listX, dragObj.listY);
                  }
                  break;
                }
                if(i == this.showOrder.length-1){
                  if(dragObj == this.draglist[i]){
                    this.applyDefaultActions(dragObj, "onPlacedRight");
                    this.shiftRight(dragObj, dragObjX, i)
                  } else{
                    this.nError++;
                    this.applyDefaultActions(dragObj, "onPlacedWrong");
                    this.moveTo(dragObj, dragObj.listX, dragObj.listY);
                  }
                }
              }
            } else
              this.moveTo(dragObj, dragObj.listX, dragObj.listY);            
          } else if(dragObj.objectContent.x < dragObj.listX - (dragObj.objectContent.width + this.offsetX)/2){          
            if(dragObj.index > 0){            
              for(var i=dragObj.index-1; i>=0; i--){
                if(centerX < this.draglist[this.showOrder[i]].listX + this.draglist[this.showOrder[i]].objectContent.width + this.offsetX/2)
                  dragObjX -= this.draglist[this.showOrder[i]].objectContent.width + this.offsetX
                else{
                  if(dragObj == this.draglist[i+1]){
                    this.applyDefaultActions(dragObj, "onPlacedRight");
                    this.shiftLeft(dragObj, dragObjY, i+1);
                  } else{
                    this.nError++;
                    this.applyDefaultActions(dragObj, "onPlacedWrong");
                    this.moveTo(dragObj, dragObj.listX, dragObj.listY);
                  }
                  break;
                }
                if(i == 0){
                  if(dragObj == this.draglist[i]){
                    this.applyDefaultActions(dragObj, "onPlacedRight");
                    this.shiftLeft(dragObj, dragObjY, i);
                  } else{
                    this.nError++;
                    this.applyDefaultActions(dragObj, "onPlacedWrong");
                    this.moveTo(dragObj, dragObj.listX, dragObj.listY);
                  }
                }
              }
            } else
              this.moveTo(dragObj, dragObj.listX, dragObj.listY);
          } else
            this.moveTo(dragObj, dragObj.listX, dragObj.listY);
        }
      }
      var complete = 0;
      for( var i=0; i<this.draglist.length; i++){      
        if(this.draglist[i] == this.draglist[this.draglist[i].index])
          complete++
        else
          break;
      }
      if(complete == this.draglist.length)
        this.applyDefaultActions(dragObj, "onComplete");
    }    
    dragObj.depth += 500;
  }
    

//==================================================================
//  функция пока не используется
  function getResult(resultsArray){
    this.rightObjects = 0;
    this.wrongObjects = 0;
    for(var i=0; i<this.draglist.length; i++){
      if(this.draglist[i].index == i)
        this.rightObjects++
      else
        this.wrongObjects++;
    }
    switch(this.crAlgorithm){
      case COUNTING_RESULTS_NO:
        this.objectResult = 0;
      case COUNTING_RESULTS_DISCRETE:
        this.objectResult = (this.rightObjects == this.draglist.length) ? 100 : 0;
      case COUNTING_RESULTS_SMOOTH:
        this.objectResult = this.rightObjects / this.draglist.length;
      case COUNTING_RESULTS_SMOOTH_ERR:
        if(this.rightObjects >= this.nErrors)
          this.objectResult = (this.rightObjects - this.nErrors) / this.draglist.length
        else
          this.objectResult = 0;
    }
    if(resultsArray && this.crAlgorithm != "none"){
      var resultString = this.objectResult + ";1.0";
      resultsArray.push(resultString);
    }

    rnmc_stdMediaGroup_getResult(resultsArray);
  }
  
//  вычисления результатов выполнения заданий объекта по заданному алгоритму
  function objectResult(){
    this.results = new Array();
    this.results.allElements = this.results.allRightElements = this.draglist.length;
    this.results.allWrongElements = 0;
    this.results.rightElements = 0;
    this.results.wrongElements = 0;
    for(var i=0; i<this.draglist.length; i++){
      if(this.draglist[i].index == i)
        this.results.rightElements++
      else
        this.results.wrongElements++;
    }
    this.results.wrongElements += this.nError;
    countObjectResult(this.results, this.crAlgorithm, false);      
  }
]]></script>
</composition>
</rmml>