var pref_observe =
{
	QueryInterface: function(iid)
	{
		if (!iid.equals(Components.interfaces.nsISupports) &&	!iid.equals(Components.interfaces.nsIObserver))
			throw Components.results.NS_ERROR_NO_INTERFACE;

		return this;
	}
};

var cometbird_tabs_click_count 	= 0;
var cometbird_page_first_loaded = false;

function cometbird_OnLoad()
{
	const Cc = Components.classes;
	const Ci = Components.interfaces;
	
	netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");	

	try 
	{
		var pref_svc = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService);		
		var branch = pref_svc.getBranch("");
		branch.QueryInterface(Ci.nsIPrefBranch2).addObserver("", pref_observe, false);
	}
	catch (ex) 
	{
		// do nothing
	}
	
	cometbird_page_first_loaded = true;
	var appcontent = document.getElementById("appcontent"); // browser
    if( appcontent )
		appcontent.addEventListener("DOMContentLoaded", cometbird_PageLoaded, true);

	for( var i = 0; i < gBrowser.mTabContainer.childNodes.length; ++i )
	{
		gBrowser.mTabContainer.childNodes[i].setAttribute( "ondblclick", "cometbird_dblclick()" );
		gBrowser.mTabContainer.childNodes[i].setAttribute( "cometbird_click_count", ++cometbird_tabs_click_count );
	}
}

function cometbird_PageLoaded( event )
{
	if ( cometbird_page_first_loaded && event.originalTarget instanceof HTMLDocument ) 
	{
        if ( gBrowser.mTabContainer.childNodes.length > 0 && gBrowser.mTabContainer.childNodes[0] == gBrowser.mCurrentTab )
		{
			try{
				gBrowser.mCurrentTab.setAttribute( "cometbird_click_count", ++cometbird_tabs_click_count );
				CometBirdFile.writeLog( "cometbird_PageLoaded");
			}catch(e){}
		}
		cometbird_page_first_loaded = false;
    }
}

//	closes browser if you press ctrl-W with only one tab open. Even if you don't autohide the tab bar.
// @line /browser/base/content/browser.js
function BrowserCloseTabOrWindow()
{
	
	//@line 1631 "/e/fx19rel/WINNT_5.2_Depend/mozilla/browser/base/content/browser.js"
	
	if (gBrowser.tabContainer.childNodes.length > 1) 
	{
		gBrowser.removeCurrentTab();
		return;
	}
	
//	//@line 1638 "/e/fx19rel/WINNT_5.2_Depend/mozilla/browser/base/content/browser.js"
//	if (gBrowser.localName == "tabbrowser" && window.toolbar.visible &&
//	!gPrefService.getBoolPref("browser.tabs.autoHide")) {
//		// Replace the remaining tab with a blank one and focus the address bar
//		gBrowser.removeCurrentTab();
//		if (gURLBar)
//		setTimeout(function() { gURLBar.focus(); }, 0);
//		return;
//	}
	//@line 1647 "/e/fx19rel/WINNT_5.2_Depend/mozilla/browser/base/content/browser.js"

	gBrowser.removeCurrentTab();
	closeWindow(true);	
}

function cometbird_OnUnload()
{
	const Cc = Components.classes;
	const Ci = Components.interfaces;
	
	try 
	{
		var pref_svc = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService);		
		var branch = pref_svc.getBranch("");
		branch.QueryInterface(Ci.nsIPrefBranch2).removeObserver("", pref_observe, false);
	}
	catch (ex) 
	{
		// do nothing
	}
}

window.addEventListener("load", cometbird_OnLoad, false);
window.addEventListener("unload", cometbird_OnUnload, false);
window.addEventListener("TabOpen", cometbird_TabOpen, false);
window.addEventListener("TabSelect", cometbird_TabSelect, false);
window.addEventListener("TabClose", cometbird_TabClose, false);


function cometbird_TabOpen(event)
{
	event.originalTarget.setAttribute( "ondblclick", "cometbird_dblclick()" );
	event.originalTarget.setAttribute( "onCloseTempTab", "false" );
	
	if ( !gBrowser.selectedTab )	
	{
		event.originalTarget.setAttribute( "cometbird_click_count", ++cometbird_tabs_click_count );
		return;
	}
	
	var selected_click	= parseInt( gBrowser.selectedTab.getAttribute( "cometbird_click_count" ) );
	for( var i = 0; i < gBrowser.mTabContainer.childNodes.length; ++i )
	{
		if( gBrowser.mTabContainer.childNodes[i] == event.originalTarget )
			continue;
				
		var cometbird_click_count = parseInt( gBrowser.mTabContainer.childNodes[i].getAttribute( "cometbird_click_count" ) );
		if( cometbird_click_count > selected_click )
			gBrowser.mTabContainer.childNodes[i].setAttribute( "cometbird_click_count", cometbird_click_count + 1 );
	}

	event.originalTarget.setAttribute( "cometbird_click_count", selected_click + 1 );
	++cometbird_tabs_click_count;
}

function cometbird_dblclick()
{
	// check is enable
	var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
    var prefs = prefService.getBranch("");
	
	if ( prefs.getBoolPref("browser.enhancement.dblclick_close_tab.enable") )
		gBrowser.removeTab( gBrowser.mCurrentTab );
}

function cometbird_TabClose(event)
{
	var min_click_count = 0;
	var next_tab = null;
	
	if( gBrowser.selectedTab != event.originalTarget )
	{
		return;
	}
		
	for( var i = 0; i < gBrowser.mTabContainer.childNodes.length; ++i )
	{
		if( gBrowser.mTabContainer.childNodes[i] == event.originalTarget )
			continue;

		var cometbird_click_count = parseInt( gBrowser.mTabContainer.childNodes[i].getAttribute( "cometbird_click_count" ) );
		if( cometbird_click_count > min_click_count )
		{
			next_tab = gBrowser.mTabContainer.childNodes[i];
			min_click_count = cometbird_click_count;
		}
	}

	// when close temp tab, don't selected tab
	if ( event.originalTarget.getAttribute( "onCloseTempTab") != "true" )
		gBrowser.mTabContainer.selectedItem = next_tab;
}

function cometbird_TabSelect()
{
	gBrowser.mCurrentTab.setAttribute( "cometbird_click_count", ++cometbird_tabs_click_count );
}