
var trayIcon = {};

trayIcon._icon = null;

trayIcon._platform = Components.classes["@mozilla.org/xre/app-info;1"].getService(Components.interfaces.nsIXULRuntime).OS;

trayIcon.init = function () {
  
  // trayIcon only active on win32
  if (navigator.platform.indexOf("Win")==-1)
    return;
  
  // We need to open an hidden window for the tray menupopup
  // Because if we use it in the main window (main.xul)
  // this popup will not show up when the main window is minimized :'(
  
  // So open one hidden window (dependant=yes -> not in taskbar, enabled/visibility to false hide it from screen)
  var win = window.open("chrome://yoonorunner/content/tray-icon-hidden-window.xul","wnd-tray","chrome=yes,dependant=yes,titlebar=no");
  var baseWin = win.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                                        .getInterface(Components.interfaces.nsIWebNavigation)
                                        .QueryInterface(Components.interfaces.nsIDocShell)
                                        .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
                                        .treeOwner
                                        .QueryInterface(Components.interfaces.nsIBaseWindow);
  baseWin.visibility = false;
  baseWin.enabled = false;
  
  // Now create *the* tray icon, by using the minimzetotray binary component!
  // But we need to wait some internal window loading
  // in order to have a valid icon!
  window.setTimeout(function () {
    trayIcon._icon = Components.classes["@codefisher.org/minimizetotray/window-icon;1"]
        .createInstance(Components.interfaces.trayIWindowIcon);
    
    trayIcon._icon.setup(window, "Yoono desktop");
    trayIcon._icon.showIcon();
  }, 1500);
  
  // Give a trayIcon object reference to this window
  // because it's used on all oncommand/onclick
  win.trayIcon = trayIcon;
  
  // And finally as some listeners to track trayIcon events
  window.addEventListener("TrayClick", function(event) {
		if (event.button == 0) { // Left click
			trayIcon.restore();
		} else if(event.button == 2) { // Right click
      var popup = win.document.getElementById('tray-menu');
      popup.openPopupAtScreen(event.screenX, event.screenY, true);
    }
  },true);
  
  window.addEventListener("TrayDblClick", function () {
    trayIcon.restore();
  }, true);
  
  // Try to remove icon before quitting
  window.addEventListener("unload",function () {
    window.removeEventListener("unload",arguments.callee,false);
    trayIcon._icon.hideIcon();
  },false);
  
}

trayIcon.addNetwork = function (callback) {
  this.restore();
  addNetwork();
}

trayIcon.showFriendsConfig = function (event) {
  showFriendsConfig(event);
}

trayIcon.restore = function (callback) {
  var xulWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
           .getInterface(Components.interfaces.nsIWebNavigation)
           .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
           .treeOwner
           .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
           .getInterface(Components.interfaces.nsIXULWindow);
  xulWindow.QueryInterface(Components.interfaces.nsIBaseWindow).visibility = true;
  
  window.setTimeout(function () {
    try {
      var chromeWindow = window.QueryInterface(CI.nsIDOMChromeWindow);
      chromeWindow.restore();
      
      var desktop = Components.classes["@mozilla.org/desktop-environment;1"].getService(CI.nsIDesktopEnvironment);
      desktop.setZLevel(window, CI.nsIDesktopEnvironment.zLevelTop);
    } catch(e) {
      Components.utils.reportError(e);
    }
    if (typeof callback=="function") {
      callback();
    }
  }, 100);
}

trayIcon.minimize = function () {
  var chromeWindow = window.QueryInterface(CI.nsIDOMChromeWindow);
  chromeWindow.minimize();
}

trayIcon.close = function () {
  var chromeWindow = window.QueryInterface(CI.nsIDOMChromeWindow);
  chromeWindow.minimize();
  
  var xulWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
           .getInterface(Components.interfaces.nsIWebNavigation)
           .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
           .treeOwner
           .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
           .getInterface(Components.interfaces.nsIXULWindow);
  xulWindow.QueryInterface(Components.interfaces.nsIBaseWindow).visibility = false;
  
  if (navigator.platform.indexOf("Win")==-1)
    return;
  
  var YServices = YOONO_CMPT.getYServices();
  YServices.core.require("relativePopups",function () {
    var title = "Closing Yoono";
    var text = "";
    text += "You will continue to receive pop up alerts and IM notifications "
    text += "even though Yoono is closed. To sign out instead, select \"Sign Out\" ";
    text += "from the Yoono menu. ";
    var disableText = "Don't show this again";
    YServices.relativePopups.open("yd-close","screen-bottom-right",0,0,null,title,text,disableText);
  });
}

trayIcon.isOpen = function () {
  var chromeWindow = window.QueryInterface(CI.nsIDOMChromeWindow);
  return chromeWindow.windowState != chromeWindow.STATE_MINIMIZED;
}

trayIcon.quit = function () {
  var chromeWindow = window.QueryInterface(CI.nsIDOMChromeWindow);
  trayIcon.saveColumns(chromeWindow);
  setTimeout(function() {window.close()}, 300);
}

// changing the document in the column iframe will trigger the finalize method that will
// save the columns configuration
trayIcon.saveColumns = function(chromeWindow) {
  try {
    var colIframe = chromeWindow.document.getElementById("columns-iframe");
    colIframe.setAttribute('src', 'about:blank');
  } catch(e) {
    YServices.log.exception(e);
  }
}

window.addEventListener("load",function () {
  
  window.removeEventListener("load",arguments.callee,false);
  
  trayIcon.init();
  
  var minimizeButton = document.getElementById("minimize-button");
  minimizeButton.addEventListener("click",function () {
    
    trayIcon.minimize();
    
  }, false);
  
}, false);
