// cometbird Statistics.js

var CometbirdStatistics = 
{
	isLoaded	: false,
	
	init : function()
	{
	try{		
		var appcontent = document.getElementById("appcontent");// browser
		if( appcontent )
			appcontent.addEventListener("DOMContentLoaded", CometbirdStatistics.initDoSearch, true);
		
		if ( CometbirdStatistics.browserHasLogin() )
		{
			return;
		}
		
		// send info to server
		CometbirdStatistics.reportToServer( "-=-report browser today-=-" );
		
	}catch(e){CometBirdFile.writeLog( "CometbirdStatistics init: " + e);}
	},
	
	browserHasLogin : function()
	{
		// check if need to report
		var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
        var prefs = prefService.getBranch("");
		if ( !prefs.getBoolPref("browser.enhancement.statistics.enable") )
			return true;
			
		// now time
		var now = new Date();
		var now_format_time = now.getFullYear() + "-";
		
		var month = now.getMonth() + 1;
		if ( month < 10 )
			now_format_time += "0";
		now_format_time = now_format_time + month.toString() + "-"; 
		
		var date = now.getDate();
		if ( date < 10 )
			now_format_time += "0";
		now_format_time = now_format_time + date.toString();
		
		// get time from perf.js
		var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
        var prefs = prefService.getBranch("");
		var browser_start_date = prefs.getCharPref("browser.enhancement.statistics.browser_start_date");
		
		if ( browser_start_date != now_format_time )// set it now
			prefs.setCharPref("browser.enhancement.statistics.browser_start_date", now_format_time);
		
		return ( now_format_time == browser_start_date );
	},
	
	browserHasSearch : function( searchValue )
	{	
		// current research value
		CometBirdFile.writeLog ("---rearch value: " + searchValue);
		
		// currentEngine
		var ss = Cc["@mozilla.org/browser/search-service;1"].getService(Ci.nsIBrowserSearchService);
		var currentEngineName = ss.currentEngine.name;

		// send info to server
		CometbirdStatistics.reportToServer( currentEngineName );
	},
	
	initDoSearch : function()
	{
	try{
		if ( CometbirdStatistics.isLoaded )
			return;
		CometbirdStatistics.isLoaded = true;
		
		// get rearch engine info
		var searchcontainer = document.getElementById("search-container");
		var searchbar = document.getElementById("searchbar");
		if ( !searchbar || !searchcontainer )
			return;
		
		// over write handleSearchCommand
		eval("searchbar.handleSearchCommand = " + searchbar.handleSearchCommand.toString().replace(
			"this.doSearch(textValue, where);",
			"$& CometbirdStatistics.browserHasSearch(textValue);"));
			
		//CometBirdFile.writeLog( "show new code: \n" + searchbar.handleSearchCommand.toSource() );
	}catch(e){CometBirdFile.writeLog("CometbirdStatistics dosearch: " + e);};
	},
	
	release : function()
	{
	},
	
	reportToServer : function( content )
	{
	try{	
		var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
        var prefs = prefService.getBranch("");
		
		// check if need to report
		if ( !prefs.getBoolPref("browser.enhancement.statistics.enable") )
			return;
		
		// get local language
		var locate_name = prefs.getCharPref("general.useragent.locale");

        var url = "";     
		if ( locate_name == "zh-CN" || locate_name == "zh-TW" )// chinese lang
			url = "http://www.cometbrowser.com/client/usage-stats/";
		else // other lang 
			url = "http://www.cometbird.com/client/usage-stats/";
		
		if ( content == "-=-report browser today-=-" )// report browser
		{
			url = url + "?l=" + encodeURIComponent( locate_name ) + "&daily=1";
		}
		else // engine
		{
			url = url + "?l=" + encodeURIComponent( locate_name ) + "&search=" + encodeURIComponent( content );
		}

		// open url in panel
		CometBirdFile.writeLog ( "url: " + url );
		CometBirdFile.writeLog ( "open url in panel" );
		var srcframe = document.getElementById("statistcs_url_frame");
		if ( srcframe != null )
			srcframe.setAttribute( "src", url );
		
    }catch(e){CometBirdFile.writeLog(e);}
	}
};

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