require "sync"

test0=1;

local function readlist1()
	local fname=GetFolder(ProfileFolder).."\\CharList.txt";
	local f = assert(io.open(fname,"r"))
	local g = f:read("*all")
	f:close()	
	km.setpref(TYPE_STRING,"kmeleon.character.list",g)
end

local function readlist2()
	local fname=GetFolder(ProfileFolder).."\\IETabList.txt";
	local f = assert(io.open(fname,"r"))
	local g = f:read("*all")
	f:close()	
	km.setpref(TYPE_STRING,"kmeleon.IETab.list",g)
end

function OnStartup(hwnd)
	sync()
	-- if getpref(TYPE_BOOL,"kmeleon.startupgroups.enabled")==true then
	--	gstartup()
	-- end
	proxy_menu()
	group_menu()
	undo_menu()
	useragent_sync()
	
	readlist1()
	readlist2()
end

function OnInit(hwnd)
--	if getpref(TYPE_BOOL,"general.firststart")==true then
--		skinSelect()
--		setpref(TYPE_BOOL,"general.firststart",false)
--	end
end	

function OnOpenWindow(hwnd)
--	title, url = GetDocInfo(hwnd)
--	if url=="about:blank" then
--		id("ID_SELECT_URL")
--	else
--		id("ID_SELECT_DOC")
--	end
end


function OnQuit()
--	setpref(TYPE_INT,"kmeleon.session.number",0)
	km.delpref("kmeleon.character.list");
	km.delpref("kmeleon.IETab.list")
end

--[[function OnExit()
	km.delpref("kmeleon.character.list");
	km.delpref("kmeleon.IETab.list");
end --]]
	
--[[ function OnLoad(hwnd)
		title, url = GetDocInfo(hwnd)
		if url~="about:blank" then
			plugin("gestures","view")
		end
end --]]

local function refresh(hwnd)
	local folder=GetFolder(ProfileFolder)
	local fname=folder.."\\chrome\\ad.css"
	local f = assert(io.open(fname,"r"))
	local css = f:read("*all")
	-- alert(css)
	km.InjectCSS(css,true)
	f:close()
end

local function readCharList()
	--local fname=GetFolder(ProfileFolder).."\\CharList.txt";
	--local f = assert(io.open(fname,"r"))
	--local g = f:read("*all")
	--f:close()
	g=km.getpref(TYPE_STRING,"kmeleon.character.list")
	return g;	
end

local function readIETabList()
	--local fname=GetFolder(ProfileFolder).."\\IETabList.txt";
	--local f = assert(io.open(fname,"r"))
	--local g = f:read("*all")
	--f:close()
	g=km.getpref(TYPE_STRING,"kmeleon.IETab.list")
	return g;	
end

local function writeCharList(url)
	local fname=GetFolder(ProfileFolder).."\\CharList.txt";
	local f = assert(io.open(fname,"r"))
	local g = f:read("*all")
	g = g..url.."\n"
	f=assert(io.open(fname,"w"))
	f:write(g)
	--g=readCharList()
	--g=g.."\t"..url
	--km.setpref(TYPE_STRING,"kmeleon.character.list",g)
	f:close()
end

local function writeIETabList(url)
	local fname=GetFolder(ProfileFolder).."\\IETabList.txt";
	local f = assert(io.open(fname,"r"))
	local g = f:read("*all")
	g = g..url.."\n"
	f=assert(io.open(fname,"w"))
	f:write(g)
	--g=readIETabList()
	--g=g.."\t"..url
	--km.setpref(TYPE_STRING,"kmeleon.IETab.list",g)
	f:close()
end

function OnLoad()
	if(getpref(TYPE_BOOL,"kmeleon.prefetch.on")==true) then
		inJS("fetch.js")
	end
	--if km.GetGlobalVar(TYPE_STRING,"CHARSET")~="gb18030" then
	--	km.SetForceCharset("gb18030")
	--end
	local title, url = GetDocInfo(hwnd)
	local g = readCharList()
	if (string.find(g,url) and km.GetGlobalVar(TYPE_STRING,"CHARSET")~="gb18030") then 
		SetForceCharset("gb18030")
	end
end

function ForcePageChar(hwnd)
	local title,url = GetDocInfo(hwnd)
	writeCharList(url)
	km.SetForceCharset("gb2312")
	readlist1()
end

function AddIEtab(hwnd)
	local title,url = GetDocInfo(hwnd)
	writeIETabList(url)
	alert("This address is added into IETab Auto List","IETab", ICON_INFO) 
	readlist2()
end

function ForcePageChar1(hwnd)
	local title,url = GetDocInfo(hwnd)
	writeCharList(url)
	km.SetForceCharset("gb18030")
end

--function OnCreateTab(hwnd)
--	title, url = GetDocInfo(hwnd)
--	alert(url)
--end

--function OnLoading()
--end

function OnLocationChange()
	local title,url=GetDocInfo(hwnd)
	local g=readIETabList()
	if string.find(g,url) then
    		local url1="chrome://plugin/ietab.html?url="..url
    		km.NavigateTo(url1,OPEN_NORMAL)		
		--km.SetGlobalVar(TYPE_STRING,"URLBAR",url)
	end
	--if string.find(url,"chrome://plugin/ietab.html?url=") then
	--	alert(km.GetWindowVar(Window_LinkURL))
	--end
end

function OnCreateTab(hwnd)
	alert("test")
	km.NavigateTo("about:blank",OPEN_NORMAL)
end	
