
/* 
* Tab Manager Object
*/
function gproxy_TabManager(){
	this.manage	= false;
	this.tabs 	= new Array();
	
	// Read Preferences (toggle manager on/off)
	try{
		var	oPrefs	= Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
			manage	= (oPrefs.prefHasUserValue("gproxy.tabs.manager") && oPrefs.getBoolPref("gproxy.tabs.manager") == true);
		
		// Add Pref Observer
		var oTabManagerPrefObserver = {
			observe : function(subject, topic, data){ if(data == true && manage == false) { manage = true; /*gproxy_setProxy(false)*/; } }
		};
		oPrefs	= Components.classes["@mozilla.org/preferences-service;1"].createInstance(Components.interfaces.nsIPrefBranchInternal);
		oPrefs.addObserver("gproxy.tabs.manager", oTabManagerPrefObserver, false);
	} catch(err){}
	
	
	this.getTabForBrowser	= gproxy_tab_getTabForBrowser;
	this.getTabForDocument	= gproxy_tab_getTabForDocument;
	this.tabLoading 		= gproxy_tab_tabLoading;
}

// Called on new Window/Tab 
function gproxy_tab_tabLoading(){
	
	try{
		// Verify all tabs are in list
		var aBrowsers 	= gBrowser.browsers;
		for(var i = 0; i < aBrowsers.length; i++){
			if(this.getTabForBrowser(aBrowsers[i]) == null){
				var index = this.tabs.length;
				this.tabs[index] = new gproxy_Tab(aBrowsers[i], null, index);
			}
		}
	} catch(err) {alert(err)}

}

// Return gproxy_Tab object for oBrowser
function gproxy_tab_getTabForBrowser(oBrowser){
	for(var t = 0; t < this.tabs.length; t++){
		if(this.tabs[t].browser == oBrowser)
			return this.tabs[t];
	}	
	return null;
}

// Return gproxy_Tab object for oDocument
function gproxy_tab_getTabForDocument(oDocument){
	for(var t = 0; t < this.tabs.length; t++){
		if(this.tabs[t].browser.contentDocument == oDocument)
			return this.tabs[t];
	}	
	return null;
}

/*
* Tab Object
*/
function gproxy_Tab(browser, proxy, index){
	this.browser 	= browser;
	this.proxy		= proxy;
	this.title		= "";
	this.index		= index;
	
	//alert(this.browser.contentDocument)
	
	if(this.browser != null){
		try{
			this.browser.addEventListener("focus", gproxy_tab_onfocus, true);
			this.browser.addEventListener("unload", gproxy_tab_onunload, true);
			this.browser.addEventListener("load", gproxy_tab_onloaded, true);
		} catch(err) { alert(err); }
	}
	
	this.updateTitle = function(){
		
		if(this.proxy != null && this.proxy != ""){
			
			// Get label for proxy uri
			var	oRes		= gproxy_ds_getResource(aProxies[i]);
			var sProxyName	= gproxy_ds_getValueFor(oRes, gproxy_ds_getResource(gSProxyRdfNodeName));
			
			// Set title
			if(sProxyName != null){
				this.browser.contentDocument.title = "["+ sProxyName +"] " +this.title;
			}
		}		
	}
}


/*
* Utilities
*/
	// Notify current tab of proxy change
	function gproxy_tab_notify(sUri){
		var oTab = gproxy_getTabManager().getTabForBrowser(gBrowser.selectedBrowser);
		
		oTab.proxyUri = sUri;
		oTab.updateTitle();
	}

/*
* Event Handlers
*/

	// On Tab Focus
	function gproxy_tab_onfocus(event){
		try{
			var oTab = gproxy_getTabManager().getTabForBrowser(gBrowser.selectedBrowser)
			//oTab.browser.contentDocument.title = oTab.index;
			
			//gproxy_setProxy();
		}catch(err) { }
	}
	
	// On Window Focus
	function gproxy_tab_windowFocus(event){
		try{
			if(event.target.nodeName = "tabbrowser"){
				
			}
		}catch(err) { }
	}
	
	// On tab unload
	function gproxy_tab_onunload(event){
		try{
			var oTab = gproxy_getTabManager().getTabForDocument(event.target);
			//oTab.browser.contentDocument.title = "Refreshing...";
		}catch(err) { }
	}
	
	// On tab load
	function gproxy_tab_onloaded(event){
		try{
			var oTab = gproxy_getTabManager().getTabForDocument(event.target);
			//oTab.browser.contentDocument.title = "Loaded";
		}catch(err) { }
	}