
var trayIcon = {};

trayIcon._icon = null;

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

var gMinTrayR = {};
trayIcon.minimized = false;

trayIcon.init = function () {
  if (navigator.platform && (-1 != navigator.platform.indexOf('Win'))) {
    Components.utils.import("resource://mintrayr/mintrayr.jsm", gMinTrayR);
    gMinTrayR = new gMinTrayR.MinTrayR(
      document.getElementById("MinTrayR_context"),
      null,
      function() {
      },
      // Added for Yoono
      function onMaximize () {
        trayIcon.minimized = false;
      }
    );
  } else {
    gMinTrayR.restore = function() {window.focus()};
  }
}

trayIcon.minimize = function () {
  if (navigator.platform.indexOf("Win") != -1) {
    gMinTrayR.minimize();
  } else {
    window.minimize();
  }
  trayIcon.minimized = true;
}

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

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

trayIcon.restore = function (callback) {
  if(trayIcon.minimized) {
    gMinTrayR.restore();
  }
  trayIcon.minimized = false;
  if (typeof callback=="function") {
    callback();
  }
}

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 minimized.";
    var disableText = "Don't show this again";
    YServices.relativePopups.open("yd-close","screen-bottom-right",0,0,null,title,text,disableText);
  });
}

trayIcon.isOpen = function () {
  YOONO_LOG.error("MINIMIZED : " + trayIcon.minimized) ;
  return(!trayIcon.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();
}, false);
