function mini_create()
  init_mini(true)
  create_mini(8128);
end

function musi4ka()
      for i=10,21 do
        ActivateObject(i,false)
        setCursor(i,3)
      end
      Block_Show_Cur(false)
      flags.proshelPianino = true
      playeffect("mini_complete")
      Journal:addText(7,"9013",567,98,330)
      Journal:addPicture(7,"page7",201,112,0,271,751,501);
      playeffect("tain_piano")
      speciale_action(2,obj.gerb_7_14.id,true,1)
      speciale_action(2,22,true,1)
end;


if flags.ustanovilNotu then
  speciale_action(2,1)
  speciale_action(2,30)
  for i=1,5 do
    setCursor(i,4)
  end
  if flags.videlNoty then
    if not flags.otkrylKryshku then
      helpID = "626"
    else
      helpID = "627"
      for i=1,5 do
        set_visible_object(i,false)
      end
      -- for i=10,21 do
        -- setCursor(i,4)
      -- end
      set_visible_object(6,false)
    end
    if flags.proshelPianino then
      speciale_action(2,22)
      speciale_action(2,obj.gerb_7_14.id)
      for i=10,21 do
        setCursor(i,3)
        ActivateObject(i,false)
      end
    else
      miniCreate()
    end
  end
end

local x0,y0,x1,y1 = 0,0,0,0
local att = nil
local over = 0
local pressed = nil
local state = {3,5,1,2,4}
local sequence = {17,19,21,19,17,18,21,19,16,20}
local key = 0
local wrongKeys = 0

function gameinput (sender,obj_id,attachedID)
  MiniInterfaceHandler(sender,obj_id,attachedID)
  sendersProcessing(sender, obj_id, attachedID);
  -- --out("gameinput("..tostring(sender)..","..tostring(obj_id)..","..tostring(attachedID)..")\n")
  if sender==1 then
    if obj_id == 30 then
      if obj.nota_5_1 and attachedID == obj.nota_5_1.id then
        removeFromMouse()
        UnStoreObj(obj.nota_5_1.id)
        playeffect("notes")
        flags.ustanovilNotu = true
        speciale_action(2,1,true,0.5)
        speciale_action(2,30,true,0.1)
        for i=1,5 do
          setCursor(i,4)
        end
        if flags.videlNoty then
          helpID = "626"
          miniCreate()
        else
          CreatePlashechka("637")
        end
      else
        CreatePlashechka("665")
      end
    end
    if obj_id == obj.gerb_7_14.id then
      neighbors.piano.lock = true
      flags.ZalMuzeyaHidden2=true
      neighbors.ZalMuzeyaHidden.lock = false
      StoreObj(obj.gerb_7_14.id)
    end
  end
  if sender == 26 then
  --  CloseSatelite()
  end
  -- MINI 1
  if flags.ustanovilNotu and not flags.otkrylKryshku then
    if sender == 1 then
      if obj_id >= 1 and obj_id <= 5 then
        if flags.videlNoty then
          if not att then
            att = obj_id
            setcolor(att,0.7,0.7,0.7,1)
          elseif att == obj_id then
            setcolor(att,1,1,1,1)
            att = nil
          elseif att and att ~= obj_id then
            local x1,y1 = GetObjectPosition(obj_id)
            local x2,y2 = GetObjectPosition(att)
            SetObjectPosition(obj_id,x2,y2)
            SetObjectPosition(att,x1,y1)
            playeffect("notes")
            setcolor(att,1,1,1,1)
            state[att],state[obj_id] = state[obj_id],state[att]
            att = nil
            checkNotes()
          end
        else
          CreatePlashechka("616")
        end
      end
    end
    if sender == 10 then
      flags.otkrylKryshku = true
      Block_Show_Cur(true)
      StartTimer(timerID,2,"frame1")
      playeffect("kr_piano")
      for i=1,5 do
        speciale_action(2,i,true,1)
      end
      -- for i=10,21 do
        -- setCursor(i,4)
      -- end
      speciale_action(2,6,true,1)
      return
    end
  end
  
  -- MINI 2
  if flags.otkrylKryshku and not flags.proshelPianino then
    if sender == 1 then
      if obj_id >= 10 and obj_id <= 21 then
        SetAlfaValue(obj_id,1)
        pressed = obj_id
        eff="note"..tostring(obj_id)
        playeffect(eff)
      end
    end
    if sender == 8129 then
      if pressed then
        SetAlfaValue(pressed,0.01)
        if pressed == sequence[key+1] then
          key = key + 1
          set_visible_object(key+30,true);
          if key == #sequence then
            finish_mini_game(true)

          end
        else
          wrongKeys = wrongKeys + 1 + key
          key = 0
          if wrongKeys >= 1 then
            for i=31,40,1 do
              set_visible_object(i,false);
            end;
            CreatePlashechka("641")
            wrongKeys = 0
            
          end
        end
        pressed = nil
      end
    end
    if sender == 10 then
      key = 0
      Block_Show_Cur(true)
      frame2()    
    

    end
  end
  
  if sender == 30 then
    if neighbors.piano.lock then
      dofile("./data/7/0/comics.lua")
    end
  end
end

function checkNotes()
  --out("state={")
  for i=1,5 do
    out(tostring(state[i])..",")
  end
  --out("}\n")
  for i=1,5 do
    if state[i] ~= i then
      return
    end
  end
  finish_mini_game(true)
end

function frame1()
  helpID = "627"
  Block_Show_Cur(false)
  miniCreate()
end

function frame2()
  --out("2")
  key = key + 1
  SetAlfaValue(sequence[key],1)
  StartTimer(timerID,0.5,"frame3")
end

function frame3()
  --out("3\n")
          eff="note"..tostring(sequence[key])
        playeffect(eff)
  SetAlfaValue(sequence[key],0)
  if key < 10 then
    -- StartTimer(timerID,0.3,"frame2")
    frame2()
  else
  musi4ka()
  --  finish_mini_game(true)
  end
end