require "path"
require "common_dialog"

function gestures(hwnd)
	local number,string=choose("Choose a Mouse Gestures","Mouse Gestures","up","down","left","right","upleft","upright","downleft","downright")
	if (number==1) then
		pref = "kmeleon.plugins.gestures.up";
	elseif (number == 2) then
		pref = "kmeleon.plugins.gestures.down";
	elseif (number == 3) then
		pref = "kmeleon.plugins.gestures.left";
	elseif (number == 4) then
		pref = "kmeleon.plugins.gestures.right";
	elseif (number == 5) then
		pref = "kmeleon.plugins.gestures.upleft";
	elseif (number == 6) then
		pref = "kmeleon.plugins.gestures.upright";
	elseif (number == 7) then
		pref = "kmeleon.plugins.gestures.downleft";
	elseif (number == 8) then
		pref = "kmeleon.plugins.gestures.downright";
	end
	if (number~=nil) then 
		showchoices(hwnd,pref)
	end		
end

function showchoices(hwnd,pref)
	local index,choice=choose("Commands","Commands","back","forward","stop","refresh","Open a new tab","Close tab")
	if (index == 1) then
		command = "ID_NAV_BACK";
	elseif (index == 2) then
		command = "ID_NAV_FORWARD";
	elseif (index == 3) then
		command = "ID_NAV_STOP";
	elseif (index == 4) then
		command = "ID_NAV_REFRESH";
	elseif (index == 5) then
		command = "ID_NEW_TAB";
	elseif (index == 6) then
		command = "ID_CLOSE_TAB";
	end
	if (index~=nil) then
		setpref(TYPE_STRING,pref,command);
	end
end

function prefdialog(hwnd)
	local num,str = choose("Preference","","Mouse Gestrures","Bookmarks","Hotkey","Search Bar","URL Bar","Favorites","Advanced","MRU","Download","Font","Path")
	if (num==0) then
	elseif (num==1) then
		gestures(hwnd)
		prefdialog(hwnd)
	elseif (num==2) then
		bookmark(hwnd)
		prefdialog(hwnd)
	elseif (num==3) then
		hotkey(hwnd)
		prefdialog(hwnd)
	elseif (num==4) then
		searchbar(hwnd)
		prefdialog(hwnd)
	elseif (num==5) then
		urlbar(hwnd)
		prefdialog(hwnd)
	elseif (num==6) then
		favorites(hwnd)
		prefdialog(hwnd)
	elseif (num==7) then
		advanced(hwnd)
		prefdialog(hwnd)
	elseif (num==8) then
		mru(hwnd)
		prefdialog(hwnd)
	elseif (num==9) then
		download(hwnd)
		prefdialog(hwnd)
	elseif (num==10) then
		font(hwnd)
		prefdialog(hwnd)
	elseif (num==11) then
		path(hwnd)
		prefdialog(hwnd)
	end
end

function bookmark(hwnd)
	local number,string = choose("Bookmarks","","Open in current tab","Open in new tab","Open in background tab","config")
	local pref="kmeleon.plugins.bookmarks.openurl"
	if (number==1) then
		setpref(TYPE_STRING,pref,"ID_OPEN_LINK")
	elseif (number==2) then
		setpref(TYPE_STRING,pref,"ID_OPEN_LINK_IN_NEW_TAB")
	elseif (number==3) then
		setpref(TYPE_STRING,pref,"ID_OPEN_LINK_IN_BACKGROUNDTAB")
	elseif (number==4) then
		plugin("bookmarks","config")
	end
end

function hotkey(hwnd)
	local number,string = choose("Hotkey","","CTRL+1","CTRL+2","CTRL+3","CTRL+4","CTRL+5","CTRL+6","CTRL+7","CTRL+8","CTRL+9")
	if (number~=nil) then
	pref="kmeleon.plugins.macros.hotlink"..number..".url"
	original_pref=getpref(TYPE_STRING,pref)
	final_pref=prompt("Input address for this hotkey","Hotkey",original_pref)
	setpref(TYPE_STRING,pref,final_pref)
	end
end

function searchbar(hwnd)
	local number,string=choose("Searchbar","","Open in current tab","Open in new tab","Open in background tab")
	local pref="kmeleon.plugins.searchbar.openurl"
	if (number==1) then
		setpref(TYPE_STRING,pref,"ID_OPEN_LINK")
	elseif (number==2) then
		setpref(TYPE_STRING,pref,"ID_OPEN_LINK_IN_NEW_WINDOW")
	elseif (number==3) then
		setpref(TYPE_STRING,pref,"ID_OPEN_LINK_IN_BACKGROUND")
	end
end

function urlbar(hwnd)
	local number,string=choose("URL Bar","","Open in current tab","Open in new tab")
	local pref="kmeleon.general.openurl"
	if (number==1) then
		setpref(TYPE_STRING,pref,"ID_OPEN_LINK")
	elseif (number==2) then
		setpref(TYPE_STRING,pref,"ID_OPEN_LINK_IN_NEW_WINDOW")
	end
end	

function quickbookmark(hwnd)
	local qbookmark=getpref(TYPE_STRING,"kmeleon.plugins.bookmarks.bookmarkFile");
	NavigateTo(qbookmark,OPEN_NORMAL);
end

function weather(hwnd)
	local acode=getpref(TYPE_STRING,"kmeleon.weather.area.code");
	local site = "http://www.weather.com/weather/local/";
	opennew(site .. acode);
end

function favorites(hwnd)
	local number,string = choose("Favorites","","Open in current tab","Open in new tab","Open in background tab","config","Toolbar folder")
	local pref="kmeleon.plugins.favorites.openurl"
	if (number==1) then
		setpref(TYPE_STRING,pref,"ID_OPEN_LINK")
	elseif (number==2) then
		setpref(TYPE_STRING,pref,"ID_OPEN_LINK_IN_NEW_TAB")
	elseif (number==3) then
		setpref(TYPE_STRING,pref,"ID_OPEN_LINK_IN_BACKGROUNDTAB")
	elseif (number==4) then
		plugin("favorites","config")
	elseif (number==5) then
		favtoolbar(hwnd)
	end
end

function favtoolbar(hwnd)
	local tf="kmeleon.plugins.favorites.toolbarFolder";
	local folder=getpref(TYPE_STRING,tf); 
--	if (folder=="") then 
--		folder="Links";
--	else  
		folder=prompt("Enter the name of the folder to be displayed on your favorites toolbar.","Favorites Toolbar Folder",folder);
		if (folder=="") then 
			folder=""
		else 
			setpref(TYPE_STRING,tf,folder);
			alert("Favorites Toolbar Folder will be  ' " ..folder.." '  when K-Meleon is restarted","Favorites Toolbar Folder");
		end
	end
-- end

function setareacode(hwnd)
	local areacode=prompt("Input Area Code:", "Area Code");
	setpref(TYPE_STRING,"kmeleon.weather.area.code", areacode);
end

function advanced(hwnd)
	local number,string = choose("Advanced","","Area Code")
	if (number==1) then
		setareacode(hwnd)
	end
end

function mru(hwnd)
	local number,string = choose("MRU","","Save all","Only domain","Only input","Don't save","Set drop down menu lines")
	if (number==1) then
		setpref(TYPE_INT,"kmeleon.MRU.behavior",0);
	elseif (number==2) then
		setpref(TYPE_INT,"kmeleon.MRU.behavior",1);
	elseif (number==3) then
		setpref(TYPE_INT,"kmeleon.MRU.behavior",2);
	elseif (number==4) then
		setpref(TYPE_INT,"kmeleon.MRU.behavior",3);
	elseif (number==5) then
		local dropsave=getpref(TYPE_INT,"kmeleon.urlbar.dropdown_lines");
		local dropdowns=prompt("Enter the number of lines for url bar drop down menu","# of lines",dropsave);
		setpref(TYPE_INT,"kmeleon.urlbar.dropdown_lines",dropdowns);
	end
end

function download(hwnd)
	local number=choose("Download","","Set default download folder...","Set save as folder...","Set save image as folder...","Set save link as folder...")
	if (number==1) then
		local default=getpref(TYPE_STRING,"kmeleon.general.saveDir")
		default=prompt("Enter Directory Path. (Include backslash at the end of folder name.)","Default",default)
		setpref(TYPE_STRING,"kmeleon.general.saveDir",default)
	elseif (number==2) then
		local default=getpref(TYPE_STRING,"kmeleon.general.savePageDir")
		default=prompt("Enter Directory Path. (Include backslash at the end of folder name.)","Save as",default)
		setpref(TYPE_STRING,"kmeleon.general.savePageDir",default)
	elseif (number==3) then
		local default=getpref(TYPE_STRING,"kmeleon.general.saveImageDir")
		default=prompt("Enter Directory Path. (Include backslash at the end of folder name.)","Save image as",default)
		setpref(TYPE_STRING,"kmeleon.general.saveImageDir",default)
	elseif (number==4) then
		local default=getpref(TYPE_STRING,"kmeleon.general.saveLinkDir")
		default=prompt("Enter Directory Path. (Include backslash at the end of folder name.)","Save link as",default)
		setpref(TYPE_STRING,"kmeleon.general.saveLinkDir",default)
	end
end

function font(hwnd)
	local number=choose("Font","","minimum font size")
	if (number==1) then
		local fontsize=getpref(TYPE_INT,"font.minimum-size.x-western");
		fontsize=prompt("Enter the minimum size of font","minimum font size",fontsize);
		setpref(TYPE_INT,"font.minimum-size.x-western",fontsize);
		setpref(TYPE_INT,"font.minimum-size.x-unicode", fontsize);
		setpref(TYPE_INT,"font.minimum-size.zh-CN",fontsize);
		setpref(TYPE_INT,"font.minimum-size.zh-TW",fontsize);
	end
end

function path(hwnd)
	local number=choose("Path","","Download manager path","CyberArticle path");
	if (number==1) then
		downloadpath()
	elseif (number==2) then
		cyberarticlepath()
	end
end
