Components.utils.import("resource://cpmanager/cpmanager_mod.js");

//var CPMANAGER_ADDON_LIST_NEW_URL = "http://www.g-fox.cn/live.php";
//var CPMANAGER_ADDON_LIST_NEW_URL_FIRSTTIME = "http://www.g-fox.cn/activate.php";
//var CPMANAGER_ADDON_LIST_NEW_URL = "chrome://cpmanager/content/addon_list_new.xml";
//var CPMANAGER_ADDON_LIST_NEW_URL_FIRSTTIME = "chrome://cpmanager/content/addon_list_new.xml";
//var CPMANAGER_ADDON_LIST_FILE_PATH_DEFAULT = "chrome://cpmanager/content/addon_list.xml";
//var CPMANAGER_ADDON_LIST_FILE_NAME="chinaedition_addon_list.xml";
var CPMANAGER_ZIHU_VERSION_URL="";
var CPMANAGER_GET_SN_URL="http://www.g-fox.cn/gensn.php";
var cpmanager_snXmlHttp = null;
var cpmanager_xmlHttp = null;
var cpmanager_addonList = null;
//var cpmanager_addonListNew = null;
var cpmanager_ZiHuVersionNew = null;
var cpmanager_installNum = 0;
var cpmanager_installedNum = 0;
var cpmanager_update_delay = 0;
var cpmanager_init_delay_initial = 5000;
var cpmanager_init_delay = 15*60*1000;
var cpmanager_relive_delay = 24*60*60*1000;
//var cpmanager_partner_activate_interval = 7*24*60*60*1000
var cpmanager_partner_activate_interval = 0;
var gPref = Components.classes["@mozilla.org/preferences-service;1"]
               .getService(Components.interfaces.nsIPrefService)
               .QueryInterface(Components.interfaces.nsIPrefBranch2);

function cpmanager_setPrefValue(name,value){
//		var partnerID = prefs.getCharPref("mozilla.partner.id");
	try {
		var prefs = Application.extensions.get("cpmanager@mozillaonline.com").prefs;
		prefs.setValue(name,value);
	} catch (e){
		Components.utils.reportError(e);
	}
}

function cpmanager_getPrefValue(name,def_val){
	try {
		cpmanager_LOG("cpmanager: cpmanager_getPrefValue");
		var prefs = Application.extensions.get("cpmanager@mozillaonline.com").prefs;
		cpmanager_LOG("cpmanager: cpmanager_getPrefValue: " + prefs);
		return prefs.getValue(name,def_val);
	} catch (e) {
  		Components.utils.reportError(e);
  	}
}

function cpmanager_getFilePath(fileName){
	try {
		netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
	} catch (e) {
		cpmanager_LOG("China edition Manager: Permission save to file was denied.");
		return;
	}

	// get the path to the user's home (profile) directory
	try { 
		var file = Components.classes["@mozilla.org/file/directory_service;1"]
                     .getService(Components.interfaces.nsIProperties)
                     .get("ProfD", Components.interfaces.nsIFile);
		file.append("extensions");
		file.append(fileName);
		return file.path;
	} catch (e) {
		// alert("error");
		return;
	}

	// determine the file-separator
	//if (path.search(/\\/) != -1) {
		//window
	//	path = path + "\\extensions\\cpmanager@mozillaonline.com\\content\\";
	//} else {
		//linux
	//	path = path + "/extensions/cpmanager@mozillaonline.com/content/";
	//}
	//file_name = path+file_name;
	// alert("get_file_path: file_name:" + file_name);
	return file_name;
}


function cpmanager_getFileContent(filePath) {

	// alert("get_file_content");  
	var content = "";
	try {
		netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
	} catch (e) {
	// alert("Permission to read file was denied.");
		return;
	}
	var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
	file.initWithPath( filePath );
	if ( file.exists() == false ) {
		// alert("File does not exist");
		return content;
	}
	var is = Components.classes["@mozilla.org/network/file-input-stream;1"]
		.createInstance( Components.interfaces.nsIFileInputStream );
	is.init( file,0x01, 00004, null);
	var sis = Components.classes["@mozilla.org/scriptableinputstream;1"]
		.createInstance( Components.interfaces.nsIScriptableInputStream );
	sis.init( is );
	var output = sis.read( sis.available() );
	content = output;
//	this.LOG("get_file_content: output:" + output );
	return content;
}

function cpmanager_getXMLFromLocalFile(filePath) {
	cpmanager_LOG ("cpmanager: cpmanager_getXMLFromLocalFile " + filePath);
	//check whether is a local file
	if ( !(/file:\/\//.test(filePath)) && !(/chrome:\/\//.test(filePath))) {
		return null;
	}
	try {

		var req = new XMLHttpRequest();
		req.open("GET", filePath, false); 
		req.send(null);
		// print the name of the root element or error message
		var dom = req.responseXML;
		// alert(req.responseText);
		// alert(dom);
		// alert(dom.documentElement.nodeName);
		if ( dom.documentElement.nodeName == "parsererror" ) return null;
		return dom;
	} catch (e){
		cpmanager_LOG("cpmanager: cpmanager_getXMLFromLocalFile: file does not exist.");
		return null;
	}
}


//copyed from MDC
function cpmanager_openAndReuseOneTabPerURL(url) {
  var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
                     .getService(Components.interfaces.nsIWindowMediator);
  var browserEnumerator = wm.getEnumerator("navigator:browser");

  // Check each browser instance for our URL
  var found = false;
  while (!found && browserEnumerator.hasMoreElements()) {
    var browserWin = browserEnumerator.getNext();
    var tabbrowser = browserWin.getBrowser();

    // Check each tab of this browser instance
    var numTabs = tabbrowser.browsers.length;
    for(var index=0; index<numTabs; index++) {
      var currentBrowser = tabbrowser.getBrowserAtIndex(index);
      if (url == currentBrowser.currentURI.spec) {

        // The URL is already opened. Select this tab.
        tabbrowser.selectedTab = tabbrowser.mTabs[index];

        // Focus *this* browser-window
        browserWin.focus();

        found = true;
        break;
      }
    }
  }

  // Our URL isn't open. Open it now.
  if (!found) {
	if (gBrowser){
		gBrowser.selectedTab = gBrowser.addTab(url);
	} else {
		window.open(url,"_blank",null);
	}
  }
}


function cpmanager_browse(url){
	cpmanager_LOG("cpmanager: browse product change page");
	if (gBrowser){
		gBrowser.selectedTab = gBrowser.addTab(url,null);
	} else {
		window.open(url,"_blank",null);
	}
}

function cpmanager_compareVersions(a,b){
	try {
		var x = Components.classes["@mozilla.org/xpcom/version-comparator;1"]
	                   .getService(Components.interfaces.nsIVersionComparator)
	                   .compare(a,b);
		return x;
	} catch (e) {
		cpmanager_LOG("cpmanager: compareVersion: the version you gave is not version format:" + e.toString());
		return 0;
	}
}

/*
	return whether this is the first update of the day as a param.
*/
function cpmanager_paramFUOD(){
  	try {
		var prefName = "update_date";
//		var partnerID = prefs.getCharPref("mozilla.partner.id");
		var lastdate = cpmanager_getPrefValue(prefName,"");
//			this.cpmanager_LOG(prefName + " = " + initialized);
		var date = new Date();
		var strDate = "" + date.getYear() + "/" + date.getMonth() + "/" + date.getDate();
  		if (lastdate != strDate) {
  			cpmanager_setPrefValue(prefName, strDate);
//first update of the day
			return "&fuod=true";
  		}
		return "";
  	} catch (e) {
  		Components.utils.reportError(e);
		return "";
  	}
}

function cpmanager_checkZiHuVersionUpdates(){
	cpmanager_LOG("cpmanager: cpmanager_checkAddonUpdates");
//	var addonsInstall = new Object();
//	cpmanager_installNum = 0;
//	var addonsDisable = new Array();
//	var addonsUpdate = new Array();
	
//	if (cpmanager_addonList == null) cpmanager_addonList = cpmanager_getAddonList();
//	if (cpmanager_addonListNew == null || cpmanager_addonList == null) return;
//	var newVer = cpmanager_addonListNew.getElementById("china_edition_addon_list").getAttribute("version");
//	var ver = cpmanager_addonList.getElementById("china_edition_addon_list").getAttribute("version");
	// alert("cpmanager_checkAddonUpdates3");
 // assuming we're running under Firefox  
   var appInfo = Components.classes["@mozilla.org/xre/app-info;1"]  
					.getService(Components.interfaces.nsIXULAppInfo);  
	var newVer = cpmanager_ZiHuVersionNew..getElementById("Zihu_list").getAttribute("version");
	var updateLink = cpmanager_ZiHuVersionNew..getElementById("Zihu_list").getAttribute("updateLink");
	if (cpmanager_compareVersions(newVer,appInfo.version) <= 0) {
		cpmanager_LOG ("cpmanager: you have installed the latest version of china edition available.");
		return;
	}
	cpmanager_LOG ("cpmanager: china edition update detected.");

//	var addons = cpmanager_addonList.getElementsByTagName("addon");
//	var addonsNew = cpmanager_addonListNew.getElementsByTagName("addon");
	// alert("number of addons in the new list: "+ addonsNew.length);
//check for addons in the new list, whether they exist on the old list or already installed by user
/*	for (var i = 0 ; i < addonsNew.length ; i++){
		// alert("i="+i);
		// alert("id="+addonsNew.item(i).getAttribute("id"));
		var addonNow = cpmanager_addonList.getElementById(addonsNew[i].getAttribute("id"));
		// alert("if there is no such addon, should be null: " + addonNow);
		if (addonNow == null){
			// alert("url:" + addonsNew.item(i).getAttribute("url"));
//check whether it's already on the machine if it'is not on the list
			if (Application.extensions.has(addonsNew[i].getAttribute("id"))){
				// alert("addon exists");
				if (cpmanager_compareVersions(Application.extensions.get(addonsNew[i].getAttribute("id")).version, addonsNew[i].getAttribute("version")) < 0) {
//					addonsUpdate.push(addonsNew[i].getAttribute("id"));
					cpmanager_LOG("addon updates detected");
					addonsInstall[addonsNew[i].getAttribute("name")] = addonsNew[i].getAttribute("url");
					cpmanager_installNum++;

				}
				else {
					// alert ("Do not update addon");
				}
			} else {
				// alert("addon does not exist");
				addonsInstall[addonsNew[i].getAttribute("name")] = addonsNew[i].getAttribute("url");
				cpmanager_installNum++;
				//installAddon(addonsNew.item(i).getAttribute("url"));
			}
		}
	}*/
	//check for removed and updated addons
/*	for (var i = 0 ; i < addons.length ; i++){
		var addonNew = cpmanager_addonListNew.getElementById(addons.item(i).getAttribute("id"));
		var addonNow = addons[i];
		if (addonNew != null){
			if (addonNow.getAttribute("uninstalled") == "true"){
				addonNew.setAttribute("uninstalled","true");
			}
			if (addonNow.getAttribute("disabled") == "true"){
				addonNew.setAttribute("disabled","true");
			}
			if (cpmanager_compareVersions(addonNow.getAttribute("version"),addonNew.getAttribute("version")) < 0 && addonNow.getAttribute("disabled") != "true" && addonNow.getAttribute("uninstalled") != "true"){
				cpmanager_LOG("addon updates detected");
				addonsInstall[addonNew.getAttribute("name")] = addonNew.getAttribute("url");
				cpmanager_installNum++;
//				addonsUpdate.push(addonNow.getAttribute("id"));
			}
		} else {
			if (!(addonNow.getAttribute("uninstalled") == "true")){
				if (cpmanager_isAddonEnabled(addonNow.getAttribute("id"))){
					addonsDisable.push(addonNow.getAttribute("id"));
				}
			}
		}
	}*/
	if (cpmanager_compareVersions(newVer,appInfo.version) > 0){
		try {
	//prompt for update

			var checkResult = {};
			var strbundle = document.getElementById("cpmanagerStrings");
			//alert(strbundle.getString);
			//var title = strbundle.getString("title");
			//alert(title);
			window.setTimeout(function (){
					try {
						var buttons = [
							{
							    label: strbundle.getString("notification1.yes"),
							    accessKey: "U",
							    popup: null,
//inner function cpmanager_startUpgrade for access data in this function
							    callback: function cpmanager_startUpgrade() {
									try {
										window.open('updateLink');
									} catch (e) {
										cpmanager_LOG("cpmanager: cpmanager_startUpgrade: exception: "+e);
									}
									gBrowser.getNotificationBox().removeCurrentNotification();
								},
							},
							{
							    label: strbundle.getString("notification1.nountilnexttime"),
							    accessKey: "N",
							    popup: null,
//inner function cpmanager_startUpgrade for access data in this function
							    callback: function cpmanager_nountilnexttime() {
									//cpmanager_saveAddonList(cpmanager_addonListNew,cpmanager_getFilePath(CPMANAGER_ADDON_LIST_FILE_NAME));
									gBrowser.getNotificationBox().removeCurrentNotification();
								},
							},
							{
							    label: strbundle.getString("notification1.no"),
							    accessKey: "L",
							    popup: null,
							    callback: function (){
									gPref.setBoolPref('browser.download.ZiHuUpdate', false);
									gBrowser.getNotificationBox().removeCurrentNotification();
								},
							},
						];
						var notificationBox = gBrowser.getNotificationBox();
						var priority = notificationBox.PRIORITY_INFO_MEDIUM;
						var newBar = notificationBox.appendNotification(strbundle.getString("notification1.message"), "china-edition-upgrade",
			                                             "chrome://cpmanager/content/logo32x32_cn.png",
			                                             priority, buttons);
						newBar.persistence+=3;
					}
					catch(e) {cpmanager_LOG("cpmanager: cpmanager_checkAddonUpdates: exception: "+e);}
				},cpmanager_update_delay);
/* 			var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
                    .getService(Components.interfaces.nsIPromptService);

			if(promptService.confirmCheck(window,strbundle.getString("prompt1.title"),strbundle.getString("prompt1.content"),strbundle.getString("prompt1.checkbox"),checkResult)){
				if (cpmanager_installNum != 0){
					cpmanager_installAddons(addonsInstall);
				}
				// if (addonsUpdate.length != 0){
					// cpmanager_updateAddons(addonsUpdate);
					// BrowserOpenAddonsMgr();
				// }
				if (addonsDisable.length != 0){
					cpmanager_disableAddons(addonsDisable);
					BrowserOpenAddonsMgr();

				}
//				cpmanager_LOG("cpmanager: saving the new addon list.");
//				cpmanager_setPrefValue("installing",true);
//				cpmanager_saveAddonList(cpmanager_addonListNew,cpmanager_getFilePath(CPMANAGER_ADDON_LIST_FILE_NAME));
			}
			if (checkResult.value){
				//save the new addon list
				cpmanager_LOG("cpmanager: saving the new addon list.");
				//cpmanager_setPrefValue("installing",false);
				cpmanager_saveAddonList(cpmanager_addonListNew,cpmanager_getFilePath(CPMANAGER_ADDON_LIST_FILE_NAME));
			}
 */		}
		catch(e) {cpmanager_LOG("cpmanager: cpmanager_checkAddonUpdates: exception: "+e);}
	} else {
// China Edition Upgrade Successful
		cpmanager_LOG("cpmanager: cpmanager_checkAddonUpdates: do not need to update");
//		cpmanager_saveAddonList(cpmanager_addonListNew,cpmanager_getFilePath(CPMANAGER_ADDON_LIST_FILE_NAME));
		//prompt for update complete.
/*		if (!cp_mod.firstTime){
			var strbundle = document.getElementById("cpmanagerStrings");*/
/* 		var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
	                .getService(Components.interfaces.nsIPromptService);
			promptService.alert(window,strbundle.getString("prompt2.title"),strbundle.getString("prompt2.content"));	
	 */
/*			var changesUrl = cpmanager_addonListNew.documentElement.getAttribute("changes_url");
			var buttons = null
			if (changesUrl){
				buttons = [
					{
					    label: strbundle.getString("notification2.button"),
					    accessKey: "L",
					    popup: null,
//inner function cpmanager_startUpgrade for access data in this function
					    callback: function() {
							gBrowser.getNotificationBox().removeCurrentNotification();
							cpmanager_openAndReuseOneTabPerURL(changesUrl);
						},
					}];
			}else {
				cpmanager_LOG("cpmanager: no changes_url.");
			}
			
//Notification of upgrade complete
			var notificationBox = gBrowser.getNotificationBox();
			var priority = notificationBox.PRIORITY_WARNING_MEDIUM;
			var newBar = notificationBox.appendNotification(strbundle.getString("notification2.message"), "china-edition-upgraded",
		                                             "chrome://cpmanager/content/logo32x32_cn.png",
		                                             priority, buttons);
			newBar.persistence+=3;*/
			//Update the distribution file to new file!
/* 		if (Application.prefs.getValue("distribution.version","2008.11b") == "2008.11b"){
				cpmanager_copyDistribution();
			}
	 */		//Set distribution version, deprecated.
/*			if (cpmanager_compareVersions(newVer,Application.prefs.getValue("distribution.version","2008.11")) > 0){
				Application.prefs.setValue("distribution.version",newVer);
			}
		}*/
	}
}


function cpmanager_getZiHuStateChange(){
	try{
		cpmanager_LOG("cpmanager: cpmanager_getZiHuStateChange: readyState: " + cpmanager_xmlHttp.readyState);
		// if cpmanager_xmlHttp shows "loaded"
		if (cpmanager_xmlHttp.readyState==4)
		{
			cpmanager_LOG("cpmanager: cpmanager_getZiHuStateChange: status: " + cpmanager_xmlHttp.status);
			// alert("readyState2: " + cpmanager_xmlHttp.readyState);
		  // if "OK"  , 0 is ok when is a local file
			if (cpmanager_xmlHttp.status==200 || cpmanager_xmlHttp.status==0){
			// set history OK
			//aler("Update completed");
				cpmanager_ZiHuVersionNew = cpmanager_xmlHttp.responseXML;
				// alert(cpmanager_xmlHttp.responseText);
				// alert(cpmanager_addonListNew);
//				cpmanager_checkAddonUpdates();
			cpmanager_checkZihuVersionUpdates();
			// var file_path = get_file_path( kTOP_EXT_LIST_FILE_NAME ) ;
			// write_ext_list_update_to_file(file_path,gExtlist_xmlhttp_top.responseText);
			
			// var ext_arr = fill_extension_list_array(kTOP_EXT_LIST_FILE_NAME);
			// gExtList_top_extension_arr = ext_arr.extension_array;
			// gExtList_top_catigories_arr = ext_arr.catigories_arr;
			// if(gExtList_top_ext_for_install_arr != null) gExtList_top_ext_for_install_arr.length = 0;
			// ExtList_Top_Available_Extension_GetCategory();
			} else{
				// alert(cpmanager_xmlHttp.status);
				// alert("China edition Manager: Error take Extensions update. Plese try later.");
			}
		}
	}
	catch (e){}
}


function cpmanager_startUpdate(){
//	alert(cpmanager_getUid());
//	alert(cpmanager_getActCode("123456"));
//	var updateUrl = CPMANAGER_ADDON_LIST_NEW_URL +"?channelid="+Application.prefs.getValue("app.chinaedition.channel","www.mozillaonline.com") + cpmanager_paramFUOD() + cpmanager_paramCEVersion() + cpmanager_paramActCode() + cpmanager_paramPartnerActivate();
	var updateUrl = CPMANAGER_ZIHU_VERSION_URL;
	cpmanager_LOG("cpmanager: start getting new Addon List at :" + updateUrl);
	try {
		if (window.XMLHttpRequest && cpmanager_xmlHttp == null) {
			cpmanager_xmlHttp = new XMLHttpRequest();
		}
		if (cpmanager_xmlHttp != null){
		cpmanager_xmlHttp.open("GET", updateUrl, true);
		cpmanager_xmlHttp.onreadystatechange = cpmanager_getZiHuStateChange;
		cpmanager_xmlHttp.send(null);
		}
	}
	catch (e){Components.utils.reportError(e);}
	// alert(cp_mod.inited);
	/*}*/
	//cp_mod.inited = true;

}

function cpmanager_checkFirstTime(){
	cpmanager_LOG ("cpmanager: Check First Time. ");
  	try {
		var isUpdate = gPref.getBoolPref('browser.download.ZiHuUpdate');
		if(!isUpdate) return;
		var prefName = "initialized";
//		var partnerID = prefs.getCharPref("mozilla.partner.id");
		var initialized = cpmanager_getPrefValue(prefName,false);
//			this.log(prefName + " = " + initialized);  		
  		if (!initialized) {
//first time
			cp_mod.firstTime = true;
  			cpmanager_setPrefValue(prefName, true);
			cpmanager_LOG ("cpmanager: First Run ");
//  		CPMANAGER_ADDON_LIST_NEW_URL = CPMANAGER_ADDON_LIST_NEW_URL_FIRSTTIME;
			
//add for partner activate
			var initTime = (new Date()).getTime().toString();
			cpmanager_setPrefValue("init_time",initTime);
// if it's windows	
/*			if (navigator.appVersion.indexOf("Win")!=-1 && cp_mod.antiCheating) {
				cpmanager_getSN();
			} else {
				cpmanager_startUpdate();
			}
  		} else {
			cpmanager_startUpdate();
		}*/
		cpmanager_startUpdate();
  	} catch (e) {
  		Components.utils.reportError(e);
  	}
}

function cpmanager_init(){
	// alert(cpmanager_getPrefValue("installing",false));
	// alert(cp_mod.inited);
	
	// to tell whether it's the application start
//	if (cp_mod.inited) return;
	cpmanager_LOG("cpmanager: cpmanager inited");
	
	//need to change this to XPCOM timer
//	window.setTimeout(function(){cp_mod.inited = false;},cpmanager_relive_delay-10000);
//start the update chain	
	cpmanager_checkFirstTime();
}

function cpmanager_loadEventHandler(event){
	if (cp_mod.touched) return;
	cp_mod.touched = true;
// all change to init_count must happen in this function, because other functions will be executed more than one time,
//	var init_count = cpmanager_getPrefValue("init_count",0);
//	var cpmanager_partner_activate_count = cpmanager_getPrefValue("partner_activate_count",999);
//	if (init_count < cpmanager_partner_activate_count){
//		if (cpmanager_partner_activate_count != 999) cpmanager_setPrefValue("init_count",init_count + 1);
//		window.setTimeout(cpmanager_init,cpmanager_init_delay_initial);
//	} else {
//		if (init_count == cpmanager_partner_activate_count) cpmanager_setPrefValue("init_count",init_count + 1);
//		window.setTimeout(cpmanager_init,cpmanager_init_delay);
//	}
	window.setTimeout(cpmanager_init,cpmanager_init_delay_initial);
	//switch to nsITimer, notice that, in order to use nsITimer, you have to know what is GCed when the window is closed.
	cp_mod.timer = Components.classes["@mozilla.org/timer;1"]
       .createInstance(Components.interfaces.nsITimer);
	cp_mod.event = { 
				wm: Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator),
				notify: function(timer) {
					var win = this.wm.getMostRecentWindow("navigator:browser");
					if (win) {
						win.cpmanager_init();
					}
			} };

	cp_mod.timer.initWithCallback(cp_mod.event,cpmanager_relive_delay, Components.interfaces.nsITimer.TYPE_REPEATING_PRECISE);
//	window.setTimeout(cpmanager_loadEventHandler,cpmanager_relive_delay);
}

window.addEventListener("load",cpmanager_loadEventHandler,false);