loadresource("data\\all_cursor.png","all_cursor");
--loadresource("data\\tr_shtaf.png","tresh1");
function Block_Show_Cur(param,AttachedItem)
   if param then
      ClearCursor1()
      if not flags.IngameMenu then
      ShowCursor(false)
      BlockCursor(true)
    flags.CursorBlocking=true
    Write("Must Be true")
    end
   return;end;
      ShowCursor(true)
      BlockCursor(false)   
    flags.CursorBlocking=false
     Write("Must Be false")
return;end;


skip1 = -910
skip2 = -911
skip3 = -912
renderTarget = -766
out = Write
currentCursors = {} -- сюда записываются уже установленные, но не сброшенные пользовательские курсоры для того,
          -- чтобы не было глюков при наведени на объекты, которые один на другом

SetShtrafFunc("Shtrafing",10,30) -- устанавливаем обработчик штрафа за клик

-- nFPS = 0
-- FPS = false
-- FPS_id = 345678
-- FPS_timer = 678932
-- FPS_flag = true

-- function FPS_timer_callback()
  -- FPS_flag = true
-- end


----------------------------------------------------------
--  function: showFPS
----------------------------------------------------------
-- function showFPS(arg)
  -- FPS = arg
  -- if FPS then
    -- addtexttogame(FPS_id,10,10,"1")
    -- set_text(FPS_id,"0",true)
    -- setcolor(FPS_id,1,1,1,1,true)
    -- speciale_action(4,FPS_id)
    -- WaitRender()
  -- else
    -- del_object_from_game(FPS_id)
  -- end
-- end


----------------------------------------------------------
--  function: freeCustomCursor
-- обёртка для функции FreeCustomCursor()
-- нужна для того, чтобы сделать штраф за клики
----------------------------------------------------------
function freeCustomCursor()
  SetObjectPosition(500002,2000,2000)
  SetObjectPosition(500004,2000,2000)
  SetObjectPosition(500005,2000,2000)
  SetObjectPosition(500006,2000,2000)
  SetObjectPosition(500007,2000,2000)
  SetObjectPosition(500008,2000,2000)
  SetObjectPosition(500013,2000,2000)
  FreeCustomCursor()
  SetCustomCursor(10000002)
end
SetCustomCursor(10000002)

------------------------------------------------------------
--  function: OnMouseOverMenu
-- этот callback взывается при наведении на элемент интерфейса
-- и сбрасывает пользовательские курсоры
------------------------------------------------------------
function OnMouseOverMenu(ncount)
  if table.maxn(currentCursors) > 0 then
    ClearCursor1()
  end
end;


------------------------------------------------------------
--  function: LevelComplete
-- эта функция вызывается при прохождении хиддена
-- или при Ctrl+Shift+F7
------------------------------------------------------------
function LevelComplete()
  if flags.is_hidden_loc then
    BlockCursor(false)
    hiddenending=false
    playeffect("hidenend")
    StoreObj(obj[reward].id)
  end
end


------------------------------------------------------------
--  function: SetActivLeaf
------------------------------------------------------------
function SetActivLeaf(leafname)
  AddObjForCur()
  set_activ_leaf(leafname)
  -- showFPS(true)
end


------------------------------------------------------------
--  function: setcurentleaf
------------------------------------------------------------
function setcurentleaf(leafname,level,sublevel)          
  SetActivLeaf(leafname);
  ShowMenu(true);
  UpdateStack()
  dofile("data\\"..level.."\\"..sublevel.."\\level.lua");
  if flags.is_hidden_loc then
    SwitchToSheet("re");
    SetDrawSheet("gr",false);
  else
    SwitchToSheet("gr");
    SetDrawSheet("gr",true);
  end;
end;


------------------------------------------------------------
--  function: dummy
------------------------------------------------------------
function dummy()
end


------------------------------------------------------------
--  function: ClearCursor
-- сбрасывает все пользовательские курсоры
------------------------------------------------------------
function  ClearCursor()
  ClearCursor1()
  for i,v in pairs(cursors) do
    StopOnMouseOver(i)
  end
  cursors = {}
end


------------------------------------------------------------
--  function: ClearCursor1
-- сбрасывает текущий курсор
------------------------------------------------------------
function  ClearCursor1()

  freeCustomCursor()
  currentCursors = {}
end


------------------------------------------------------------
--  function: AddObjForCur
-- добавляет на локацию объекты для пользовательских курсоров
-- я её вызываю в SetActivLeaf() и в renderTargetSateliteCB()
------------------------------------------------------------
function AddObjForCur()
  add_animation(500002)  -- глаз
  add_animation_frame(500002,"all_cursor",2000,0,199,0,70,65,0.2); -- закрыт
  add_animation_frame(500002,"all_cursor",2000,0,784,0,67,65,0.2);
  add_animation_frame(500002,"all_cursor",2000,0,269,0,67,65,0.2);
  add_animation_frame(500002,"all_cursor",2000,0,336,0,67,65,2); -- открыт
  add_animation_frame(500002,"all_cursor",2000,0,269,0,67,65,0.2);
  add_animation_frame(500002,"all_cursor",2000,0,784,0,67,65,0.2);
  StartAnimation(500002)
  
  add_animation(500004)  -- рука
  add_animation_frame(500004,"all_cursor",2000,0  ,578,0  ,59,70,0.5);
  add_animation_frame(500004,"all_cursor",2000,0  ,710,4  ,59,70,0.25);
  add_animation_frame(500004,"all_cursor",2000,0  ,403,0  ,60,70,1);-- разжат
  add_animation_frame(500004,"all_cursor",2000,0  ,710,4  ,59,70,0.25);
  StartAnimation(500004)
  
  add_animation(500005)  -- вверх
  add_animation_frame(500005,"all_cursor",2000,0,  463,0  ,55,68,0.5);
  add_animation_frame(500005,"all_cursor",2000,0,  642,0  ,55,68,0.5);
  StartAnimation(500005)
  
  add_animation(500006)  -- вниз
  add_animation_frame(500006,"all_cursor",2000,0,518,0,55,68,0.5);
  add_animation_frame(500006,"all_cursor",2000,0,133,65,55,57,0.5);
  StartAnimation(500006)
  
  add_animation(500007)  -- вправо
  add_animation_frame(500007,"all_cursor",2000,0,199,65,68,55,0.5);
  add_animation_frame(500007,"all_cursor",2000,0,269,65,57,55,0.5);
  StartAnimation(500007)
  
  add_animation(500008)  -- влево
  add_animation_frame(500008,"all_cursor",2000,0,403,70,68,55,0.5);
  add_animation_frame(500008,"all_cursor",2000,0,325,65,68,55,0.5);
  StartAnimation(500008)
  
  add_animation(500013)  -- шерстеренка
  add_animation_frame(500013,"all_cursor",2000,0,0,0,67,66,0.2);
  add_animation_frame(500013,"all_cursor",2000,0,67,0,66,66,0.2);
  add_animation_frame(500013,"all_cursor",2000,0,133,0,66,65,0.2);
  StartAnimation(500013)
end;


------------------------------------------------------------
--  function: sendersProcessing
-- эта функция обрабатывает типичные события;
-- вызывается в каждом гейминпуте
------------------------------------------------------------
function sendersProcessing(sender, obj_id, attachedID)
  -- if sender ~= 100 then
    --out("sendersProcessing("..tostring(sender)..", "..tostring(obj_id)..", "..tostring(attachedID)..")\n")
  -- end
    if sender==2 then
    
        MessageProc(sender,obj_id);
    return;end;  
  
  if sender==1 then
    if flags.is_hidden_loc then
      if obj_id > 0 then
        playeffect("collaps")
        CollapseObj(obj_id);
      end
    end
  elseif sender == 8 then
    removeFromMouse()
  elseif sender == 10 then
    deleteMiniOutfit()
    NOW_MINI = false
  elseif sender == 12 then
    if flags.collapsed == 15 then
      -- BlockCursor(true)
      SaveGame()
      hiddenending=true
      flags["hidden"..tostring(gamedata.cur_lev_number)] = true
      LevelComplete()
      -- AlternateTimer("LevelComplete", 1000);
    end
  elseif sender == 14 then
    if obj_id ~= -1 then
      if NOW_ATTACHED then
        removeFromMouse()
      end
      addToMouse(obj_id)
    end
  elseif sender == 16 then
    local x,y = GetObjectPosition(obj_id)
    local a,b = GetObjectSize(obj_id)
    local x0,y0 = GetCursorPos()
    if x0 >= x and x0 <= x+a and y0 >= y and y0 <= y+b then
      gameinput(35,obj_id,attachedID)
    end
  elseif sender == 26 then
    if not flags.is_hidden_loc then
      addToInventory()
    end;
    -- BlockCursor(true)
    -- AlternateTimer("unBlock_Show_Cur",500)
    -- if flags.is_hidden_loc then
      -- neighbors[gamedata.current_level].obj_created = false
      -- CallbackForSheetButton(nCount);
    -- end
  elseif sender == 30 then
    ReturnHiddenObjects()
    setcurentleaf(gamedata.current_level,gamedata.cur_lev_number,gamedata.cur_sub_lev_number)
    if NOW_MINI then
      finish_mini_game(false)
      NOW_MINI = false
    end
    DeleteRenderTarget(renderTarget)
    objectsToRender = {}
    ClearCursor1()
    -- SaveGame()
  elseif sender == 35 then
    if cursors[obj_id] and cursors[obj_id][1] ~= 3 then
      local tmpD = currentCursors
      ClearCursor1()
      currentCursors = tmpD
      currentCursors[obj_id] = cursors[obj_id][1]
      SetObjectPosition(10000002,2000,2000)
      --out("35      cC["..tostring(obj_id).."]="..tostring(currentCursors[obj_id]).."\n")
      SetCustomCursor(500000+currentCursors[obj_id],-15,-15)
    end
  elseif sender == 39 or sender == 15 then
    if cursors[obj_id] then
      currentCursors[obj_id] = nil
      local tmpD = currentCursors
      ClearCursor1()
      currentCursors = tmpD
      if table.maxn(currentCursors) > 0 then
        --out("39      curr["..table.maxn(currentCursors).."]="..tostring(currentCursors[table.maxn(currentCursors)]).."\n")  
        SetObjectPosition(10000002,2000,2000)
        SetCustomCursor(500000+currentCursors[table.maxn(currentCursors)],-15,-15)
      end
    end
  -- elseif sender == 100 then
    -- if FPS then
      -- nFPS = nFPS + 1
      -- if FPS_flag then
        -- set_text(FPS_id,tostring(nFPS),true)
        -- nFPS = 0
        -- FPS_flag = false
        -- StartTimer(FPS_timer,1,"FPS_timer_callback",true)
      -- end
      -- WaitRender()
    -- end
  end
end


------------------------------------------------------------
--  function: Shtrafing
-- штраф за клик
------------------------------------------------------------
function Shtrafing()
  if not flags.is_hidden_loc then
    return
  end
  --out("SHTRAF\n")
  if not (rendering or hiddenending) then
  rendering=true
  
  shtrafing=true
  GetRenderTarget(renderTarget,"renderTargetCB")
  objectsToRender = {10000002,renderTarget}
  end;

end


------------------------------------------------------------
--  function: shtraf1
------------------------------------------------------------
function shtraf1()
  SetAlfa(-10000002,0,1,2,"hideShtraf")
  StopEmiter("top_left1",true)
  StopEmiter("top_right1",true)
  StopEmiter("bottom_right1",true)
  StopEmiter("bottom_left1",true)    
end


------------------------------------------------------------
--  function: hideShtraf
-- прекращаем штраф за клик
------------------------------------------------------------
function hideShtraf()
  BlockCursor(false)
  FreezeCursor(false)
  del_object_from_game(-10000002)
  -- FreeCustomCursor()
  ShowCursor(true)
  SetCustomCursor(10000002)
  ReturnHiddenObjects()
  DeleteRenderTarget(renderTarget)
  objectsToRender = {}  
end


------------------------------------------------------------
--  function: giveMeHint
-- рисует частички подсказки
------------------------------------------------------------
function giveMeHint(id,emiter)
  if not hintRuning then
    playeffect("hint")
    StartAnimation(7000019,0,9) -- и глаз открывает
    hintReady = false

    if emiter then
      hintEmiter = emiter
    else
      hintEmiter = "HINT_FTW"
    end

    local x,y = GetObjectPosition(id)
    local a,b = GetObjectSize(id)
    hint_poly_table = {1,1,2,1,2,2}
    CreatePolygon(-567,3,"hint_poly_table")
    speciale_action(4,-567)
    -- put={"InstantiationOfLine", 2, 1024,768, x,y,6, true};
    -- StartTravel(-567,1,nil,"hintEmiter",put)
    AddParticleToObject(-567,hintEmiter)
    MoveEmiter(hintEmiter,x+a/2,y+b/2)
    StartEmiter(hintEmiter)
    -- RotateParticle(hint_objg,"hintEmiter",2,1);
    StartTimer(5628,hintTime,"hintWasGoodEnough",true)
    hintRuning = true
  end
end


------------------------------------------------------------
--  function: hintWasGoodEnough
-- прекращает рисовать частички подсказки
------------------------------------------------------------
function hintWasGoodEnough()
  if hintRuning then
    StopEmiter(hintEmiter,true)
    hintRuning = false
    hintID = nil
  end
end


------------------------------------------------------------
--  function: showHint
-- подсказка. вызывает рендерТаргет, частицы - всё как положено
-- только на хидденах вызывается
------------------------------------------------------------
function showHint(t,id)
  --out("\tID="..tostring(id).."\n")
  if not (rendering or hiddenending) then
    if id == -1 then
      SelectRandom()
      return
    end
    if not hintID then
      hintID = id
      hintprocessing=true
      StartTimer(2340,hintTime,"hideHint")
      SetShtrafFunc("dummy",10,30)
      rendering=true
      GetRenderTarget(renderTarget,"renderTargetCB")
      objectsToRender = {id,renderTarget,-567}
      for i,v in pairs(obj) do
        if obj[i].id == id then
          if obj[i].shadow then
            table.insert(objectsToRender,obj[i].shadow)
            break
          end
        end
      end
    end
  end
end;


------------------------------------------------------------
--  function: renderTargetCB
-- callback для рендерТаргета
------------------------------------------------------------
function renderTargetCB(texName,texID)
  --out("renderTargetCB("..tostring(hintID)..","..tostring(texName)..","..tostring(texID)..")\n")
  -- FlipVertical(renderTarget)
  -- SetObjectPosition(renderTarget,0,-256)
  -- SetAlfa(renderTarget,0,1,0)
  -- ActivateObject(renderTarget,true)
  -- FlipVertical(renderTarget)
  -- SetObjectPosition(renderTarget,0,-256)
  SetAlfa(renderTarget,0,1,0)
  LeaveObjectsOnReder("objectsToRender")
  --Postprocess(renderTarget,1)  
  if hintID then
    speciale_action(4,hintID)
    giveMeHint(hintID)
    hintID = nil
  end
  if shtrafing then
    BlockCursor(true)
    shtrafing=false
    FreeCustomCursor()
    SetObjectPosition(10000002,2000,2000)
    ShowCursor(false)
    FreezeCursor(true)
    local x,y = GetCursorPos()
    addstaticobjecttogame(-10000002,"cursor",x,y,"")
    speciale_action(4,-10000002)
    SetAlfa(-10000002,1,0,4,"shtraf1")  
    tab={0,0,1,0,1,1,0,1}
    CreatePolygon(507,4,"tab")
    CreatePolygon(508,4,"tab")
    CreatePolygon(509,4,"tab")
    CreatePolygon(510,4,"tab")
    local a,b = 325,275
    AddParticleToObject(509,"top_left1")
    MoveEmiter("top_left1",a,b)
    StartEmiter("top_left1")

    AddParticleToObject(507,"top_right1")
    MoveEmiter("top_right1",1024-a,b)
    StartEmiter("top_right1")

    AddParticleToObject(508,"bottom_right1")
    MoveEmiter("bottom_right1",1024-a,768-b)
    StartEmiter("bottom_right1")

    AddParticleToObject(510,"bottom_left1")
    MoveEmiter("bottom_left1",a,768-b)
    StartEmiter("bottom_left1")    
    
  end;
  rendering=false
  -- LeaveObjectsOnReder("objectsToRender")
  -- BlackAndWite(renderTarget)
  -- Postprocess(renderTarget,1)
  -- StartAnimation(12345000)
  -- speciale_action(4,12345000)
end


------------------------------------------------------------
--  function: hideHint
-- прекращаем показывать подсказку
------------------------------------------------------------
function hideHint()
  SetShtrafFunc("Shtrafing",10,30)
  hintprocessing=false
  ReturnHiddenObjects()
  DeleteRenderTarget(renderTarget)
  objectsToRender = {}
end


------------------------------------------------------------
--  function: showFlags
-- этой функцыей можно посмотреть все флаги
------------------------------------------------------------
function showFlags()
  for key,value in pairs(flags) do
    out(tostring(key).." = "..tostring(value).."\n")
  end
end


------------------------------------------------------------
--  function: CreateWindow
-- окошко награды
------------------------------------------------------------

function HaveToSetAlfa()
SetAlfa(AlfaItem,1,0,0.3);
end;

function CreateWindow(text,winId,bg,item,align)
  
  ------ GENERIC
  local defWinId     = 1000024
  local textId    = 1000025
  local itemId    = 1000026
  local defBG      = "message"
  local freeWidth    = 1024
  local freeHeight  = 650
  local textLeftX    = 315
  local textLeftY    = 170
  local textCenterX  = 512
  local textCenterY  = 440
  local margins    = 155
  local itemX      = 451
  local itemY      = 260
     local x,y = 0,0 
    local a,b = 0,0 
    local tobig=false 
  ------ END GENERIC
  
      function messageGameInput(sender,obj_id)
        if sender == 6 then
  
        del_object_from_game(winId)
        del_object_from_game(textId)
    
        StopTravel(itemId);
        do
          if not letitmed then
            local x1,y1=GetObjectPosition(itemId);
            local x2,y2,_,_=GetNewElementPlaceAndSize("gr");

            BlockCursor(true)
            playeffect("collaps")
            StartTravel(itemId,1,nil,"store",{"InstantiationOfFreeParabola",3,x1,y1,x1-20,y1-20,x2,y2,1.5,false},"TravelFinished",true);
            AlfaItem=itemId
            StartTimer(2011,0.4,"HaveToSetAlfa")
          else
            letitmed=false
          end;  
        end;
        setInputFunction()
        gameinput(26,winId,-1)
      end
      if sender==41 and obj_id==itemId then
        StartTravel(itemId,2,nil,nil,travel_obj1,travel_obj2);
      end
      -- if sender==29 and obj_id==itemId then
        -- if tobig then
        -- tobig=false
        -- ScaleObject(itemId,1,0.9,3,x+a/2,y+b/2)
        -- else
        -- tobig=true
        -- ScaleObject(itemId,0.9,1,3,x+a/2,y+b/2)
        -- end;
      -- end  
      end
  
  if not winId then
    winId = defWinId
  end
  
  if not bg then
    bg = defBG
  end
  
  addstaticobjecttogame(winId,bg,0,0,"")
  speciale_action(4,winId)
  speciale_action(2,winId)
  speciale_action(2,winId,true,0.5)
  
  local a,b = GetObjectSize(winId)
  local wx = (freeWidth - a)/2
  local wy = (freeHeight - b)/2
  SetObjectPosition(winId,wx,wy)  
  
  local tx,ty = 0,0
  
  if align == 1 then
    -------------
  elseif align == 2 then
    tx,ty = textCenterX,textCenterY
  else
    tx,ty = textLeftX,textLeftY
  end
  
  addtexttogame(textId,tx,ty,text,a-margins)
  speciale_action(4,textId)
  set_text_property(textId,100,100,align)
  setcolor(textId,0,0,0,1,1)
  set_text(textId,text)
  
  if item ~= nil then 
    addstaticobjecttogame(itemId,item,itemX,itemY,"")
    speciale_action(4,itemId)
   -- x,y = GetObjectPosition(item)
  -- a,b = GetObjectSize(item)
-- SetHotPoint(item,x+a/2,y+b/2)  
  -- ScaleObject(itemId,1,0.9,3,x+a/2,y+b/2)
    travel_obj1={"InstantiationOfCircleR",3,itemX,itemY,itemX+5,itemY+2, 0, -math.pi,3,false}
    travel_obj2={"InstantiationOfCircleR",3,itemX,itemY,itemX+5,itemY+2, -math.pi,0, 3,false}
    StartTravel(itemId,2,nil,nil,travel_obj1,travel_obj2);
  end
  setInputFunction("messageGameInput")
end


------------------------------------------------------------
--  function: InvitationTutorial
-----------------------------------------------------------
function InvitationTutorial ()
    function invitationGameInput(sender,obj_id)
    sendersProcessing(sender,obj_id,attachedID)
    Write("sender = "..sender.."\n")
      if sender==35 then
        if obj_id==45127 or obj_id==45128 then
          setcolor(obj_id-2,0.8,0.05,0.05,1,1)
        end;
      end;
      if sender==39 then
        if obj_id==45127 or obj_id==45128 then
          setcolor(obj_id-2,0.3,0.05,0.05,1,1)
        end;
      end;      
      if sender == 1  then
        
        if obj_id==45127 then
          flags.ShowTutorial=true
          del_object_from_game(45123)
          del_object_from_game(45124)
          del_object_from_game(45125)
          del_object_from_game(45126)
          setInputFunction()
          gameinput(154)            
        return;end;
        if obj_id==45128 then
          flags.ShowTutorial=false
          del_object_from_game(45123)
          del_object_from_game(45124)
          del_object_from_game(45125)
          del_object_from_game(45126)
          setInputFunction()
          gameinput(154)            
        return;end;      
      
      end
    end
  addstaticobjecttogame(45123,"tutorial_win",264,270,"")
  speciale_action(4,45123)
  speciale_action(2,45123)
  speciale_action(2,45123,true,0.5)
  winw,winh=GetObjectSize(45123)

  addtexttogame(45124,512,340,"10002")
  speciale_action(4,45124)
  set_text_property(45124,100,100,2)
  setcolor(45124,0.3,0.05,0.05,1,1)
  set_text(45124,"10002")  
  
  addtexttogame(45125,400,400,"10003")
  speciale_action(4,45125)
  set_text_property(45125,100,100,2)
  setcolor(45125,0.3,0.05,0.05,1,1)
  set_text(45125,"10003")  
  addtexttogame(45126,600,400,"10004")
  speciale_action(4,45126)
  set_text_property(45126,100,100,2)
  setcolor(45126,0.3,0.05,0.05,1,1)
  set_text(45126,"10004")  
  for_yes={
    370,400,
    430,400,
    430,450,
    370,450
  }
  for_no={
    580,400,
    630,400,
    630,450,
    580,450  
  }  
  CreatePolygon(45127,4,"for_yes") 
  CreatePolygon(45128,4,"for_no") 
    speciale_action(4,45127)
  speciale_action(4,45128)
  OnMouseOver(45127,true)
  OnMouseOver(45128,true)
  -- addactivobjecttogame(45125,"tutorial_arrow",x+winw,y+winh,"")
  -- addactivobjecttogame(45126,"tutorial_arrow1",x-56,y+winh,"")
  -- speciale_action(4,45125)
  -- speciale_action(2,45125)
  -- speciale_action(2,45125,true,0.5)  
  setInputFunction("invitationGameInput")
end;


------------------------------------------------------------
--  function: CreateTutorial
-----------------------------------------------------------

function CloseTutorial()
  del_object_from_game(45123)
  del_object_from_game(45124)
  del_object_from_game(45125)
  tutorial_window=false
  setInputFunction()
  
end;

function CreateTutorial(text,x,y,arr_type)
    function tutorialGameInput(sender,obj_id)
    sendersProcessing(sender,obj_id,attachedID)
      if sender == 1 or sender == 6 then
        CloseTutorial()
        gameinput(sender,obj_id)
        gameinput(154)    
      end
    end
  playeffect("tutorial")
  tutorial_window=true
  addstaticobjecttogame(45123,"tutorial_win",x,y,"")
  speciale_action(4,45123)
  speciale_action(2,45123)
  speciale_action(2,45123,true,0.5)
  winw,winh=GetObjectSize(45123)
  tx=winw/2+x
  ty=y+50
  addtexttogame(45124,tx,ty,text,winw-20)
  speciale_action(4,45124)
  
  setcolor(45124,0.3,0.05,0.05,1,1)
  set_text(45124,text)  
  set_text_property(45124,100,600,2)
  if arr_type==0 then
  addstaticobjecttogame(45125,"tutorial_arrow",x+winw,y+winh,"")
  else
  addstaticobjecttogame(45125,"tutorial_arrow1",x-56,y+winh,"")
  end;
  speciale_action(4,45125)
  speciale_action(2,45125)
  speciale_action(2,45125,true,0.5)  
  setInputFunction("tutorialGameInput")
end; 

------------------------------------------------------------
--  function: CreatePlashechka
-- плашечка
------------------------------------------------------------
timers1223 = 0
function CreatePlashechka(text)
    
  local function closePlashechka()
    --out("closePlashechka()\n")
    set_visible_object(-902,false)
    set_visible_object(-900,false)
    setInputFunction()
    gameinput(121,-1,-1)
  end
  
  function plashechkiGameInput(sender,obj_id,attachedID)
    if sender == 5 or sender == 14 then
      closePlashechka() -- Возможно стОит ущё раз вызвать функцию set_visible_object(-902,false)
    end

    if sender == 36 then
      if obj_id == 1223 then
        timers1223 = timers1223 - 1
        if timers1223 == 0 then
          closePlashechka()
        end
      elseif obj_id == 1222 then
        addtexttogame(-902,512,5,text,875)
        set_text_property(-902,100,250,2)
        setcolor(-902,1,1,1,1,1)
        set_text(-902,text)
        speciale_action(4,-902)
        set_visible_object(-902,true)
      end
    end
    gameinput(sender,obj_id,attachedID)
  end
  
  setInputFunction("plashechkiGameInput")
  addstaticobjecttogame(-900,"plashechka",0,-1000,"")
  set_visible_object(-900,true)
  speciale_action(10,-900)
  speciale_action(4,-900)
  SetAlfaValue(-900,0.75)
  local a,b=GetObjectSize(-900)
  MoveObjectY(-900,-b,0,0.1)
  StartTimer(1222,0.1)
  StartTimer(1223,10)
  timers1223 = timers1223 + 1
end





------------------------------------------------------------
--  function: addSkip
-- добавить кнопку Скип (на комикс)
------------------------------------------------------------
function addSkip()
  addactivobjecttogame(skip3,"skip3",870,20,"","")
  addactivobjecttogame(skip2,"skip1",870,20,"","")
  addactivobjecttogame(skip1,"skip2",870,20,"","")  
  speciale_action(4,skip1)
  speciale_action(4,skip2)
  speciale_action(4,skip3)
  OnMouseOver(skip2)
  set_pixel_check(skip2,false)
  SetAlfaValue(skip2,0.01)
  set_visible_object(skip3,false)
  set_visible_object(skip3,false)
end


------------------------------------------------------------
--  function: AddTextSheet
-- добавляет текстовую вкладку; вызывается на хидденах
------------------------------------------------------------
function AddTextSheet(Width)
  local _Width=686
  local _X=32

  if Width then
    _Width=Width
    _X=17
  end
  SetCurrentRM("menu");
  AddSheet(7700001,"re","menupressed",3000,3000,207,79,"inventory",155,666,_Width,115,_X,29,5,0,0,0,7000034,5);
  AddButtonToSheet("re",7000031,"clues3","clues1","clues2",nil,357,645,188,68,true);
  SetCurrentRM("main");
end;


------------------------------------------------------------
--  function: deleteMiniOutfit
-- убирает кнопки Скип и Хелп с допки
------------------------------------------------------------
function deleteMiniOutfit()
  mini_Skip_gray    = -812
  mini_Skip_prgress  = -811
  mini_Skip_pressed  = -810
  mini_Skip_active  = -809
  mini_Skip      = -808
  mini_Skip_BG    = -807
  mini_Help_pressed  = -806
  mini_Help_active  = -805
  mini_Help      = -804
  
  del_object_from_game(mini_Skip_gray    )
  del_object_from_game(mini_Skip_prgress  )
  del_object_from_game(mini_Skip_pressed  )
  del_object_from_game(mini_Skip_active  )
  del_object_from_game(mini_Skip      )
  del_object_from_game(mini_Skip_BG    )
  del_object_from_game(mini_Help_pressed  )
  del_object_from_game(mini_Help_active  )
  del_object_from_game(mini_Help      )
end;


------------------------------------------------------------
--  function: CreateMiniOutfit
-- добавляет кнопки Скип и Хелп на допку
------------------------------------------------------------
function CreateMiniOutfit()
  mini_Shadow      = -813
  mini_Skip_gray    = -812
  mini_Skip_prgress  = -811
  mini_Skip_pressed  = -810
  mini_Skip_active  = -809
  mini_Skip      = -808
  mini_Skip_BG    = -807
  mini_Help_pressed  = -806
  mini_Help_active  = -805
  mini_Help      = -804
  mini_Close_pressed  = -803
  mini_Close_active  = -802
  mini_Close      = -801
  mini_Ramka      = -800
      
  addstaticobjecttogame(mini_Skip_prgress,"mini_Ramka",373,15 ,"",0  ,46 ,304,28 )
  addstaticobjecttogame(mini_Skip_gray,"mini_Ramka"  ,372,15 ,"",304,46 ,304,28 )  
  addstaticobjecttogame(mini_Skip_pressed,"mini_Ramka",376,15 ,"",444,0  ,294,36 )  --mini_Skip_pressed
  addstaticobjecttogame(mini_Skip_active,"mini_Ramka"  ,451,8  ,"",738,0  ,140,43 )  --mini_Skip_active
  addstaticobjecttogame(mini_Skip,"mini_Ramka"    ,482,14 ,"",878,0  ,86 ,37 )  --mini_Skip
  addstaticobjecttogame(mini_Skip_BG,"mini_Ramka"    ,304,11 ,"",0  ,0  ,444,46 )  --mini_Skip_BG
  addstaticobjecttogame(mini_Help_pressed,"mini_Ramka",113,63 ,"",878,37 ,70 ,51 )  --mini_Help_pressed
  addstaticobjecttogame(mini_Help_active,"mini_Ramka"  ,113,63 ,"",0  ,622,68 ,49 )  --mini_Help_active
  addstaticobjecttogame(mini_Help,"mini_Ramka"    ,113,63 ,"",948,37 ,70 ,51 )  --mini_Help
    
  set_visible_object(mini_Skip_pressed,false)
  SetAlfaValue(mini_Skip_active,0.01)
  set_visible_object(mini_Help_pressed,false)
  SetAlfaValue(mini_Help_active,0.01)
  
  speciale_action(4,mini_Help)
  speciale_action(4,mini_Help_active)
  speciale_action(4,mini_Help_pressed)
  speciale_action(4,mini_Ramka)
  
  speciale_action(4,mini_Skip_gray)
  speciale_action(4,mini_Skip_prgress)
  speciale_action(4,mini_Skip_BG)
  speciale_action(4,mini_Skip)
  speciale_action(4,mini_Skip_active)
  speciale_action(4,mini_Skip_pressed)
  
  StartProgress(mini_Skip_prgress,skipTime,true)
  
  -- speciale_action(2,mini_Skip)
  
  OnMouseOver(mini_Help_active)
end;


------------------------------------------------------------
--  function: CreateDopLocationOutfit
-- рамка допки
------------------------------------------------------------
function CreateDopLocationOutfit()

  mini_Shadow      = -813
  mini_Skip_gray    = -812
  mini_Skip_prgress  = -811
  mini_Skip_pressed  = -810
  mini_Skip_active  = -809
  mini_Skip      = -808
  mini_Skip_BG    = -807
  mini_Help_pressed  = -806
  mini_Help_active  = -805
  mini_Help      = -804
  mini_Close_pressed  = -803
  mini_Close_active  = -802
  mini_Close      = -801
  mini_Ramka      = -800
  
  addstaticobjecttogame(mini_Ramka,"mini_Ramka"      ,92 ,41 ,"",0  ,74 ,858,548)  --mini_Ramka
  addstaticobjecttogame(mini_Close_pressed,"mini_Ramka"  ,860,63 ,"",878,88 ,70 ,51 )  --mini_Close_pressed
  addstaticobjecttogame(mini_Close_active,"mini_Ramka"  ,860,63 ,"",68 ,622,68 ,49 )  --mini_Close_active
  addstaticobjecttogame(mini_Close,"mini_Ramka"      ,860,63 ,"",0  ,671,70 ,51 )  --mini_Close
  addstaticobjecttogame(mini_Shadow,"mini_Ramka_shadow"  ,100 ,45 ,"")
  
  SetAlfaValue(mini_Shadow,0.7)
    
  set_visible_object(mini_Close_pressed,false)
  SetAlfaValue(mini_Close_active,0.01)
  OnMouseOver(mini_Close_active)  
  speciale_action(10,mini_Ramka)
  speciale_action(10,mini_Shadow)
end;


------------------------------------------------------------
--  function: MiniInterfaceHandler
-- обработчик интерфейса допок; вызывается в гейминпуте на допках
------------------------------------------------------------
function MiniInterfaceHandler(sender,x,y,fSoftBlock)
  if not SoftBlock then
    if threeStateButton(sender,x,y,mini_Close_active,mini_Close_pressed) then
      -- removeFromMouse(y)
      CloseSatelite()
    end
    if threeStateButton(sender,x,y,mini_Help_active,mini_Help_pressed) then
      CreatePlashechka(helpID)
    end
    if skipActive and threeStateButton(sender,x,y,mini_Skip_active,mini_Skip_pressed) then
      finish_mini_game(true)
    end
    if sender==6 then
      local x0,y0=GetCursorPos()

      if (x0<92) or (y0<15) or (x0>92+858) or (y0>15+575) and not Journal.visible then
        CloseSatelite()
      end
    end
  end
  if sender==37 then
    skipActive=true
    OnMouseOver(mini_Skip_active)
  end
end


------------------------------------------------------------
--  function: wrongItemWarning
-- эта функция выводит предупреждение о том, что мы используем неправильный предмет
-- в качестве необязательных параметров нужно передавть айдишники или имена объектов в инвентаре,
-- которые можно применять на этой локации
------------------------------------------------------------
function wrongItemWarning(sender, obj_id, attachedID, ...)
  local n = #arg
  if sender == 1 and obj_id > 0 and attachedID ~= -1 then
    Write("wrongItemWarning("..sender..","..obj_id..","..attachedID..")\n")
    for i,v in pairs(obj) do
      if obj_id == obj[i].id then
        if obj[i].notice then
          return false
        end
      end
    end
    if n > 0 then
      for i=1,n do
        if obj[arg[i]] then
          if attachedID == obj[arg[i]].id then
            return false
          end
        end
        if attachedID == arg[i] then
          return false
        end
      end
    end
    Write("Wrong item\n",true)
    CreatePlashechka("10000","he")
    return true
  else
    return false
  end
end
function TravelFinished(nCount,Param1)
  del_object_from_game(Param1);
  ShowSheet("gr","move",2000,true);
  StartTimer(2011,1.5,"Bell");
end;
function Bell()
  BlockCursor(false)
  addToInventory();
  if flags.IsSatellite then
    CloseSatelite();
  end; 
  if flags.is_hidden_loc then
    neighbors[gamedata.current_level].obj_created = false
    CallbackForSheetButton(nCount);
  end  
end;