/////////////////////////////////////////////////////////////////////////////////////
// global
var
 g_cats
,g_ext=external
,g_setting=g_ext.setting
,g_app=g_ext.app
,g_initBookmark=false
,g_initHome=false
,g_initToolbar=false
,g_initAccel=false
,g_initSearch=false
,g_initUserAgent=false
,g_initMouseAction=false
,g_initScriptSetting=false
,g_loadUrlAction
,g_initTabSnapshot
,g_initUrlAction
,g_initStyleSheet
,g_urlAction
,g_initBHO
,g_bhoList
,g_initProxy=false
,g_defaultTabFont=true
,g_settings=[
	  // 0: category 1: setting name 2: init setting func
	  ["main,","startSetting",null]
	, ["main,tab,","appStyleSetting",null]
	, ["main,tab,","loadingStyleSetting",null]
	, ["main,style,","viewStyleSetting",null]
	, ["main,","instanceSetting",null]
	, ["main,","homeSetting",function(){initHomeSetting()}]
	, ["reg,","assocSetting",function(){initAssocSetting()}]
	, ["mouse,","dropActionSetting",null]
	, ["mouse,","mouseGestureSetting",null]
	, ["mouse,","mousePlusSetting",null]
	, ["main,","pageSetting",null]
	, ["main,","jumpListSetting",null]
	, ["main,","taskBarSetting",null]
	, ["main,","confirmSetting",null]
	, ["main,","exitSetting",null]
	, ["main,","memorySetting",null]
	, ["main,","crashSetting",null]
	, ["bookmark,sidebar,","bookmarkSetting",function(){initBookmark()}]
	, ["bookmark,sidebar,","addBookmarkSetting",null]
	, ["webkit,","webkitFontSetting",null]
	, ["webkit,","webkitSetting",null]
	, ["trident,","renderingSetting",null]
	, ["trident,","securitySetting",null]
	, ["trident,","securityControlSetting",null]
	, ["trident,","proxySetting",function(){initProxy()}]
	, ["trident,","userAgentSetting",function(){initUserAgent()}]
	, ["trident,","BHOSetting",function(){initBHO()}]
	, ["trident,","featureSetting",null]
	, ["script,","scriptSetting",function(){initScriptSetting()}]
	, ["trident,script,","pageScriptSetting",null]
	, ["trident,","tridentEtcSetting",null]
	, ["tab,","newUrlSetting",null]
	, ["tab,search,","searchInBrowserSetting",null]
	, ["tab,","tabStyleSetting",null]
	, ["tab,","tabFontSetting",null]
	, ["tab,","tabPosSetting",null]
	, ["tab,","newTabPositionSetting",null]
	, ["tab,","newTabActivateSetting",null]
	, ["tab,","openPopupSetting",null]
	, ["tab,","closeActiveTabSetting",null]
	, ["tab,","selectTabSetting",null]
	, ["tab,","lastTabCmdSetting",null]
	, ["tab,","tabSnapshotSetting",function(){initTabSnapshot()}]
	, ["tab,","tabOptionSetting",null]
	, ["urlaction,","urlActionSetting",function(){initUrlAction()}]
	, ["styleSheet,","styleSheetSetting",function(){initStyleSheet()}]
	, ["mouse,","actionXButtonSetting",function(){initMouseAction()}]
	, ["mouse,","actionWithButtonSetting",function(){initMouseAction()}]
	, ["tab,mouse,","actionTabItemSetting",function(){initMouseAction()}]
	, ["tab,mouse,","actionTabBkgdSetting",function(){initMouseAction()}]
	, ["tab,mouse,","actionTabItemKeySetting",function(){initMouseAction()}]
	, ["tab,mouse,","actionTabBkgdKeySetting",function(){initMouseAction()}]
	, ["tab,","tabWidthSetting",null]
	, ["tab,","tabClosedSetting",null]
	, ["tab,","maxTabSetting",null]
	, ["tab,","travelLogSetting",null]
	, ["main,toolbar,","frameAdditionalButtonSetting",null]
	, ["toolbar,","toolbarSetting",function(){initToolbar()}]
	, ["toolbar,","addToolbarSetting",null]
	, ["toolbar,","hilitePosSetting",null]
	, ["toolbar,","isearchPosSetting",null]
	, ["accel,","accelSetting",function(){initAccel()}]
	, ["search,","searchEngineSetting",function(){initSearch()}]
	, ["search,","hiliteSetting",null]
	, ["search,","maxSearchHistorySetting",null]
	, ["sidebar,","sidebarEdgeSizeSetting",null]
	, ["main,sidebar,","EBPlusSetting",null]
	, ["main,","testFeatureSetting",null]
	, ["script,","activeScriptSetting",null]
	, ["cache,","faviconCacheSetting",null]
	, ["cache,","thumbnailCacheSetting",null]
]
,g_view=""
,g_txtScript=g_app.getCmdText("script")
,g_txtSearch=g_app.getCmdText("search")
,C_SELECTED="cSelected"
,C_UNHOVER=["cUnhover","cUnhover2"]
,g_dropActionChanged
,g_mouseGestureChanged
,g_bookmarkChanged
,g_toolChanged
,g_toolbarChanged
,g_initAssoc=false
,g_assocChanged
,g_recent
;
/////////////////////////////////////////////////////////////////////////////////////
// proxy
function initProxy(){
	if(!g_initProxy){
		g_initProxy=true;
		loadScript("./option_proxy.js");
	}
}
/////////////////////////////////////////////////////////////////////////////////////
// tab snapshot
function initTabSnapshot(){
	if(!g_initTabSnapshot){
		g_initTabSnapshot=true;
		loadScript("./option_snapshot.js");
	}
}
/////////////////////////////////////////////////////////////////////////////////////
// url action
function loadUrlActionScript(){
	if(!g_loadUrlAction){
		g_loadUrlAction=true;
		loadScript("./option_urlAction.js");
	}
}
function initUrlAction(){
	loadUrlActionScript();
	if(!g_initUrlAction){
		g_initUrlAction=true;
		refreshUrlAction();
	}
}
function initStyleSheet(){
	loadUrlActionScript();
	if(!g_initStyleSheet){
		g_initStyleSheet=true;
		refreshStyleSheet();
	}
}
/////////////////////////////////////////////////////////////////////////////////////
// toolbar
function initToolbar(){
	if(! g_initToolbar){
		g_initToolbar=true;
		loadScript("./option_toolbar.js");
	}
}
/////////////////////////////////////////////////////////////////////////////////////
// drop action
function dropActionSetting(){
	if(g_ext.dropActionSetting()){
		g_dropActionChanged=true;
	}
}
/////////////////////////////////////////////////////////////////////////////////////
// mouse gesture
function mouseGestureSetting(idx){
	if(g_ext.mouseGestureSetting(idx)){
		g_mouseGestureChanged=true;
	}
}
/////////////////////////////////////////////////////////////////////////////////////
// bookmark
function initBookmark(){
	if(! g_initBookmark){
		g_initBookmark=true;
		loadScript("./option_bookmark.js");
	}
}
/////////////////////////////////////////////////////////////////////////////////////
// accel
function initAccel(){
	if(! g_initAccel){
		g_initAccel=true;
		loadScript("jquery.poshytip.min.js");
		loadCss("tip-yellowsimple.css");
		loadScript("./option_accel.js");
	}
}
/////////////////////////////////////////////////////////////////////////////////////
// search
function initSearch(){
	if(!g_initSearch){
		g_initSearch=true;
		loadScript("./option_search.js");
	}
}
/////////////////////////////////////////////////////////////////////////////////////
// script
function initScriptSetting(){
	if(!g_initScriptSetting){
		g_initScriptSetting=true;
		loadScript("./option_script.js");
	}
}
/////////////////////////////////////////////////////////////////////////////////////
// user agent
function initUserAgent(){
	if(! g_initUserAgent) {
		g_initUserAgent=true;
		loadScript("option_userAgent.js");
	}
}
/////////////////////////////////////////////////////////////////////////////////////
// BHO
function initBHO(){
	if(! g_initBHO){
		g_initBHO=true;
		loadScript("./option_bho.js");
	}
}
/////////////////////////////////////////////////////////////////////////////////////
// home page
function setHomeUrl(target){
	var	urls=g_app.homeList;
	if(urls.count){
		target.value=urls.url(0);
	}
}
/////////////////////////////////////////////////////////////////////////////////////
// mouse
function initMouseAction(){
	if(! g_initMouseAction){
		g_initMouseAction=true;
		loadScript("./option_mouse.js");
	}
}
$("form.cDummy .cSelectCmd:enabled").live("mousedown focus",function(ev){
	dispSelectCmd(ev.target);
	$(ev.target.form).removeClass("cDummy");
	return false;
});
/////////////////////////////////////////////////////////////////////////////////////
// tab font
function setTabFontDefault(){
	$$("currentTabFont").innerText="規定のフォント";
	g_defaultTabFont=true;
	g_ext.defaultTabFont();
}
function chooseTabFont(){
	var	currentTabFont=null;
	if(!g_defaultTabFont){
		currentTabFont=$$("currentTabFont").innerText;
	}
	var	tabFont=g_ext.chooseFont(currentTabFont);
	if(tabFont){
		g_defaultTabFont=false;
		$$("currentTabFont").innerText=tabFont;
	}
}
/////////////////////////////////////////////////////////////////////////////////////
// home
function initHomeSetting(){
	if(!g_initHome){
		g_initHome=true;
		loadScript("./option_home.js");
	}
}
/////////////////////////////////////////////////////////////////////////////////////
// regist app
function initAssocSetting(){
	if(!g_initAssoc){
		g_initAssoc=true;
		loadScript("./option_assoc.js");
	}
}
/////////////////////////////////////////////////////////////////////////////////////
// jumplist
function jumpListSetting(){
	g_ext.jumpListSetting();
	addRecent("jumpListSetting");
}
/////////////////////////////////////////////////////////////////////////////////////
// load option, recent
function getOptionSettingPath(){
	return getFSO().BuildPath(getSettingDirPath(),"Option.json");
}
function loadOptionSetting(){
	var	option=parseJsonFile(getOptionSettingPath());
	if(!option){
		option={};
	}
	return option;
}
function saveOptionSetting(){
	var	option={};
	option.width=g_ext.width;
	option.height=g_ext.height;
	if(g_recent){
		// save count: 10
		option.recent=g_recent.slice(0,9);
	}
	var	json=JSON.stringify(option,undefined,"");
	writeUnicodeTextFile(getOptionSettingPath(),json);
}
function showOption(option){
	var	width=option.width?option.width:750;
	var	height=option.height?option.height:500;
	g_ext.show(width,height);
}
function applyRecent(option){
	var	applied;
	if(option&&typeof option.recent==="object"){
		g_recent=option.recent;
		$.each(option.recent,function(){
			var	recentSetting=this.toString();
			$.each(g_settings,function(){
				if(this[1]==recentSetting){
					this[0]+="recent,";
					applied=true;
					return;
				}
			});
		});
	}
	return applied;
}
function clearRecent(){
	g_recent=undefined;
}
function _addRecentSp(){
	if(g_assocChanged)addRecent("assocSetting");
	if(g_dropActionChanged)addRecent("dropActionSetting");
	if(g_mouseGestureChanged)addRecent("mouseGestureSetting");
	if(g_bookmarkChanged)addRecent("bookmarkSetting");
	if(g_toolChanged)addRecent("toolSetting");
	if(g_toolbarChanged)addRecent("toolbarSetting");
}
function addRecent(settingName){
	if(!g_recent)g_recent=[];
	var	index=arrayIndexOf(g_recent,settingName);
	if(-1===index){
		g_recent.unshift(settingName);
	}
}
function _addRecent(form){
	var	parent=form.parentNode;
	if(parent&&parent.id){
		addRecent(parent.id);
	}
}
function _setCheckedIndex(name,docName){
	if(!docName)docName=name;
	var	v =getCheckedIdx(document[docName].v,g_setting[name]);
	if(v!=g_setting[name]){
		g_setting[name]=v;
		_addRecent(document[docName]);
	}
}
function _setCheckedValue(name,docName){
	if(!docName)docName=name;
	var	v =getCheckedIdx(document[docName].v,g_setting[name]);
	v=parseInt(document[docName][v].value);
	if(v!=g_setting[name]){
		g_setting[name]=parseInt(v);
		_addRecent(document[docName]);
	}
}
function _setChecked(name,docName){
	if(!docName)docName=name;
	var	v=document[docName].v.checked;
	if(v!=g_setting[name]){
		g_setting[name]=v;
		_addRecent(document[docName]);
	}
}
function _setInt(name,docName){
	if(!docName)docName=name;
	var	v=parseInt(document[docName].v.value);
	if(v!=g_setting[name]){
		g_setting[name]=v;
		_addRecent(document[docName]);
	}
}
function _setValue(name,docName){
	if(!docName)docName=name;
	var	v=document[docName].v.value;
	if(v!=g_setting[name]){
		g_setting[name]=v;
		_addRecent(document[docName]);
	}
}
function _setSelectedValue(name,docName){
	if(!docName)docName=name;
	var	v=getSelectedValue(document[docName].v.options);
	if(v!=g_setting[name]){
		g_setting[name]=v;
		_addRecent(document[docName]);
	}
}
function _setSelectedIntValue(name,docName){
	if(!docName)docName=name;
	var	v=parseInt(getSelectedValue(document[docName].v.options));
	if(v!=g_setting[name]){
		g_setting[name]=v;
		_addRecent(document[docName]);
	}
}
/////////////////////////////////////////////////////////////////////////////////////
// init, apply, view
function goAddSearchPage(){
	g_app.ExecScript("reserved\\goAddSearchPage.js");
	optionClose();
}
function doFind(ev){
	if(13===ev.keyCode){
		view("find");
	}
}
function view(category){
	var	isFind=("find"===category);
	if(!isFind&&g_view===category){
		return;
	}
	g_ext.hilite("");
	var	elemNotFound=$("#notFound")
		,elemNeedKw=$("#needKw");
	$$("view").scrollTop=0;
	elemNotFound.hide();
	elemNeedKw.hide();	
	var	g_elemCnt=g_cats.length;

	$.each(g_cats,function(){
		this.toggleClass(C_SELECTED,this.get(0).id===category);
		});

	var	g_settingsCnt=g_settings.length
		,kw=document.findKeyword.v.value;
	if(isFind&&kw.length){
		kw=kw.toUpperCase();
		var notFound=true;
		for(j=0;j<g_settingsCnt;++j){
			var	setting=$("#"+g_settings[j][1]);
			if(setting){
				var	show=false;
				// call init
				if(null!==g_settings[j][2]){
					g_settings[j][2]();
					g_settings[j][2]=null;
				}
				var	txt=setting.text().toUpperCase();
				if(txt.indexOf(kw)!=-1){
					show=true;
					notFound=false;
				}
				show ? setting.show() : setting.hide();
			}
		}
		if(notFound){
			elemNotFound.slideDown(300);
		} else {
			g_ext.hilite(kw);
		}
	}else{
		for(j=0;j<g_settingsCnt;++j){
			var	setting=$("#"+g_settings[j][1]);
			if(setting){
				if(isFind||(-1!=g_settings[j][0].indexOf(category))){
					// call init
					if(null!==g_settings[j][2]){
						g_settings[j][2]();
					}
					setting.show();
				}else{
					setting.hide();
				}
			}
		}
		if(isFind){
			elemNeedKw.slideDown(300);
		}
	}
	g_view=category;
}
function init(){
	// category
	g_cats=[
		  $($$("recent"))
		, $($$("main"))
		, $($$("reg"))
		, $($$("style"))
		, $($$("bookmark"))
		//, $($$("trident"))
		, $($$("webkit"))
		, $($$("tab"))
		, $($$("urlaction"))
		//, $($$("stylesheet"))
		, $($$("toolbar"))
		, $($$("mouse"))
		, $($$("accel"))
		, $($$("search"))
		, $($$("script"))
		, $($$("cache"))
		, $($$("sidebar"))
		, $($$("find"))
	];

	var option=loadOptionSetting();
	showOption(option);
	var	firstSelect=1;
	if(applyRecent(option)){
		g_cats[0].show();
		firstSelect=0;
	}
	$.each(g_cats,function(){this.mouseout(
		function(ev){
			var	target=$(ev.target);
			if((ev.target.className.indexOf(C_SELECTED)===-1)&&ev.target.tagName==="A"){
				target.addClass(C_UNHOVER[0]);
				setTimeout(function(){
					if(!target.hasClass(C_SELECTED)){
						target.addClass(C_UNHOVER[1]);
					}
					target.removeClass(C_UNHOVER[0]);
					setTimeout(function(){
						target.removeClass(C_UNHOVER[1]);
					},50);
				},30);
			}
		})
	});

	//////////////////////////////////////////////////////
	// reg
	if(g_setting.sysVer<6000){
		$$("idSelectBrowserUI").style.display="none";
		$$("idAssocNew").style.display="none";
		$$("idAssocOld").style.display="block";
		$$("idNewSystem").style.display="none";
	}

	//////////////////////////////////////////////////////
	// webkit
	loadWebKitSetting();

	//////////////////////////////////////////////////////

	// start
	setCheckedByValue(document.start, g_setting.start);
	// app style
	//document.appStyle[g_setting.style].checked=true;
	document.appStyle[3].checked=true;
	$$("idCurrentStyle").innerText=getAppStyleText(g_setting.currentStyle);
	// tab group
	document.tabGroup.v.checked=g_setting.tabGroup;
	// loading style
	document.loadingStyle[g_setting.loadingStyle].checked=true;
	$$("idCurrentLoadingStyle").innerText=getLoadingStyleText(g_setting.currentLoadingStyle);
	// tab group color
	document.tabGroupColor.v.checked=g_setting.tabGroupColor;
	// view style
	document.viewStyle[g_setting.viewStyle].checked=true;
	$$("currentViewStyle").innerText=getViewStyleText(g_setting.currentViewStyle);
	// single instance
	document.singleInstance.v.checked=g_setting.singleInstance;
	// single instance start enable
	document.singleInstanceStartEnable.v.checked=g_setting.singleInstanceStartEnable;
	// max closed
	document.maxClosed.v.value=g_setting.maxClosed;
	// max travellog
	document.maxTravelLog.v.value=g_setting.maxTravelLog;
	// max tab
	document.maxTab.v.value=g_setting.maxTab;
	// max tab load
	document.maxTabLoad.v.value=g_setting.maxTabLoad;
	// tab snapshot
	document.tabSnapshot.v.checked=g_setting.tabSnapshot;
	// tab snapshot interval
	document.tabSnapshotInterval.v.value=g_setting.tabSnapshotInterval;
	// tab width
	document.tabWidth.v.value=g_setting.tabWidth;
	// tab auto min width
	document.tabAutoMinWidth.v.value=g_setting.tabAutoMinWidth;
	// tab auto maxwidth
	document.tabAutoMaxWidth.v.value=g_setting.tabAutoMaxWidth;
	// tab font
	if(g_setting.tabFont){
		$$("currentTabFont").innerText=g_setting.tabFont;
		g_defaultTabFont=false;
	}
	// auto text link
	document.autoTextLink.v.checked=g_setting.autoTextLink;
	// auto text link dialog
	document.autoTextLinkDialog.v.checked=g_setting.autoTextLinkDialog;
	// auto image resize
	document.autoImageResize.v.checked=g_setting.autoImageResize;
	// security
	var	security = g_setting.security;
	document.securityImage.v.checked=(security&0x10);
	document.securityVideo.v.checked=(security&0x20);
	document.securitySound.v.checked=(security&0x40);
	document.securityScript.v.checked=(0===(security&0x80));
	document.securityJava.v.checked=(0===(security&0x100));
	document.securityRunActiveX.v.checked=(0===(security&0x200));
	document.securityDownloadActiveX.v.checked=(0===(security&0x400));
	document.metaRefresh.v.checked=g_setting.metaRefresh;
	// inherit security
	document.inheritSecurity.v.checked=g_setting.inheritSecurity;
	// feature
	selectOption(document.featureBrowserEmulation.v.options,g_setting.featureBrowserEmulation);
	document.featureGpuRendering.v.checked=g_setting.featureGpuRendering;
	document.featureObjectCaching.v.checked=g_setting.featureObjectCaching;
	document.featureZoneElevation.v.checked=g_setting.featureZoneElevation;
	document.featureMimeHandling.v.checked=g_setting.featureMimeHandling;
	document.featureMimeSniffing.v.checked=g_setting.featureMimeSniffing;
	document.featureWindowRestrictions.v.checked=g_setting.featureWindowRestrictions;
	document.featureWebocPopupmanagement.v.checked=g_setting.featureWebocPopupmanagement;
	document.featureBehaviors.v.checked=g_setting.featureBehaviors;
	document.featureDisableMkProtocol.v.checked=g_setting.featureDisableMkProtocol;
	document.featureLocalmachineLockdown.v.checked=g_setting.featureLocalmachineLockdown;
	document.featureSecurityband.v.checked=g_setting.featureSecurityband;
	document.featureRestrictActivexinstall.v.checked=g_setting.featureRestrictActivexinstall;
	document.featureValidateNavigateUrl.v.checked=g_setting.featureValidateNavigateUrl;
	document.featureRestrictFiledownload.v.checked=g_setting.featureRestrictFiledownload;
	document.featureProtocolLockdown.v.checked=g_setting.featureProtocolLockdown;
	document.featureHttpUsernamePasswordDisable.v.checked=g_setting.featureHttpUsernamePasswordDisable;
	document.featureSafeBindtoobject.v.checked=g_setting.featureSafeBindtoobject;
	document.featureUncSavedfilecheck.v.checked=g_setting.featureUncSavedfilecheck;
	document.featureGetUrlDomFilepathUnencoded.v.checked=g_setting.featureGetUrlDomFilepathUnencoded;
	document.featureTabbedBrowsing.v.checked=g_setting.featureTabbedBrowsing;
	document.featureSslux.v.checked=g_setting.featureSslux;
	document.featureDisableNavigationSounds.v.checked=g_setting.featureDisableNavigationSounds;
	document.featureDisableLegacyCompression.v.checked=g_setting.featureDisableLegacyCompression;
	document.featureXmlHttp.v.checked=g_setting.featureXmlHttp;
	document.featureDisableTelnetProtocol.v.checked=g_setting.featureDisableTelnetProtocol;
	document.featureFeeds.v.checked=g_setting.featureFeeds;
	document.featureBlockInputPrompts.v.checked=g_setting.featureBlockInputPrompts;

	// target name resolve
	document.targetNameResolve.v.checked=g_setting.targetNameResolve;
	// trident theme
	document.tridentTheme.v.checked=g_setting.tridentTheme;
	// auto complete
	document.autoComplete.v.checked=g_setting.autoComplete;
	// dpi aware
	document.dpiAware.v.checked=g_setting.dpiAware;
	// drop action
	document.dropAction.v.checked=g_setting.dropAction;
	// mouse gesture
	document.mouseGesture.v[g_setting.mouseGesture].checked=true;
	// mouse gesture limit
	document.mouseGestureLimit.v.value=g_setting.mouseGestureLimit;
	// wheel redirect
	document.wheelRedirect.v.checked=g_setting.wheelRedirect;
	// hwheel redirect
	document.hwheelRedirect.v.checked=g_setting.hwheelRedirect;
	// new url
	document.newUrl.v.value=g_setting.newUrl;
	// search new tab
	document.searchNewTab.v.checked=g_setting.searchNewTab;
	// search tab group
	document.searchTabGroup.v.checked=g_setting.searchTabGroup;
	// tab style
	document.tabStyle.v[g_setting.tabStyle].checked=true;
	// tab icon
	document.tabIcon.v.checked=g_setting.tabIcon;
	// new tab position
	document.newTabPosition.v[g_setting.newTabPosition].checked=true;
	// new tab position tab list
	selectOption(document.newTabPositionTabList.v.options,g_setting.newTabPositionTabList);
	// new tab position window
	selectOption(document.newTabPositionWindow.v.options,g_setting.newTabPositionWindow);
	// new tab activate
	document.newTabActivate.v.checked=g_setting.newTabActivate;
	// new tab activate from page
	selectOption(document.newTabActivateFromPage.v.options,g_setting.newTabActivateFromPage);
	// open popup
	document.openPopup.v.checked=g_setting.openPopup;
	// new process activate
	selectOption(document.newProcessActivate.v.options,g_setting.newProcessActivate);
	// close active tab
	document.closeActiveTab.v[g_setting.closeActiveTab].checked=true;
	// last tab cmd
	document.lastTabCmd.v[g_setting.lastTabCmd].checked=true;
	// wheel tab
	document.wheelTab.v.checked=g_setting.wheelTab;
	// wheel tab loop
	document.wheelTabLoop.v.checked=g_setting.wheelTabLoop;
	// wheel tab scroll
	document.wheelTabScroll.v.checked=g_setting.wheelTabScroll;
	// select tab MDI
	document.selectTabMDI.v.checked=g_setting.selectTabMDI;
	// tab close button
	document.tabCloseButton.v.checked=g_setting.tabCloseButton;
	// tab control bar
	document.tabControlBar.v.checked=g_setting.tabControlBar;
	// nav close lock
	document.navCloseLock.v.checked=g_setting.navCloseLock;
	// show tab state
	document.showTabState.v.checked=g_setting.showTabState;
	// active no auto refresh
	document.activeNoAutoRefresh.v.checked=g_setting.activeNoAutoRefresh;
	// taskbar thumbnail
	document.taskbarThumbnail.v.checked=g_setting.taskbarThumbnail;
	// taskbar progress
	document.taskbarProgress.v.checked=g_setting.taskbarProgress;
	// confirm exit
	document.confirmExit.v.checked=g_setting.confirmExit;
	// confirm close any tab
	document.confirmCloseAnyTab.v.checked=g_setting.confirmCloseAnyTab;
	// sync ime state
	document.syncImeState.v.checked=g_setting.syncImeState;
	// check downloading
	document.checkDownloading.v.checked=g_setting.checkDownloading;
	// task tray
	document.tasktray.v.checked=g_setting.tasktray;
	// minimize task tray
	document.minimizeTasktray.v.checked=g_setting.minimizeTasktray;
	// delete history on close
	document.deleteHistoryOnClose.v.checked=g_setting.deleteHistoryOnClose;
	// delete cache on close
	document.deleteCacheOnClose.v.checked=g_setting.deleteCacheOnClose;
	// delete cookie on close
	document.deleteCookieOnClose.v.checked=g_setting.deleteCookieOnClose;
	// delete search history on close
	document.deleteSearchHistoryOnClose.v.checked=g_setting.deleteSearchHistoryOnClose;
	// delete address history on close
	document.deleteAddressHistoryOnClose.v.checked=g_setting.deleteAddressHistoryOnClose;
	// delete lasttab on close
	document.deleteLastTabOnClose.v.checked=g_setting.deleteLastTabOnClose;
	// jump list
	document.jumpList.v.checked=g_setting.jumpList;
	toggleEnable(document.jumpList.v,'jumpListButton');
	// show active script error
	document.showActiveScriptError.v.checked=g_setting.showActiveScriptError;
	// show page script error
	document.showPageScriptError.v.checked=g_setting.showPageScriptError;
	// favicon
	document.favicon.v.checked=g_setting.favicon;
	// thumbnail
	document.thumbnail.v.checked=g_setting.thumbnail;
	// feed
	document.feed.v.checked=g_setting.feed;
	// auto hilite
	document.autoHilite.v.checked=g_setting.autoHilite;
	// inherit hilite
	document.inheritHilite.v.checked=g_setting.inheritHilite;
	// tab pos
	document.tabPos.v[g_setting.tabPos].checked=true;
	// hilite pos
	document.hilitePos.v[g_setting.hilitePos].checked=true;
	// frame additional button
	document.frameAdditionalButton.v.checked=g_setting.frameAdditionalButton;
	// isearch pos
	document.isearchPos.v[g_setting.isearchPos].checked=true;
	// max search histroy
	document.maxSearchHistory.v.value=g_setting.maxSearchHistory;
	// max inc history
	document.maxIncHistory.v.value=g_setting.maxIncHistory;
	// lock toolbar
	document.lockToolbar.v.checked=g_setting.lockToolbar;
	// favicon limit count
	document.faviconLimitCount.v.value=g_setting.faviconLimitCount;
	// favicon delete percent
	document.faviconDeletePercent.v.value=g_setting.faviconDeletePercent;
	// thumbnail limit count
	document.thumbnailLimitCount.v.value=g_setting.thumbnailLimitCount;
	// thumbnail delete percent
	document.thumbnailDeletePercent.v.value=g_setting.thumbnailDeletePercent;
	// DEP
	document.DEP.v.checked=g_setting.DEP;
	// crash file
	document.crashFile.v.checked=g_setting.crashFile;
	// delete old crash file
	document.deleteOldCrashFile.v.checked=g_setting.deleteOldCrashFile;
	// save closed
	document.saveClosed.v.checked=g_setting.saveClosed;
	// save scroll pos
	document.saveScrollPos.v.checked=g_setting.saveScrollPos;
	// sidebar edge size
	document.sidebarEdgeSize.v.value=g_setting.sidebarEdgeSize;
	// test feature
	document.testFeature.v.checked=g_setting.testFeature;
	// EBPlus tab mode
	document.EBPlusTabMode.v.checked=g_setting.EBPlusTabMode;
	// url action
	document.urlAction.v.checked=g_setting.urlAction;
	// single key shortcut
	document.singlekeyShortcut.v.checked=g_setting.singlekeyShortcut;
	
	// select main
	g_cats[firstSelect].click();
	
	// focus
	document.findKeyword.v.focus();
}
// from html dialog
function onCloseHtmlDialog(){
	_addRecentSp();
	saveOptionSetting();
	if(g_toolbarChanged){
		g_ext.settingSave=true;
	}
}
function optionClose(){
	g_ext.optionClose();
}
function optionApply(){
	if(!apply_check()){
		return;
	}
	
	var	reloads=[];
	clearRecent();
	
	if(g_initUrlAction||g_initStyleSheet){
		if(saveUrlAction()){
			g_ext.reloadUrlAction();
			if(g_initUrlAction){
				addRecent("urlActionSetting");
			}
			if(g_initStyleSheet){
				addRecent("styleSheetSetting");
			}
		}
	}
	if(g_initProxy){
		saveProxy();
		var	indexProxy=getCheckedIdx(document.proxy.v,0);
		var	proxy="";
		if(1===indexProxy){
			proxy=":direct:";
		}else if(2===indexProxy){
			proxy=getSelectedValue(document.proxyList.v.options);
			if(proxy===":ng:"){
				alert("プロキシ情報が選択してください");
				document.proxyList.v.focus();
				return;
			}
			if(!getFSO().FileExists(getProxySettingPathFromName(proxy))){
				alert("プロキシ情報のファイルが読み取れません。");
				document.proxyList.v.focus();
				return;
			}
		}
		if(g_setting.proxy!=proxy){
			g_setting.proxy=proxy;
			addRecent("proxySetting");
		}
	}

	//////////////////////////////////////////////////////
	// webkit
	saveWebKitSetting();

	// start
	_setCheckedValue("start");
	// app style
	_setCheckedIndex("style","appStyle");
	// tab group
	_setChecked("tabGroup");
	// loading style
	_setCheckedIndex("loadingStyle");
	// tab group color
	_setChecked("tabGroupColor");
	// view style
	_setCheckedIndex("viewStyle");
	// single instance
	_setChecked("singleInstance");
	// single instance start enable
	_setChecked("singleInstanceStartEnable");
	// home
	if(g_initHome){
		_setSelectedValue("home");
	}
	// max closed
	_setInt("maxClosed");
	// max travellog
	_setInt("maxTravelLog");
	// max tab
	_setInt("maxTab");
	// max tab load
	_setInt("maxTabLoad");
	if(g_setting["maxTabLoad"]===0){
		g_setting["maxTabLoad"]=1;
	}
	// tab snapshot
	_setChecked("tabSnapshot");
	// tab snapshot interval
	_setInt("tabSnapshotInterval");
	if(! g_setting.tabSnapshotInterval){
		g_setting.tabSnapshotInterval=30;
	}
	if(g_initTabSnapshot){
		if(snapshot_save()){
			addRecent("tabSnapshotSetting");
		}
	}
	// tab width
	var	tabWidth=parseInt(document.tabWidth.v.value);
	if(tabWidth<48){
		document.tabWidth.v.value="48";
	}
	_setInt("tabWidth");
	// tab auto min width
	var	tabAutoMinWidth=parseInt(document.tabAutoMinWidth.v.value);
	if(tabAutoMinWidth<48){
		document.tabAutoMinWidth.v.value="48";
	}
	_setInt("tabAutoMinWidth");
	// tab auto max width
	var	tabAutoMaxWidth=parseInt(document.tabAutoMaxWidth.v.value);
	if(tabAutoMaxWidth<48){
		document.tabAutoMaxWidth.v.value="48";
	}
	_setInt("tabAutoMaxWidth");
	// tab font
	var	tabFont="";
	if(!g_defaultTabFont){
		tabFont=$$("currentTabFont").innerText;
	}
	if(g_setting.tabFont!=tabFont){
		g_setting.tabFont=tabFont;
		addRecent("tabFontSetting");
	}
	// auto text link
	_setChecked("autoTextLink");
	// auto text link dialog
	_setChecked("autoTextLinkDialog");
	// auto image resize
	_setChecked("autoImageResize");
	// security
	var	security = 0;
	security|=(document.securityImage.v.checked?0x10:0);
	security|=(document.securityVideo.v.checked?0x20:0);
	security|=(document.securitySound.v.checked?0x40:0);
	security|=(document.securityScript.v.checked?0:0x80);
	security|=(document.securityJava.v.checked?0:0x100);
	security|=(document.securityRunActiveX.v.checked?0:0x200);
	security|=(document.securityDownloadActiveX.v.checked?0:0x400);
	if(g_setting.security!=security){
		g_setting.security=security;
		addRecent("securitySetting");
	}
	_setChecked("metaRefresh");
	// inherit security
	_setChecked("inheritSecurity");
	// BHO
	if(g_initBHO){
		if(g_bhoList&&g_bhoList.length){
			var bhoList=[],bhoChanged;
			for(i=0;i<g_bhoList.length;++i){
				var	bho=g_bhoList[i];
				if(bho.enable!=bho.enableOld){
					bhoChanged=true;
				}
				if(bho.enable){
					bhoList.push(bho.clsid);
				}
			}
			if(bhoChanged){
				addRecent("BHOSetting");
				g_ext.bhoList=bhoList;
			}
		}
	}
	// user agent
	if(g_initUserAgent) {
		var	idx=document.userAgentList.v.selectedIndex;
		var	userAgent="";
		if(0!=idx){
			userAgent=document.userAgent.v.value;
		}
		if(g_setting.userAgent!=userAgent){
			g_setting.userAgent=userAgent;
			addRecent("userAgentSetting");
		}
	}
	// feature
	_setSelectedValue("featureBrowserEmulation");
	_setChecked("featureGpuRendering");
	_setChecked("featureObjectCaching");
	_setChecked("featureZoneElevation");
	_setChecked("featureMimeHandling");
	_setChecked("featureMimeSniffing");
	_setChecked("featureWindowRestrictions");
	_setChecked("featureWebocPopupmanagement");
	_setChecked("featureBehaviors");
	_setChecked("featureDisableMkProtocol");
	_setChecked("featureLocalmachineLockdown");
	_setChecked("featureSecurityband");
	_setChecked("featureRestrictActivexinstall");
	_setChecked("featureValidateNavigateUrl");
	_setChecked("featureRestrictFiledownload");
	_setChecked("featureProtocolLockdown");
	_setChecked("featureHttpUsernamePasswordDisable");
	_setChecked("featureSafeBindtoobject");
	_setChecked("featureUncSavedfilecheck");
	_setChecked("featureGetUrlDomFilepathUnencoded");
	_setChecked("featureTabbedBrowsing");
	_setChecked("featureSslux");
	_setChecked("featureDisableNavigationSounds");
	_setChecked("featureDisableLegacyCompression");
	_setChecked("featureXmlHttp");
	_setChecked("featureDisableTelnetProtocol");
	_setChecked("featureFeeds");
	_setChecked("featureBlockInputPrompts");
	// target name resolve
	_setChecked("targetNameResolve");
	// trident theme
	_setChecked("tridentTheme");
	// auto complete
	_setChecked("autoComplete");
	// dpi aware
	_setChecked("dpiAware");
	// drop action
	_setChecked("dropAction");
	// mouse gesture
	_setCheckedIndex("mouseGesture");
	// mouse gesture limit
	var	mouseGestureLimit=parseInt(document.mouseGestureLimit.v.value);
	if(mouseGestureLimit < 1) {
		document.mouseGestureLimit.v.value="4";
	}
	_setInt("mouseGestureLimit");
	// wheel redirect
	_setChecked("wheelRedirect");
	// hwheel redirect
	_setChecked("hwheelRedirect");
	// new url
	_setValue("newUrl");
	// search new tab
	_setChecked("searchNewTab");
	// search tab group
	_setChecked("searchTabGroup");
	// tab style
	_setCheckedIndex("tabStyle");
	// tab icon
	_setChecked("tabIcon");
	// new tab position
	_setCheckedIndex("newTabPosition");
	// new tab position tab list
	_setSelectedIntValue("newTabPositionTabList");
	// new tab position window
	_setSelectedIntValue("newTabPositionWindow");
	// new tab activate
	_setChecked("newTabActivate");
	// new tab activate from page
	_setSelectedIntValue("newTabActivateFromPage");
	// open popup
	_setChecked("openPopup");
	// new process activate
	_setSelectedIntValue("newProcessActivate");
	// close active tab
	_setCheckedIndex("closeActiveTab");
	// last tab cmd
	_setCheckedIndex("lastTabCmd");
	// wheel tab
	_setChecked("wheelTab");
	// wheel tab loop
	_setChecked("wheelTabLoop");
	// wheel tab scroll
	_setChecked("wheelTabScroll");
	// select tab MDI
	_setChecked("selectTabMDI");
	// tab close button
	_setChecked("tabCloseButton");
	// tab control bar
	_setChecked("tabControlBar");
	// nav close lock
	_setChecked("navCloseLock");
	// show tab state
	_setChecked("showTabState");
	// active no auto refresh
	_setChecked("activeNoAutoRefresh");
	// show active script error
	_setChecked("showActiveScriptError");
	// show page script error
	_setChecked("showPageScriptError");
	// taskbar thumbnail
	_setChecked("taskbarThumbnail");
	// taskbar progress
	_setChecked("taskbarProgress");
	// confirm exit
	_setChecked("confirmExit");
	// confirm close any tab
	_setChecked("confirmCloseAnyTab");
	// sync ime state
	_setChecked("syncImeState");
	// check downloading
	_setChecked("checkDownloading");
	// task tray
	_setChecked("tasktray");
	// minimize task tray
	_setChecked("minimizeTasktray");
	// delete history on close
	_setChecked("deleteHistoryOnClose");
	// delete cache on close
	_setChecked("deleteCacheOnClose");
	// delete cookie on close
	_setChecked("deleteCookieOnClose");
	// delete search history on close
	_setChecked("deleteSearchHistoryOnClose");
	// delete address history on close
	_setChecked("deleteAddressHistoryOnClose");
	// delete lasttab on close
	_setChecked("deleteLastTabOnClose");
	// jump list
	_setChecked("jumpList");
	// favicon
	_setChecked("favicon");
	// thumbnail
	_setChecked("thumbnail");
	// feed
	_setChecked("feed");
	// auto hilite
	_setChecked("autoHilite");
	// inherit hilite
	_setChecked("inheritHilite");
	// tab pos
	_setCheckedIndex("tabPos");
	// hilite pos
	_setCheckedIndex("hilitePos");
	// frame additional button
	_setChecked("frameAdditionalButton");
	// isearch pos
	_setCheckedIndex("isearchPos");
	// max search history
	_setInt("maxSearchHistory");
	// max inc history
	_setInt("maxIncHistory");
	// mouse
	if(g_initMouseAction){
		var	changed=mouse_saveAction();
		for(idx in changed.settings){
			_addRecent($$(changed.settings[idx]));
		}
		if(changed.reloads.length){
			reloads=reloads.concat(changed.reloads);
		}
	}
	// accel
	if(g_initAccel){
		// del add accel item
		var	accel=$($$("idAppAccel")).data("accel");
		var	list=accel.list;
		list.del(list.count-1);
		// apply del
		var	count=list.count;
	
		while(count){
			--count;
			if(list.markDel(count)){
				list.del(count);
			}
		}
		// check default accel list
		list.sort();
		g_setting.defaultAccelList=false;
		if(compareAccelList(list,g_setting.defaultAccelList)){
			g_setting.defaultAccelList=true;
		}
		// check custom accel list
		else if(! compareAccelList(list,g_setting.accelList)){
			g_setting.accelList=list;
			addRecent("accelSetting");
		}
	}
	// favicon limit count
	_setInt("faviconLimitCount");
	// favicon delete percent
	{
		var	_frm=document.faviconDeletePercent;
		_frm.v.value=common_fixRange(_frm.v.value,0,100);
	}
	_setInt("faviconDeletePercent");
	// thumbnail limit count
	_setInt("thumbnailLimitCount");
	// thumbnail delete percent
	{
		var	_frm=document.thumbnailDeletePercent;
		_frm.v.value=common_fixRange(_frm.v.value,0,100);
	}
	_setInt("thumbnailDeletePercent");
	// DEP
	_setChecked("DEP");
	// check file
	_setChecked("crashFile");
	// delete old crash file
	_setChecked("deleteOldCrashFile");
	// save closed
	_setChecked("saveClosed");
	// save scroll pos
	_setChecked("saveScrollPos");
	// sidebar edge size
	{
		var	_frm=document.sidebarEdgeSize;
		var	_v=common_fixRange(_frm.v.value,0,20);
		if(_v!=undefined){
			_frm.v.value=_v;
			_setInt("sidebarEdgeSize");
		}
	}
	// test feature
	_setChecked("testFeature");
	// EBPlus tab mode
	_setChecked("EBPlusTabMode");
	// url action
	_setChecked("urlAction");
	// single key shortcut
	_setChecked("singlekeyShortcut");

	// apply
	var	reloadSetting=reloads.join(',');
	if(1===reloads.length){
		reloadSetting+=',';
	}
	g_ext.optionApply(reloadSetting);
	g_ext.settingSave=true;
	optionClose();
}
function apply_check(){
	var	checks=
	[
			document.maxClosed
		,	document.maxTab
		,	document.maxTabLoad
		,	document.maxTravelLog
		,	document.tabWidth
		,	document.mouseGestureLimit
		,	document.maxSearchHistory
		,	document.maxIncHistory
		,	document.faviconLimitCount
		,	document.faviconDeletePercent
		,	document.thumbnailLimitCount
		,	document.thumbnailDeletePercent
	];
	for(i=0;i<checks.length;++i){
		if(0===checks[i].v.value.length){
			alert("不正な入力です。["+checks[i].name+"]");
			checks[i].v.focus();
			return false;
		}
	}
	return true;
}
