require "scripts/style"

gCurscreen = 1;
gCurpage = 1;

function ShowArrowButtons( show )
	SetWindowVisible("leftbutton",show)
	SetWindowVisible("rightbutton",show)
end

function UpdateArrows()
	--if(gCurscreen > 1) then
	if(IsAccessible(gCurscreen - 1)) then
		SetWindowVisible("leftbutton",true)
	else
		SetWindowVisible("leftbutton",false)
	end
	--if(gCurscreen < 9) then
	if(IsAccessible(gCurscreen + 1) and gCurscreen < 7) then
		SetWindowVisible("rightbutton",true)
	else
		SetWindowVisible("rightbutton",false)
	end
end

function UpdatePageNum()
	if(gCurscreen == 1) then
		if(IsAccessible(8)) then
			SetLabel("pagenum","1 / 4");
		else
			SetLabel("pagenum","1 / 2");
		end
	elseif(gCurscreen == 5) then
		if(IsAccessible(9)) then
			SetLabel("pagenum","1 / 3");
		else
			SetLabel("pagenum","1 / 1");
		end			
	elseif(GetString("casefile|"..gCurscreen.."b") ~= "#####") then
		SetLabel("pagenum","1 / 2");
	else
		SetLabel("pagenum","1 / 1");
	end
end

function SetCurrentCFPage(pagenum)
	icon = pagenum - 1
	SetPage(icon)
end

CasefileText = {
	parent=DefaultStyle,
	font = {
		standardFont,
		34,
		BlackColor
	},

	align=kHAlignLeft+kVAlignTop,
};

MakeDialog
{
name="casefile",
CaseFileWindow{

	x=0,y=0,w=1024,h=768,
	rows=gRows,			
	columns=gColumns,
	
	SetStyle(CasefileText),
	Text{
		name = "case1",
		label = "casefile|1",
		x=550,y=150,
		w=318,h=420,
		},
	Text{
		name = "pagenum",
		x=kCenter+250,y=kCenter+170,
		w=150,h=50,
		},

	SetStyle(ButtonStyle),
	Button{
		name="leftbutton",
		graphics={"ui/casefiles/btn_left01.png","ui/casefiles/btn_left00.png","ui/casefiles/btn_left00.png"},
		x=kCenter-300,
		y=kCenter+240,
		command = function()
			if(gCurscreen > 1) then
				if(IsAccessible(gCurscreen - 1)) then
					gCurscreen = gCurscreen - 1
					gCurpage = 1
					UpdateArrows()
					UpdatePageNum();
					SetCurrentCFPage(gCurscreen)
					SetLabel("case1",GetString("casefile|"..gCurscreen));
					SetWindowVisible("cleft",false)
					if(GetString("casefile|"..gCurscreen.."b") ~= "#####" and gCurscreen ~= 5) then
						SetWindowVisible("cright",true)
					elseif(gCurscreen == 5 and IsAccessible(9)) then
						SetWindowVisible("cright",true)
					else
						SetWindowVisible("cright",false)
					end
				end
			end	
		end,
		},
	Button{
		name="rightbutton",
		graphics={"ui/casefiles/btn_right01.png","ui/casefiles/btn_right00.png","ui/casefiles/btn_right00.png"},
		x=kCenter+300,
		y=kCenter+240,
		command = function()
			if(gCurscreen < 9) then
				if(IsAccessible(gCurscreen + 1)) then
					gCurscreen = gCurscreen + 1
					gCurpage = 1
					UpdateArrows()
					UpdatePageNum();
					SetCurrentCFPage(gCurscreen)
					SetLabel("case1",GetString("casefile|"..gCurscreen))
					SetWindowVisible("cleft",false)
					if(GetString("casefile|"..gCurscreen.."b") ~= "#####" and gCurscreen ~= 5) then
						SetWindowVisible("cright",true)
					elseif(gCurscreen == 5 and IsAccessible(9)) then
						SetWindowVisible("cright",true)
					else
						SetWindowVisible("cright",false)
					end
				end
			end
		end,
		},
	Button{
		name="cleft",
		graphics={"ui/journal_menu/arrow_in.png","ui/journal_menu/arrow_out.png","ui/journal_menu/arrow_out.png"},
		x=kCenter+100,
		y=kCenter+160,
		sound="",
		command = function()
			if(gCurpage == 4) then
				gCurpage = gCurpage - 1;
				SetLabel("case1",GetString("casefile|"..gCurscreen.."c"));
				SetLabel("pagenum","3 / 4");
				SetWindowVisible("cright",true)
				PlayPageFlipSound()
			elseif(gCurpage == 3) then				
				gCurpage = gCurpage - 1;
				SetLabel("case1",GetString("casefile|"..gCurscreen.."b"));
				if(GetLabel("pagenum") == "3 / 3") then
					SetLabel("pagenum","2 / 3");
				else
					SetLabel("pagenum","2 / 4");
				end
				PlayPageFlipSound()
			elseif(GetString("casefile|"..gCurscreen) ~= "#####") then
				gCurpage = gCurpage - 1;
				SetLabel("case1",GetString("casefile|"..gCurscreen));
				if(GetLabel("pagenum") == "2 / 4") then
					SetLabel("pagenum","1 / 4");					
				elseif(GetLabel("pagenum") == "2 / 3") then
					SetLabel("pagenum","1 / 3");
				else
					SetLabel("pagenum","1 / 2");
				end
				SetWindowVisible("cleft",false)
				SetWindowVisible("cright",true)
				PlayPageFlipSound()
			end	
		end,
		},
	Button{
		name="cright",
		graphics={"ui/journal_menu/arrow_down.png","ui/journal_menu/arrow_up.png","ui/journal_menu/arrow_up.png"},
		x=kCenter+260,
		y=kCenter+160,
		sound="",
		command = function()
			if(gCurscreen == 1 and IsAccessible(8)) then
				if(gCurpage == 1) then
					gCurpage = gCurpage + 1;
					SetLabel("case1",GetString("casefile|"..gCurscreen.."b"));
					SetLabel("pagenum","2 / 4");
					SetWindowVisible("cleft",true)
					PlayPageFlipSound()
				elseif(gCurpage == 2) then
					gCurpage = gCurpage + 1;
					SetLabel("case1",GetString("casefile|"..gCurscreen.."c"));
					SetLabel("pagenum","3 / 4");
					PlayPageFlipSound()
				elseif(gCurpage == 3) then
					gCurpage = gCurpage + 1;
					SetLabel("case1",GetString("casefile|"..gCurscreen.."d"));
					SetLabel("pagenum","4 / 4");
					SetWindowVisible("cright",false)
					PlayPageFlipSound()
				end
			elseif(gCurscreen == 5 and IsAccessible(9)) then
				if(gCurpage == 1) then
					gCurpage = gCurpage + 1;
					SetLabel("case1",GetString("casefile|"..gCurscreen.."b"));
					SetLabel("pagenum","2 / 3");
					SetWindowVisible("cleft",true)
					PlayPageFlipSound()
				elseif(gCurpage == 2) then
					gCurpage = gCurpage + 1;
					SetLabel("case1",GetString("casefile|"..gCurscreen.."c"));
					SetLabel("pagenum","3 / 3");
					SetWindowVisible("cright",false)
					PlayPageFlipSound()
				end				
			elseif(GetString("casefile|"..gCurscreen.."b") ~= "#####" and gCurscreen ~= 5) then
				gCurpage = gCurpage + 1;
				SetLabel("case1",GetString("casefile|"..gCurscreen.."b"));
				if(GetLabel("pagenum") == "1 / 4") then
					SetLabel("pagenum","2 / 4");
				else
					SetLabel("pagenum","2 / 2");
					SetWindowVisible("cright",false)
				end
				SetWindowVisible("cleft",true)
				PlayPageFlipSound()
			end	
		end,
		},

	},--CaseFileWindow
}

SetCurrentCFPage(1);
SetLabel("case1",GetString("casefile|1"));
SetWindowVisible("cright",true);
SetWindowVisible("cleft",false);
UpdatePageNum();
UpdateArrows();
