var CometBirdUndoClose =
{
    init : function()
    {     
        // register tab-close
        gBrowser.addEventListener( "TabClose", CometBirdUndoClose.changeCloseTabStatus, false );   
        // register sstabrestoring
        document.addEventListener( "SSTabRestoring",CometBirdUndoClose.changeCloseTabStatus, false );
		
		// save original BrowserToolboxCustomizeChange
		CometBirdUndoClose.originalToolboxCustomizeChange = BrowserToolboxCustomizeChange;
		// overwrite BrowserToolboxCustomizeChange of browser.js
		BrowserToolboxCustomizeChange = CometBirdUndoClose.toolboxCustomizeChange;
		
		// update undo-close status
		CometBirdUndoClose.changeCloseTabStatus();
    },
   
    undoCloseButton : function( event )
    {
        if ( event.target.id != "cometbird-undo-close-button" )
            return;
       
        // restore the last close tab       
        this.undoCloseTab( 0 );
    },

    undoCloseTab : function ( index )
    {
        // restore the index close tab
        var ss = Components.classes["@mozilla.org/browser/sessionstore;1"].getService(Components.interfaces.nsISessionStore);
        ss.undoCloseTab( window, index );
		
		// update it
		this.changeCloseTabStatus();
    },
	
	originalToolboxCustomizeChange : function()
	{
	},
	
	toolboxCustomizeChange : function()
	{
		// original function first
		CometBirdUndoClose.originalToolboxCustomizeChange();
		
		// additional function
		CometBirdUndoClose.changeCloseTabStatus();
	},
	
    changeCloseTabStatus : function()
    {
		// get undo_close_button object
		var undo_close_button = document.getElementById( "cometbird-undo-close-button" );
		if ( undo_close_button == null )
			return;
			
        var ss = Components.classes["@mozilla.org/browser/sessionstore;1"].getService(Components.interfaces.nsISessionStore);
        if ( ss.getClosedTabCount(window) == 0 )
        {
            undo_close_button.setAttribute( "disabled", true );
        }
        else
        {
            undo_close_button.setAttribute( "disabled", false );
        }
    },
   
    showUndoCloseTabList : function()
    {
        // show the list
        var undoPopup = document.getElementById("cometbird-undoClose-tabList");

        // remove existing menu items
        while ( undoPopup.hasChildNodes() )
            undoPopup.removeChild( undoPopup.firstChild );

        //get closed-tabs from nsSessionStore
        var ss = Components.classes["@mozilla.org/browser/sessionstore;1"].getService(Components.interfaces.nsISessionStore);
        //no restorable tabs, return
        if ( ss.getClosedTabCount(window) == 0 )
        {
			// update it
			this.changeCloseTabStatus();
			return;
		}

        //populate menu
        var undoItems = eval("(" + ss.getClosedTabData(window) + ")");
        for ( var i = 0; i < undoItems.length; i++ )
        {
            //draw each bookmrks item
            var item = document.createElement("menuitem");
            item.setAttribute( "label", undoItems[i].title );
            if ( undoItems[i].image )
                item.setAttribute( "image", undoItems[i].image );
               
            item.setAttribute("class", "menuitem-iconic bookmark-item");
            item.setAttribute( "oncommand", "CometBirdUndoClose.undoCloseTab( " + i + ");" );
           
            if ( i == 0 )// the first item
                item.setAttribute( "key", "key_undoCloseTab" );

            //add it
            undoPopup.appendChild( item );
        }
       
        //add open all in tabs
        var all_item = document.createElement("menuitem");
        var undoclose_strbundle = document.getElementById("undoclose-strings");
        all_item.setAttribute( "label", undoclose_strbundle.getString( "cometbird.undoclose.menuitem_label_openall" ) );
        all_item.setAttribute( "accesskey", undoclose_strbundle.getString( "cometbird.undoclose.menuitem_label_openall_accesskey") );
        all_item.addEventListener( "command",
            function()
            {
                for ( var i = 0; i < undoItems.length; i++ )
                    CometBirdUndoClose.undoCloseTab( 0 );
            },
        false);
       
        //add it
        undoPopup.appendChild( document.createElement("menuseparator") );
        undoPopup.appendChild( all_item );
    },
   
    release : function()
    {
        // un-register tab event
        gBrowser.removeEventListener( "TabClose", CometBirdUndoClose.changeCloseTabStatus, false );
        document.removeEventListener( "SSTabRestoring",CometBirdUndoClose.changeCloseTabStatus, false );
		
		// restore original
		BrowserToolboxCustomizeChange = CometBirdUndoClose.originalToolboxCustomizeChange;
    }
};

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