function IsSolvedSides()
    for i = 1, 8 do
        if s["seq_s03_0113_chunk_"..i] ~= 2  then
            return false
        end
    end
    return true
end

function IsSolvedThisSides()
    for i = 6, 8 do
        if s["seq_s03_0113_chunk_"..i] ~= 2  then
            return false
        end
    end
    return true
end
     

function OnUpdate()
    local currModule = _G[self:GetName()]
 
    local attName = Cursors:GetAttachmentName()
    local att = currModule[attName]
    if att~=nil and not IsMouseButtonDown(0) then
        Cursors:DetachSprite(false) 
        local pos = att.self:GetPosition()
        
        local leftMargin = 0.008
        local rightMargin =  0.05+0.07
        local topMargin = 0.008
        local bottomMargin = 0.3+0.07
         
        
        if pos.y> 1 - bottomMargin then
             att.self:SetPosition(Ogre.Vector2(pos.x,1 - bottomMargin))
        end
        
        if pos.y<topMargin then
             att.self:SetPosition(Ogre.Vector2(pos.x,topMargin))
        end
        
          if pos.x> 1 - rightMargin then
             att.self:SetPosition(Ogre.Vector2(1 - rightMargin,pos.y))
        end
        
         if pos.x<leftMargin then
             att.self:SetPosition(Ogre.Vector2(leftMargin, pos.y))
        end
        
        local regName = "reg_"..string.sub(attName,14, 20)
        local pointName = "point_"..string.sub(attName,14, 20)


        
        local point = currModule[pointName]
            
        if point == nil then
            return
        end
        local pPos = point.self:GetPosition()
        local lenght = (pPos - pos):length()
        -- if lenght<0.03 then
        -- print( Seqence(attName):GetCurrentFrame())
        if currModule[regName]~=nil and currModule[regName].self:IsInside(pos) and 
        
        Seqence(attName):GetCurrentFrame() == 0
        
        then
           -- local x, y = point.self:GetX(), point.self:GetY()
          --  currModule[attName].self:SetPosition(Ogre.Vector2(x, y))
            s[attName] = 2
            if IsSolvedSides() then
                s.m_s03_chest_left_right_solved = 1
            end
            
            if  IsSolvedThisSides() then
                s.m_s03_chest_right_solved = 1
            end
                   
            
            PlaySound("snd_s03_0100_insert_chunk")
        end
    end

end



function Seqence(name)
    local s = MystSceneManager:GetInstance():GetCurrentView():GetNode(name)
    local ss = tolua.cast(s, "MystSprite2DSequence")
    return ss
end


function OnSceneEnter()
    local currModule = _G[self:GetName()]
    OldInterfaceMap.SetCuttentModule(currModule)

    indexed = {}
    for i = 1, 8 do
        indexed[#indexed+1] = "seq_s03_0113_chunk_"..i
    end    
    
    indexed[#indexed + 1] = "spr_s03_0113_top"
    
    OldInterfaceMap.SetIndexedElements(indexed)

end