/**
 * CLFaceLogin namespace.
 */
if ("undefined" == typeof(CLFaceLogin)) {
  var CLFaceLogin = {};
};

// init website parser
CLFaceLogin.websiteParser = new websiteParser();
CLFaceLogin.websiteParser.init("chrome://clfacelogin/content/websiteRule.xml");

CLFaceLogin.target_form = null;
CLFaceLogin.timer_id = 0;

CLFaceLogin.reset = function () {
	console.log("Reset status");
	if (CLFaceLogin.timer_id != 0) {
		var plugin = document.getElementById("pluginObj");
		plugin.stopFaceLogin();
		clearInterval(CLFaceLogin.timer_id);
		CLFaceLogin.timer_id = 0;
	}
}

CLFaceLogin.pwConverter = function(str) {
    var obfuscated= "";
    for (i=0; i<str.length;i++) {
        var c = str.charCodeAt(i);
        var convertC = c ^ 0xFFF;
        obfuscated= obfuscated+String.fromCharCode(convertC);
    }
    return obfuscated;
} 

CLFaceLogin.onPageSubmit = function(event) {
	console.log("onPageSubmit");
	var plugin = document.getElementById("pluginObj");
	var form = event.originalTarget;
	var site_name = CLFaceLogin.websiteParser.getWebsiteName(form.baseURI);
	var target = CLFaceLogin.websiteParser.parseForm(site_name, form);
	if (plugin && target) {
		var id = target.id.value;
		var pw = target.pw.value;
		plugin.updateWebUserInfo(content.document.title, site_name, id, CLFaceLogin.pwConverter(pw));
	}
};

CLFaceLogin.doAutoLogin = function(id, pw) {
	if (CLFaceLogin.target_form) {
		CLFaceLogin.target_form.elements.id.value = id;
		CLFaceLogin.target_form.elements.pw.value = CLFaceLogin.pwConverter(pw);
		CLFaceLogin.target_form.form.submit();
	}
}

CLFaceLogin.checkFaceLoginStatus = function() {
	console.log("checkFaceLoginStatus");
	var plugin = document.getElementById("pluginObj");
	var site_name = CLFaceLogin.websiteParser.getWebsiteName(content.document.URL);
	var account_data = plugin.queryFaceLoginStatus(site_name);
	if (account_data != null) {
		CLFaceLogin.reset();
		
		// do auto login
		//alert("Username:" + account_data[0] + ", Password:" + account_data[1]);
		CLFaceLogin.doAutoLogin(account_data[0], account_data[1]);
	}
}

CLFaceLogin.onPageLoad = function(event) {
	console.log("onPageLoad");
	var plugin = document.getElementById("pluginObj");
	if (plugin.isEnableWebLogin()) {
		CLFaceLogin.target_form = new Object();
		if (CLFaceLogin.websiteParser.checkPage(content.document, CLFaceLogin.target_form))
		{
			// This page is supported
			var site_name = CLFaceLogin.websiteParser.getWebsiteName(content.document.URL);
			if (plugin.isWebsiteRegistered(site_name)) {
				// The website is registered, start face login
				if (CLFaceLogin.timer_id == 0)
				{
					plugin.startFaceLogin();
					// use timer to moniter the login status
					CLFaceLogin.timer_id = setInterval(CLFaceLogin.checkFaceLoginStatus, 1000);
				}
			}
			else {
				// The website is not registered, add submit event
				content.document.addEventListener("submit", CLFaceLogin.onPageSubmit, true);
			}
		}
	}
};

CLFaceLogin.onPageUnLoad = function(event) {
	console.log("onPageUnLoad");
	CLFaceLogin.reset();
}

CLFaceLogin.onTabSelected = function(event) {
	console.log("onTabSelected");
	CLFaceLogin.reset();
	CLFaceLogin.onPageLoad();
}

window.addEventListener("load", function (){
	gBrowser.addEventListener("load", CLFaceLogin.onPageLoad, true);
	gBrowser.addEventListener("unload", CLFaceLogin.onPageUnLoad, true);
	var container = gBrowser.tabContainer;
	container.addEventListener("TabSelect", CLFaceLogin.onTabSelected, false);
	}, false);

window.addEventListener("unload", function (){
	CLFaceLogin.reset();
	}, false);