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

local n = 0

local state = 
{
  [23]=true,
  [16]=true,
  [13]=true,
  [8]=true,
  [6]=true,
  [4]=true
}

local WIDTH=6
local HEIGHT=4
local COUNT=WIDTH*HEIGHT
local tabFreak=
  {
    ["StartX"]=192,
    ["StartY"]=130,
    ["W"]=106,
    ["H"]=105
  }

if flags.postavilTrafaret then
  speciale_action(2,30)
  -- miniCreate()
else
  -- NOW_MINI = true
  -- mini_create()
end

local active = nil
local pressed = nil

function gameinput (sender,obj_id,attachedID)
  MiniInterfaceHandler(sender, obj_id);
  sendersProcessing(sender, obj_id, attachedID);
  if wrongItemWarning(sender, obj_id, attachedID, "trafaret_16_12") then
    return
  end
  
  if sender == 1 or sender == 6 then
    if pressed then
      playeffect("switch")
      if flags.postavilTrafaret then
        state[pressed] = false
        for j,v in pairs(state) do
          if state[j] then
            return
          end
        end
        Block_Show_Cur(true)
        StartTimer(timerID,1,"frame1")
      else
        n = n + 1
        if n == 3 then
          n = 0
          CreatePlashechka("488")
          Block_Show_Cur(true)
          StartTimer(timerID,0.5,"unBlock_Show_Cur")
        end
      end
      SetAlfaValue(pressed,0.01)
      pressed = nil
    end
  end
  
  if sender == 1 then
    if obj_id >= 1 and obj_id <= 24 then
      if obj.trafaret_16_12 and attachedID == obj.trafaret_16_12.id then
        removeFromMouse()
        UnStoreObj(obj.trafaret_16_12.id)
        flags.postavilTrafaret = true
        playeffect("st_kom")
        speciale_action(2,30,true,1)
        for i=1,24 do setCursor(i,3) end
        setCursor(4,4)
        setCursor(6,4)
        setCursor(8,4)
        setCursor(13,4)
        setCursor(16,4)
        setCursor(23,4)
        n = 0
      end
    end
  end
  
  if sender == 5 then
    local _fFlag=false;
    local iIndex=COUNT-CursorInRect()

    if not NOW_ATTACHED and (active or iIndex<=COUNT) then
      if not active then
        _fFlag=true
        active=iIndex
      end
      pressed = active
      SetAlfaValue(active,1)
      if _fFlag then
        _fFlag=false
        active=nil
      end
    end
  end
  -- if sender == 6 then
    -- if pressed then
      -- SetAlfaValue(pressed,0.01)
      -- pressed = nil
    -- end
  -- end
  if sender == 35 then
    if obj_id >= 1 and obj_id <= 24 then
      active = obj_id
    end
  end
  if sender == 39 then
    if obj_id >= 1 and obj_id <= 24 and obj_id == active then
      active = nil
    end
  end
end

function frame1()
  Block_Show_Cur(false)
  neighbors.MidRuchka.lock = true
  neighbors.Pryazhki.lock = false
  CreateSatelite("Pryazhki")
  playeffect("sat")
end

function CursorInRect()
  local _i,_j=-1,-1; -- Строка, столбец.
  local xCursor,yCursor=GetCursorPos()

  xCursor=xCursor-tabFreak.StartX;
  yCursor=yCursor-tabFreak.StartY;
  _i=math.floor(yCursor/tabFreak.H);
  if _i>-1 and _i<HEIGHT then
    _j=math.floor(xCursor/tabFreak.W);
    if _j>-1 and _j<WIDTH then
      return _i*WIDTH+_j;
    end
  end
  return -1;
end
