var EXPORTED_SYMBOLS = ["YOONO_DIALOGS"];


Components.utils.import("resource://yoono/yoonoPrefs.js");

// Yoono stuff
var yoono = {};
var log = {info:function() {},debug:function() {},warn:function(){},error:function (){},fatal:function(){}};


// Vars
const OVERLAYTHROBBER = "yoono-throbber-button";

// Globals
const CI = Components.interfaces;
const CL = Components.classes;
const STRBUNDLE = CL["@mozilla.org/intl/stringbundle;1"].getService(CI.nsIStringBundleService);
const WMED = CL['@mozilla.org/appshell/window-mediator;1'].getService(CI.nsIWindowMediator);
const PROMPTS = CL["@mozilla.org/embedcomp/prompt-service;1"].getService(CI.nsIPromptService);


function Dialogs() {
	this.wrappedJSObject=this;
	
	this._connecting = false;

	this.bundle = STRBUNDLE.createBundle('chrome://yoono/locale/yoono.properties');
}

Dialogs.prototype.init = function (y) {
	
	yoono=y;
	log=y.log;
	
}

Dialogs.prototype.traverseBrowsers = function (fun) {
	var windowsenum = WMED.getEnumerator("navigator:browser");
	var win;
	while (windowsenum.hasMoreElements()) {
		win = windowsenum.getNext();
		fun (win);
	}
},

Dialogs.prototype.setDialog = function (name, param) {
	var uri = 'chrome://yoono/content/dialogs/' + name + '.xul';
	win = this.getFirstBrowser();
	win.openDialog(uri, name, 'chrome, modal', param);
},

Dialogs.prototype.setThrobberBusy = function (action) {
  return;
	this._connecting = action;
	log.debug('throbber busy, action='+action);
	this.traverseBrowsers (function (win) {
		var el = win.document.getElementById(OVERLAYTHROBBER);
		if (el) {
			el.setAttribute("busy", (action?'true':'false'));
			el.removeAttribute("syncfailed");
		}
	});
},

// called in case any request failed or returned bad content.
// beware : not only sync requests ! must check if throbber was busy...
Dialogs.prototype.setThrobberFailed = function () {
  return;
	log.backtrace("FAILED");
	if(this._connecting) {
		log.debug('throbber set to Failed');
		this._connecting = false;
		this.traverseBrowsers (function (win) {
			var el = win.document.getElementById(OVERLAYTHROBBER);
			if (el) {
				el.setAttribute("busy", 'false');
				el.setAttribute("syncfailed", 'true');
			}
		});
	}
},

// lors de l import, on affiche une boite de dialogue
Dialogs.prototype.warnImport = function (nbr) {
	if (!nbr) return;
	var titlestr = this.bundle.GetStringFromName('warning');
	var textstr = this.bundle.GetStringFromName('import.warning');
	textstr = textstr.replace('%1', nbr);
	return (PROMPTS.confirm(null, titlestr, textstr));
},

Dialogs.prototype.synchroinfos = function (action, nbr) {
	var textstr = this.bundle.GetStringFromName('synchroinfos' + action);
	textstr = textstr.replace('%1',nbr);
	this.traverseBrowsers(function(win){win.messageinfos.init('sync', textstr)});
},

Dialogs.prototype.notifCallback = function(url) {
	return {
		observe : function (subject, topic, data) {
			if (topic == 'alertclickcallback') {
				var win = this.getFirstBrowser();
				if (!win) return;
				var where = win.whereFromPref(YOONO_PREFS.get('reco.display-mode'));
				log.debug(where);
				var recoUrl = YOONO_PREFS.get('serverurl') + 'search.jsp?url=' + url;
				win.openUILinkIn(recoUrl, where);
			}
		}
	}
},

Dialogs.prototype.getFirstBrowser = function () {
	return WMED.getMostRecentWindow("navigator:browser");
}




var YOONO_DIALOGS = new Dialogs();