// cometbird.js

var CometBirdHandler = 
{
	_timer :	null,
	
	init : function()
	{
		// delete log
		CometBirdFile.deleteLog();
		
		// set home page when open first tab for firefox 3.5
		CometBirdUtility.setHomePage( window.arguments[0] );
		
		// update ui
		CometBirdHandler.updateTranslateUI();

		// register
		CometBirdObserver.register();
		
		// overwrite WindowIsClosing
		eval( 'WindowIsClosing=' + WindowIsClosing.toSource().replace(
			'for (var i = 0; reallyClose && i < numtabs; ++i)',
			<![CDATA[
				//check if only one tab
				if ( gBrowser.mTabContainer.childNodes.length == 1 )
				{
					gBrowser.removeCurrentTab();// close the tab first
				}
			$&]]>
			)
		);
	},
	
	release : function()
	{
		// remove register
		CometBirdObserver.unRegister();
	},
	
	updateTranslateUI : function()
	{
		var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
        var prefs = prefService.getBranch("");
        var is_enable_tranlate = prefs.getBoolPref("browser.enhancement.google.translate.enable");
       		
		var bubble = Components.classes["@mozilla.org/intl/stringbundle;1"].getService(Components.interfaces.nsIStringBundleService).createBundle("chrome://cometbird/locale/cometbird_translate.properties");
        
		// set translate state
        if ( is_enable_tranlate )
        {
			document.getElementById("cometbird-translate-statusbar-toggle").setAttribute("checked", "true");
			document.getElementById("translate_select_image").tooltipText = bubble.GetStringFromName("cometbird.translate.statusabar.open");
            document.getElementById("translate_select_image").setAttribute("src", "chrome://cometbird/skin/translator.png");
			
            // bigin to monitor whether translate
            CometBirdHandler._timer = window.setInterval( "cometbirdTranslate.monitorTranslate()", 1000 );
			
			var translate_button = document.getElementById("cometbird-translate-button");
			if ( translate_button == null )
				return;
				
            translate_button.setAttribute("checked", "true");
			document.getElementById("cometbird-translate-toggle").setAttribute("checked", "true");
			translate_button.setAttribute("tooltiptext", bubble.GetStringFromName("cometbird.translate.statusabar.open") );
			translate_button.setAttribute("image", "chrome://cometbird/skin/translator.png");
        }
        else
        {
			document.getElementById("cometbird-translate-statusbar-toggle").setAttribute("checked", "false");
			document.getElementById("translate_select_image").tooltipText = bubble.GetStringFromName("cometbird.translate.statusabar.closed");
			document.getElementById("translate_select_image").setAttribute("src", "chrome://cometbird/skin/translator_disabled.png");
			
            // cancel monitor
            if ( CometBirdHandler._timer != null )
            {
				window.clearInterval( CometBirdHandler._timer );
				CometBirdHandler._timer = null;
			}
			
			var translate_button = document.getElementById("cometbird-translate-button");
			if ( translate_button == null )
				return;
				
            translate_button.setAttribute("checked", "false");
			document.getElementById("cometbird-translate-toggle").setAttribute("checked", "false");
			translate_button.setAttribute("tooltiptext", bubble.GetStringFromName("cometbird.translate.statusabar.closed") );
			translate_button.setAttribute("image", "chrome://cometbird/skin/translator_disabled.png");
        }
	}
};

var CometBirdObserver = 
{
	observe: function( subject, topic, data )
	{
		if ( topic == CometBirdTranslateSettingDefine.changed_event )
		{
			CometBirdHandler.updateTranslateUI();
			return;
		}
		
		if ( topic == CometBirdTranslateSettingDefine.disabled_tabgroup_event )
		{
			if ( data == "enable" )
			{
				CometbirdTabGroup.updateTabsColor();
				return;
			}
			
			if ( data == "disabled" )
			{
				CometbirdTabGroup.clearTabsColor();
			}
		}
	},
	
	register : function()
	{
		var observer_service = Components.classes["@mozilla.org/observer-service;1"].getService( Components.interfaces.nsIObserverService );
		observer_service.addObserver( CometBirdObserver, CometBirdTranslateSettingDefine.changed_event, false );
		observer_service.addObserver( CometBirdObserver, CometBirdTranslateSettingDefine.disabled_tabgroup_event, false );
	},
	
	unRegister : function()
	{	
		var observer_service = Components.classes["@mozilla.org/observer-service;1"].getService( Components.interfaces.nsIObserverService );
		observer_service.removeObserver( CometBirdObserver, CometBirdTranslateSettingDefine.changed_event );
		observer_service.removeObserver( CometBirdObserver, CometBirdTranslateSettingDefine.disabled_tabgroup_event );
	}
};

window.addEventListener( "load", CometBirdHandler.init, false );
window.addEventListener( "unload", CometBirdHandler.release, false );