Journal = {}
Journal.pages = {}
Journal.threads = {}
Journal.id = -61000
Journal.currentPage = 0
Journal.visible = false

Journal.emptyPage =

-- теперь, когда создается новый разворот, для него грузится прозрачная, подложка - та, которую видно, если фотка не добавлена
-- есть такие развороты, которые заполняются сразу целиком, поэтому:
-- если для разворота не нужно грузить прозрачную подложку, то просто оставляем пустую функцию в таблице
{
  [1]  = function() end; -- как эта
  [2]  = function() Journal:addPicture(2 ,"page_2" ,129,23 ,0,0,752,469) end;
  [3]  = function() Journal:addPicture(3 ,"page_3" ,156,61 ,0,0,860,540) end;
  [4]  = function() Journal:addPicture(4 ,"page_4" ,130,44 ,0,0,772,526) end;
  [5]  = function() Journal:addPicture(5 ,"page_5" ,83 ,51 ,0,0,844,569) end;
--  [6]  = function() Journal:addPicture(6 ,"page_6" ,148,44 ,0,0,809,392) end;
  [6]  = function() Journal:addPicture(6 ,"page_6" ,130,60 ,0,0,780,472) end;
  [7]  = function() Journal:addPicture(7 ,"page_7" ,130,62 ,0,0,822,551) end;
  [8]  = function() Journal:addPicture(8 ,"page_8" ,111,82 ,0,0,776,535) end;
  [9] = function() Journal:addPicture(9,"page_9",149,154,0,0,758,466) end;
  [10] = function() Journal:addPicture(10,"page_10",122,223,0,0,777,355) end;
  -- [11] = function() Journal:addPicture(11,"page_11",22,23,0,0,777,355) end;
  [11] = function() Journal:addPicture(11,"page_11",219,152,0,0,694,413) end;
  [12] = function() Journal:addPicture(12,"page_12",139,208,0,0,783,436) end;
  [13] = function() Journal:addPicture(13,"page_13",147,137,0,0,779,443) end;
  [14] = function() Journal:addPicture(14,"page_14",137,183,0,0,807,400) end;
  [15] = function() Journal:addPicture(15,"page_15",126,71 ,0,0,841,526) end;
  [16] = function() Journal:addPicture(16,"page_16",140,61 ,0,0,884,551) end;
  [17] = function() Journal:addPicture(17,"page_17",82 ,63 ,0,0,791,501) end;
}


loadresource("data\\journal\\journal.png","journal")


------------------------------------------------------------
--  function: Journal:click
-- по кнопке
------------------------------------------------------------
function Journal:click()
if not journalprocessing then
if tutorial_window then
  CloseTutorial()
  gavetocltut=true
end;
playeffect("journal1")
  if Journal.visible then
  
    Journal:hideJournal()

  else
    Journal:showJournal()
    --freeCustomCursor()
    --out("ShowJournal")
  end
end;  
end


------------------------------------------------------------
--  function: Journal:loadPage
------------------------------------------------------------
function Journal:loadPage(n)
out("loadPage \n")
  if Journal.pages[n] and not Journal.pages[n].loaded and Journal.pages[n].thread == 0 then
    local thread = PreLoadResource("./data/0/global_res.lua","page"..n,"loadPageCallback")
    Journal.pages[n].thread = thread
    Journal.threads[thread] = n
    Write("loading Page: "..n.."\n")
  else
    Write("loading Page: "..n.." failed\n")
  end
end


------------------------------------------------------------
--  function: loadPageCallback
------------------------------------------------------------
function loadPageCallback(thread)
out("loadPageCallback \n")
  if Journal.threads[thread] then
    Write("Journal.threads[thread] = "..Journal.threads[thread].."\n")
    Journal.pages[Journal.threads[thread]].loaded = true
    Journal.pages[Journal.threads[thread]].thread = 0
    Write("LOADED PAGE "..Journal.threads[thread]..";   thread="..thread.."\n")
    table.remove(Journal.threads,thread)
    
  end
end


------------------------------------------------------------
--  function: Journal:unloadPage
------------------------------------------------------------
function Journal:unloadPage(n)
out("unloadPage \n")
  if Journal.pages[n] and Journal.pages[n].loaded and Journal.pages[n].thread == 0 --[[and n ~= table.maxn(Journal.pages)]] then
    local thread = DeleteResources("./data/0/global_res.lua","page"..n,"unloadPageCallback")
    Journal.pages[n].thread = thread
    Journal.threads[thread] = n
    Write("deleting Page: "..n.."\n")
  else
    Write("deleting Page: "..n.." failed\n")
  end
end


------------------------------------------------------------
--  function: unloadPageCallback
------------------------------------------------------------
function unloadPageCallback(thread)
out("unloadPageCallback \n")
  if Journal.threads[thread] then
    Journal.pages[Journal.threads[thread]].loaded = false
    Journal.pages[Journal.threads[thread]].thread = 0
    Write("\tDELETED PAGE "..Journal.threads[thread]..";   thread="..thread.."\n")
    table.remove(Journal.threads,thread)
    -- Journal.hideJournal()
  end
  
end


------------------------------------------------------------
--  function: Journal:showJournal
------------------------------------------------------------
function Journal:showJournal()
  BlockCursor(true)
  journalprocessing=true
  out("ShowJournal \n")
  StopEmiter("bookopen",true);
  
  -- Journal.currentPage = table.maxn(Journal.pages)
  if not pages_loaded then
    pages_loaded=true
    out("Vivodim obecti jurnala \n")
    addstaticobjecttogame(-60000,"journal",49,31,"",0,0,947,641) -- фон
    addactivobjecttogame(-60001,"journal",120,541,"","",0,0,0,641,81,81) -- листалка влево
    addactivobjecttogame(-60002,"journal",853,532,"","",0,0,81,641,81,81) -- листалка вправо
    addstaticobjecttogame(-60005,"journal",931,89,"",947,38,47,48) -- наведение
    addstaticobjecttogame(-60006,"journal",937,96,"",947,0,38,38) -- нажатие
    speciale_action(4,-60000)
    speciale_action(4,-60001)
    speciale_action(4,-60002)
    speciale_action(4,-60005)
    speciale_action(4,-60006)  
    OnMouseOver(-60001)
    OnMouseOver(-60002)
    OnMouseOver(-60005)  
    set_pixel_check(-60001,false)
    set_pixel_check(-60002,false)  
    SetAlfaValue(-60001,0.01)
    SetAlfaValue(-60002,0.01)
    SetAlfaValue(-60005,0.01)
    set_visible_object(-60006,false)
  end;
  if not Journal.pages[Journal.currentPage].loaded and Journal.pages[Journal.currentPage].thread == 0 then
    Journal:loadPage(Journal.currentPage)
    checkPageLoading()
    return
  end
  
  Journal:showPage(Journal.currentPage)
  Journal:loadPage(Journal.currentPage-1)
  Journal:loadPage(Journal.currentPage+1)  
  local n1 = 1000 + Journal.currentPage*2 - 1
  local n2 = 1000 + Journal.currentPage*2  
  addtexttogame(-60003,149,570,tostring(n1))
  addtexttogame(-60004,882,560,tostring(n2))
  setcolor(-60003,0.2,0.2,0.2,1,1)
  setcolor(-60004,0.2,0.2,0.2,1,1)
  speciale_action(4,-60003)
  speciale_action(4,-60004)
  StartTimer(timerID+1,0.1,"FinishShow")


end

function FinishShow()
freeCustomCursor()
-- ClearCursor()
  Journal.visible = true  
  journalprocessing=false
  setInputFunction("mygameinput")
  BlockCursor(false)
end;

------------------------------------------------------------
--  function: checkPageLoading
------------------------------------------------------------
function checkPageLoading()          -- Loaded? ORLY?
out("checkPageLoading \n")
  if Journal.pages[Journal.currentPage].thread == 0 then
    if not Journal.pages[Journal.currentPage].loaded then        
      Journal:loadPage(Journal.currentPage)
      StartTimer(7881,0.1,"checkPageLoading")
    else
    Journal:showJournal()
    end
  else
    StartTimer(7880,0.1,"checkPageLoading")
  end
end


------------------------------------------------------------
--  function: Journal:hideJournal
------------------------------------------------------------

function FinishHide()
  Journal.visible = false
  pages_loaded=false
  setInputFunction()
  journalprocessing=false
    if gavetocltut then
      gavetocltut=false
      gameinput(154)  
    end;    
  Write("hideJournal\n")
end;

function Unload()
  del_object_from_game(-60000)
  del_object_from_game(-60001)
  del_object_from_game(-60002)
  del_object_from_game(-60003)
  del_object_from_game(-60004)
  del_object_from_game(-60005)
  del_object_from_game(-60006)
  Journal:unloadPage(Journal.currentPage+1)
  Journal:unloadPage(Journal.currentPage)
  Journal:unloadPage(Journal.currentPage-1)
  StartTimer(timerID+1,0.1,"FinishHide")

end;

function Journal:hideJournal()
if Journal.visible then
out("hideJournal "..Journal.currentPage.." \n")
journalprocessing=true
  -- prev=Journal.currentPage-1  
  -- nextp=Journal.currentPage+1
  -- if Journal[currentPage-1].loaded then
    -- Journal:unloadPage(prev)
  -- return;end;
  -- if Journal.nextp.loaded then
      -- Journal:unloadPage(nextp)
  -- return;end;
  -- if Journal.currentPage.loaded then
    -- Journal:unloadPage(Journal.currentPage)
  -- return;end;
  
  Journal:hidePage(Journal.currentPage)
  StartTimer(timerID+1,0.1,"Unload")
  -- set_visible_object(-60000,false)

end;  
end


------------------------------------------------------------
--  function: Journal:showPage
------------------------------------------------------------
function Journal:showPage(n)
out("showPage \n")
  local a = table.maxn(Journal.pages[n].picture)
  for i = 1,a do
    local ID = Journal.pages[n].picture[i].id
    local resource = Journal.pages[n].picture[i].resource
    local x = Journal.pages[n].picture[i].x
    local y = Journal.pages[n].picture[i].y
    local xtex = Journal.pages[n].picture[i].xtex
    local ytex = Journal.pages[n].picture[i].ytex
    local width = Journal.pages[n].picture[i].width-10
    local height = Journal.pages[n].picture[i].height
    addstaticobjecttogame(ID,resource,x,y,"",xtex,ytex,width,height)
    speciale_action(4,ID)
    set_visible_object(ID,true)
  end
  local b = table.maxn(Journal.pages[n].text)
  for i = 1,b do
    local ID = Journal.pages[n].text[i].id
    local text = Journal.pages[n].text[i].text
    local x = Journal.pages[n].text[i].x
    local y = Journal.pages[n].text[i].y
    local width = 290--Journal.pages[n].text[i].width
    addtexttogame(ID,x,y,text,width)
    set_text_property(ID,60,40,3)
    speciale_action(4,ID)
    set_visible_object(ID,true)
  end
  local n1 = 1000 + Journal.currentPage*2 - 1
  local n2 = 1000 + Journal.currentPage*2
  set_text(-60003,tostring(n1))
  set_text(-60004,tostring(n2))
  Write("showPage\n")
end


------------------------------------------------------------
--  function: Journal:hidePage
------------------------------------------------------------
function Journal:hidePage(n)
out("hidePage \n")
  local a = table.maxn(Journal.pages[n].picture)
  for i = 1,a do
    local ID = Journal.pages[n].picture[i].id
    set_visible_object(ID,false)
    del_object_from_game(ID)
  end
  local b = table.maxn(Journal.pages[n].text)
  for i = 1,b do
    local ID = Journal.pages[n].text[i].id
    set_visible_object(ID,false)
    del_object_from_game(ID)
  end
  Write("hidePage\n")
end


------------------------------------------------------------
--  function: Journal:addPage
------------------------------------------------------------

function StopBook()
out("StopBook \n")
StopEmiter("bookopen",true);
end;
function Journal:addPage()
out("addPage \n")
playeffect("pencil")
  table.insert(Journal.pages,{})
  local n = table.maxn(Journal.pages)
  Journal.pages[n].picture = {}
  Journal.pages[n].text = {}
  Journal.pages[n].resources = "page"..tostring(n)
  Journal.pages[n].loaded = false
  Journal.pages[n].thread = 0
  Journal:loadPage(n)
  Journal:unloadPage(n-1)
  Journal.currentPage = n
  Journal.emptyPage[n]()
  Write("addPage: "..Journal.pages[n].resources.."\n")
end


------------------------------------------------------------
--  function: Journal:addPicture
------------------------------------------------------------
function Journal:addPicture(page, resource, x, y, xtex, ytex, width, height)
out("addPicture \n")
  while not Journal.pages[page] do
    Journal:addPage()
  end
  table.insert(Journal.pages[page].picture,{})
  local n = table.maxn(Journal.pages[page].picture)
  Journal.pages[page].picture[n].id = Journal.id
  Journal.id = Journal.id - 1
  Journal.pages[page].picture[n].resource = resource
  Journal.pages[page].picture[n].x = x
  Journal.pages[page].picture[n].y = y
  Journal.pages[page].picture[n].xtex = xtex
  Journal.pages[page].picture[n].ytex = ytex
  Journal.pages[page].picture[n].width = width
  Journal.pages[page].picture[n].height = height
  Write("addPicture: "..resource..", "..x..", "..y..", "..xtex..", "..ytex..", "..width..", "..height.."\n")
end


------------------------------------------------------------
--  function: Journal:addText
------------------------------------------------------------
function Journal:addText(page, text, x, y, width)
out("addText \n")
  
  AddParticleToObject(7000033,"bookopen",80,700);
  MoveEmiter("bookopen",80,700);
  StartEmiter("bookopen");
  StartTimer(643932,7,"StopBook",true)
  while not Journal.pages[page] do
    Journal:addPage()
  end
  table.insert(Journal.pages[page].text,{})
  local n = table.maxn(Journal.pages[page].text)
  Journal.pages[page].text[n].id = Journal.id
  Journal.id = Journal.id - 1
  Journal.pages[page].text[n].text = text
  Journal.pages[page].text[n].x = x
  Journal.pages[page].text[n].y = y
  Journal.pages[page].text[n].width = width
  Write("addText: "..text..", "..x..", "..y..", "..width.."\n")
end


------------------------------------------------------------
--  function: Journal:changePage
------------------------------------------------------------
function Journal:changePage(arg)
out("changePage \n")
  local k = nil
  if arg then
    k = 1
  else
    k = -1
  end
  if Journal.pages[Journal.currentPage + k] and Journal.pages[Journal.currentPage + k].loaded then
  playeffect("journal1")
    Journal:hidePage(Journal.currentPage)
    Journal.currentPage = Journal.currentPage + k
    Journal:showPage(Journal.currentPage)
    Journal:loadPage(Journal.currentPage+k)
    Journal:unloadPage(Journal.currentPage-2*k)  
    Write("changePage\n")
  end
end


------------------------------------------------------------
--  function: Journal:mygameinput
------------------------------------------------------------

function UnChange()
    BlockCursor(false)
    changing=false
end;

function mygameinput(sender,arg1,arg2)
out("Sender is "..sender.."\n")
-- if sender==5 or sender==35 or sender==39 or sender==1 or sender==36 then
if sender~=6 then
  if twoStateButton(sender,arg1,arg2,-60001) then
    Journal:changePage(false)-- влево
    BlockCursor(true)
    changing=true
    StartTimer(2133456,0.5,"UnChange")
  return;end
  if twoStateButton(sender,arg1,arg2,-60002) then
    Journal:changePage(true)-- вправо
    BlockCursor(true)
    changing=true
    StartTimer(2133456,0.5,"UnChange")
  return;end
  if threeStateButton(sender,arg1,arg2,-60005,-60006) then
    Journal:click()-- крестик
  return;end
else
--if sender==6 then
if not changing then
out("Click \n")
Journal:click()
end;
--end;
end;  
end



-- Write(tostring(pcall((loadresource),("data\\xz"),("xz"))).."\n")
-- assert(loadstring("Journal:prevPage()"))()

-- param = true

-- function coc()
  -- StartTimer(12345678,1,"coco")
-- end

-- function coco()
  -- param = false
-- end

-- Write("\n\tstart\n\n")

-- c = coroutine.create(coc)

-- coroutine.resume(c)

-- while(param) do 
  -- Write("1")
-- end

-- Write("\n\tend\n\n")

-- set_text_property(id,stroki,slova,align)


--[[
-- PAGE 1
Journal:addText(1,"9001",166,91,330)
Journal:addPicture(1,"page1",534,69,0,0,414,460)
Journal:addPicture(1,"page1",163,289,414,0,355,273);

-- PAGE 2
Journal:addText(2,"9002",163,423,330)
Journal:addText(2,"9003",587,503,330)
Journal:addPicture(2,"page2",129,23,0,0,451,449);
Journal:addPicture(2,"page2",575,127,451,0,306,365);

-- PAGE 3
Journal:addText(3,"9004",575,131,330)
Journal:addPicture(3,"page3",554,225,0,0,349,342);
Journal:addPicture(3,"page3",128,44,349,0,391,492);

-- PAGE 4
Journal:addText(4,"9005",170,331,330)
Journal:addPicture(4,"page4",156,61,0,0,860,540);

-- PAGE 5
Journal:addText(5,"9006",168,451,330)
Journal:addText(5,"9007",561,105,330)
Journal:addPicture(5,"page5",561,60,0,0,367,560);
Journal:addPicture(5,"page5",83,51,0,560,443,376);

-- PAGE 6
Journal:addText(6,"9008",148,382,330)
Journal:addText(6,"9009",542,400,330)
Journal:addPicture(6,"page6",147,44,0,0,810,392);

-- PAGE 7
Journal:addText(7,"9010",180,489,330)
Journal:addText(7,"9011",563,126,330)
Journal:addPicture(7,"page7",130,60,0,0,428,453);
Journal:addPicture(7,"page7",531,231,428,0,379,301);

-- PAGE 8
Journal:addText(8,"9012",164,335,330)
Journal:addText(8,"9013",567,98,330)
Journal:addPicture(8,"page8",130,62,0,0,370,271);
Journal:addPicture(8,"page8",201,112,0,271,751,501);

-- PAGE 9
Journal:addText(9,"9014",145,120,330)
Journal:addText(9,"9015",558,344,330)
Journal:addPicture(9,"page9",548,82,0,0,339,250);
Journal:addPicture(9,"page9",111,198,339,0,430,419);

-- PAGE 10
Journal:addText(10,"9016",168,98,330)
Journal:addText(10,"9017",555,99,330)
Journal:addPicture(10,"page10",149,153,0,0,758,467);

-- PAGE 11
Journal:addText(11,"9018",154,111,330)
Journal:addText(11,"9019",542,121,330)
Journal:addPicture(11,"page11",122,223,0,0,777,388);

-- PAGE 12
Journal:addText(12,"9020",155,118,330)
Journal:addText(12,"9021",200,525,330)
Journal:addPicture(12,"page12",219,71,0,0,694,494);

-- PAGE 13
Journal:addText(13,"9022",156,111,330)
Journal:addText(13,"9023",569,111,330)
Journal:addPicture(13,"page13",139,210,0,0,368,434);
Journal:addPicture(13,"page13",525,208,368,0,397,378);

-- PAGE 14
Journal:addText(14,"9024",148,129,330)
Journal:addText(14,"9025",549,126,330)
Journal:addPicture(14,"page14",147,206,0,0,339,333);
Journal:addPicture(14,"page14",517,137,339,0,409,443);

-- PAGE 15
Journal:addText(15,"9026",148,112,330)
Journal:addText(15,"9027",550,88,330)
Journal:addPicture(15,"page15",137,183,0,0,807,400);

-- PAGE 16
Journal:addText(16,"9028",144,122,330)
Journal:addText(16,"9029",562,426,330)
Journal:addPicture(16,"page16",521,71,0,0,446,483);
Journal:addPicture(16,"page16",126,230,446,0,417,367);

-- PAGE 17
Journal:addText(17,"9030",145,106,330)
Journal:addText(17,"9031",150,493,330)
Journal:addText(17,"9032",562,364,330)
Journal:addPicture(17,"page17",140,225,0,0,319,387);
Journal:addPicture(17,"page17",536,61,319,0,488,295);

-- PAGE 18
Journal:addText(18,"9033",176,450,330)
Journal:addText(18,"9034",556,164,330)
Journal:addPicture(18,"page18",606,235,0,0,267,329);
Journal:addPicture(18,"page18",82,63,267,0,473,384);
]]