/* define global const variables */
if( typeof( CometBirdTranslateSettingDefine ) == "undefined" )
{
	try
	{
		eval('																							\r\n\
			const CometBirdTranslateSettingDefine =														\r\n\
			{																							\r\n\
				changed_event				:	"cometbird.translate.enable_translate_changed",			\r\n\
				disabled_tabgroup_event		:	"cometbird.tabgroup.disabled",							\r\n\
			};																							\r\n\
		');
	} 
	catch( e ) 
	{
		alert ( e ); 
	}
}

// Cometbird translate setting 
var CometBirdTranslateSetting = 
{
	doLoad : function()
	{
	try{	
		// set the window type name
		document.documentElement.setAttribute("windowtype", "CometBirdTranslateSettingDialog");
		
		// get lang-pair
		var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
        var prefs = prefService.getBranch("");
        var tranlate_from_lang = prefs.getCharPref("browser.enhancement.google.translate.from_lang");
		var tranlate_to_lang = prefs.getCharPref("browser.enhancement.google.translate.to_lang");

		// init
		var from_menu_list = document.getElementById("translate-from");
		for ( var i=0; i<from_menu_list.itemCount; i++ )
		{
			if ( tranlate_from_lang == from_menu_list.getItemAtIndex(i).value )
			{
				document.getElementById("translate-from").selectedItem = from_menu_list.getItemAtIndex(i);
				break;
			}
		}
		
		var to_menu_list = document.getElementById("translate-to");
		for ( var i=0; i<to_menu_list.itemCount; i++ )
		{
			if ( tranlate_to_lang == to_menu_list.getItemAtIndex(i).value )
			{
				document.getElementById("translate-to").selectedItem = to_menu_list.getItemAtIndex(i);
				break;
			}
		}
		
		// init translate enable
		if ( prefs.getBoolPref("browser.enhancement.google.translate.enable") )
		{
			document.getElementById("cometbird-translate-getword").setAttribute("checked", "true");
		}
		else
		{
			document.getElementById("cometbird-translate-getword").removeAttribute("checked");
		}
		
		// init translate selected funciton
		if ( prefs.getBoolPref("browser.enhancement.google.translate_selected.enable") )
		{
			document.getElementById("cometbird-translate-selected").setAttribute("checked", "true");
		}
		else
		{
			document.getElementById("cometbird-translate-selected").removeAttribute("checked");
		}
		
		// init enable drag link
		if ( prefs.getBoolPref("browser.enhancement.dragdrop_link.enable") )
		{
			document.getElementById("cometbird-drag-drop").setAttribute("checked", "true");
		}
		else
		{
			document.getElementById("cometbird-drag-drop").removeAttribute("checked");
		}
		
		// init enable drag text
		if ( prefs.getBoolPref("browser.enhancement.dragdrop_text.enable") )
		{
			document.getElementById("cometbird-drag-drop-text").setAttribute("checked", "true");
		}
		else
		{
			document.getElementById("cometbird-drag-drop-text").removeAttribute("checked");
		}
		
		// init tab group
		if ( prefs.getBoolPref("browser.enhancement.tabgroup.enable") )
		{
			document.getElementById("cometbird-tab-group").setAttribute("disabled", "false");
			document.getElementById("cometbird-tab-group").setAttribute("checked", "true");
		}
		else
		{
			document.getElementById("cometbird-tab-group").setAttribute("disabled", "false");
			document.getElementById("cometbird-tab-group").removeAttribute("checked");
		}
		if ( !prefs.getBoolPref("browser.tabs.insertRelatedAfterCurrent") )// false
		{
			document.getElementById("cometbird-tab-group").setAttribute("disabled", "true");
		}
		
		// init statistics
		var bubble = Components.classes["@mozilla.org/intl/stringbundle;1"].getService(Components.interfaces.nsIStringBundleService).createBundle("chrome://cometbird/locale/cometbird_statistics.properties");
		var text = bubble.GetStringFromName("cometbird.enhancement.statistics.text");
		var text_node = document.createTextNode( text );
		document.getElementById("cometbird-statistics-description").appendChild( text_node );
		
		if ( prefs.getBoolPref("browser.enhancement.statistics.enable") )
		{
			document.getElementById("cometbird-statistics-checkbox").setAttribute("checked", "true");
		}
		else
		{
			document.getElementById("cometbird-statistics-checkbox").removeAttribute("checked");
		}
		
		// show translator panel when open setting 
		if ( window.arguments[0] )
		{
			var translate_pane = document.getElementById("cometbird_setting_translate_panel");
			document.getElementById("cometbird_translate_setting").showPane( translate_pane );
		}
	}catch(e){CometBirdUtility.showErrorMsg(e);}
	},
	
	doOk : function()
	{
	try{
		// get lang-pair
		var from_menu_item = document.getElementById("translate-from").selectedItem;
		var from_lang = "en";
		if ( from_menu_item != null )
			from_lang = from_menu_item.value;
			
		var to_menu_item = document.getElementById("translate-to").selectedItem;
		var to_lang = "zh-CN";
		if ( to_menu_item != null )
			to_lang = to_menu_item.value;
		
		// save in perf.js
		var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
        var prefs = prefService.getBranch("");
        prefs.setCharPref("browser.enhancement.google.translate.from_lang", from_lang );
		prefs.setCharPref("browser.enhancement.google.translate.to_lang", to_lang);
		
		// set translate enable
		var checkbox = document.getElementById("cometbird-translate-getword");
		if ( checkbox.hasAttribute("checked") )
		{
			prefs.setBoolPref("browser.enhancement.google.translate.enable", true);
		}
		else
		{
			prefs.setBoolPref("browser.enhancement.google.translate.enable", false);
		}
		
		// notify to update ui	
		var bserverService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
		bserverService.notifyObservers( null, CometBirdTranslateSettingDefine.changed_event, null );
		
		// set translate selected
		checkbox = document.getElementById("cometbird-translate-selected");
		if ( checkbox.hasAttribute("checked") )
		{
			prefs.setBoolPref("browser.enhancement.google.translate_selected.enable", true);
		}
		else
		{
			prefs.setBoolPref("browser.enhancement.google.translate_selected.enable", false);
		}
		
		// set drag and drop link
		checkbox = document.getElementById("cometbird-drag-drop");
		if ( checkbox.hasAttribute("checked") )
		{
			prefs.setBoolPref("browser.enhancement.dragdrop_link.enable", true);
		}
		else
		{
			prefs.setBoolPref("browser.enhancement.dragdrop_link.enable", false);
		}
		
		// set drag and drop text
		checkbox = document.getElementById("cometbird-drag-drop-text");
		if ( checkbox.hasAttribute("checked") )
		{
			prefs.setBoolPref("browser.enhancement.dragdrop_text.enable", true);
		}
		else
		{
			prefs.setBoolPref("browser.enhancement.dragdrop_text.enable", false);
		}
		
		// set tab group
		checkbox = document.getElementById("cometbird-tab-group");
		if ( checkbox.hasAttribute("checked") )
		{
			prefs.setBoolPref("browser.enhancement.tabgroup.enable", true);
			// notify to update color	
			bserverService.notifyObservers( null, CometBirdTranslateSettingDefine.disabled_tabgroup_event, "enable" );
		}
		else
		{
			prefs.setBoolPref("browser.enhancement.tabgroup.enable", false);
			// notify to update color	
			bserverService.notifyObservers( null, CometBirdTranslateSettingDefine.disabled_tabgroup_event, "disabled" );
		}
		
		// set statistics
		checkbox = document.getElementById("cometbird-statistics-checkbox");
		if ( checkbox.hasAttribute("checked") )
		{
			prefs.setBoolPref("browser.enhancement.statistics.enable", true);
		}
		else
		{
			prefs.setBoolPref("browser.enhancement.statistics.enable", false);
		}
		
		return true;
	}catch(e){CometBirdUtility.showErrorMsg(e);}
	},
	
	doUnload : function()
	{
	},
	
	openHomePage : function()
	{
		var bubble = Components.classes["@mozilla.org/intl/stringbundle;1"].getService(Components.interfaces.nsIStringBundleService).createBundle("chrome://cometbird/locale/cometbird_translate.properties");
		var url = bubble.GetStringFromName("cometbird.translate.settingdialog.homepage.url");
		CometBirdUtility.openURL( url );
	},
	
	openHelpUrl : function()
	{
		var bubble = Components.classes["@mozilla.org/intl/stringbundle;1"].getService(Components.interfaces.nsIStringBundleService).createBundle("chrome://cometbird/locale/cometbird_translate.properties");
		var url = bubble.GetStringFromName("cometbird.translate.settingdialog.help.url");
		CometBirdUtility.openURL( url );
	}
};