"use strict";
var EXPORTED_SYMBOLS = ["module"];
var module = function (application) {
var log = function (message) {
application.logger.trace("[Notification Window] " + message);
}
;
var _windowsCounter = 0;
function NotificationWindow(data, position) {
this._id = _windowsCounter++;
this._owner = data.owner;
this._data = data.data;
this._window = null;
this._initialPosition = position;
this.init(data);
log("Create notification @ " + JSON.stringify(position));
}

NotificationWindow.DEFAULT_WIDTH = 350;
NotificationWindow.DEFAULT_HEIGHT = 200;
NotificationWindow.prototype = {
constructor: NotificationWindow,
init: function NotificationWindow_init(data) {
this._window = this.open(data);
this._window.externalInterface = this.createExternal();
}
,
finalize: function NotificationWindow_finalize() {
this.close();
}
,
open: function NotificationWindow_open(data) {
const WINDOW_NAME = "yb-notification-" + Date.now();
var screen = this._screen;
var features = ["chrome=yes", "popup=yes", "titlebar=no", "width=" + NotificationWindow.DEFAULT_WIDTH, "height=" + NotificationWindow.DEFAULT_HEIGHT, "top=" + screen.availHeight, "left=" + screen.availWidth];
return this._windowWatcher.openWindow(null,data.page,WINDOW_NAME,features.join(","),null);
}
,
close: function NotificationWindow_close() {
if (! this.isOpened)
return;
var id = this.id;
this._window.close();
this._mailman.send({
subject: "notification-window-closed",
body: id});
}
,
moveTo: function NotificationWindow_moveTo(x, y) {
if (! this.isOpened)
return;
this._window.moveTo(x,y);
}
,
resizeTo: function NotificationWindow_resizeTo(params) {
if (! this.isOpened)
return;
var width = this.width, height = this.height;
if ("undefined" != typeof params)
{
if ("undefined" != typeof params.width)
{
width = params.width;
}

if ("undefined" != typeof params.height)
{
height = params.height;
}

}

var xDiff = this.width - width;
var yDiff = this.height - height;
var window = this._window;
window.resizeTo(width,height);
window.moveBy(xDiff,yDiff);
log("Resize to " + width + "x" + height);
}
,
createExternal: function NotificationWindow_createExternal() {
var External = application.loadModule("ExternalInterface");
var external = new External();
external.setExternalReadyState(true);
var notification = this;
external.addHandler("data_request",function () {
external.sendToExternal("data",JSON.stringify(notification.data));
}
);
external.addHandler("resize_request",function (topic, height) {
var window = notification._window;
var initialPosition = notification._initialPosition;
var moveAndResize = function () {
log("Move to " + JSON.stringify(initialPosition));
window.moveTo(initialPosition.x,initialPosition.y);
notification.resizeTo({
height: height});
notification._mailman.send("notification-window-created");
}
;
if (notification._isLinux)
{
window.addEventListener("load",function () {
window.setTimeout(moveAndResize,0);
}
,false);
}
 else
{
moveAndResize();
}

}
);
external.addHandler("close",function () {
notification._mailman.send({
subject: "notification-window-close-request",
body: notification.id});
}
);
external.addHandler("open_global_settings",function () {
notification._mailman.send("open-settings");
}
);
external.addHandler("mouse",function (topic, action) {
notification._mailman.send({
subject: "mouse-action",
body: action});
}
);
external.addDefaultHandler(function (topic, data) {
notification._mailman.send({
subject: "send-to-owner",
body: {
from: notification.id,
message: {
subject: topic,
body: data}}});
}
);
return external;
}
,
get id() {
if (! this.isOpened)
return;
var window = this._window;
var util = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor).getInterface(Components.interfaces.nsIDOMWindowUtils);
var id = util.currentInnerWindowID;
if ("undefined" == typeof id)
{
id = this._id;
}

return id;
}
,
get owner() {
return this._owner;
}
,
get data() {
return this._data;
}
,
get isOpened() {
return null !== this._window && ! this._window.closed;
}
,
get width() {
if (this.isOpened)
{
return this._window.outerWidth;
}

}
,
get height() {
if (this.isOpened)
{
return this._window.outerHeight;
}

}
,
get position() {
if (this.isOpened)
{
return {
x: this._window.screenX,
y: this._window.screenY};
}

}
,
_osName: null,
get _isLinux() {
if (null === this._osName)
{
this._osName = application.api.Environment.os.name;
}

return this._osName == "linux";
}
,
__windowWatcher: null,
get _windowWatcher() {
if (null === this.__windowWatcher)
{
this.__windowWatcher = Components.classes["@mozilla.org/embedcomp/window-watcher;1"].getService(Components.interfaces.nsIWindowWatcher);
}

return this.__windowWatcher;
}
,
__windowMediator: null,
get _windowMediator() {
if (null === this.__windowMediator)
{
this.__windowMediator = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
}

return this.__windowMediator;
}
,
get _screen() {
var mainWindow = this._windowMediator.getMostRecentWindow("navigator:browser");
return mainWindow.screen;
}
,
__mailman: null,
get _mailman() {
return this.__mailman ? this.__mailman : (this.__mailman = application.loadModule("Mailman"));
}
};
return NotificationWindow;
}
;
