/*
 * Copyright (c) OutWit Technologies 2007-2017.
 * All rights reserved.
 */

/**
 * 21 : Module creator(grubshka) please document
 * this module !
 */


var popupMenuCtrl = {};

popupMenuCtrl.viewSourceWindow = null;
popupMenuCtrl.viewImageWindow = null;

popupMenuCtrl.showPopup = function(anEvent, from) {
  popupMenuCtrl.target = anEvent.target;
  popupMenuCtrl.from = from;
  switch (from) {
    case 'catch':
      document.getElementById('owImagesImgContextMenuRemove').disabled = false;
      document.getElementById('owImagesImgStartSlideshow').disabled = true;
      //document.getElementById('owImagesImgContextMenuSaveAll').disabled = false;
      break;
    case 'thumbs':
      document.getElementById('owImagesImgContextMenuRemove').disabled = true;
      document.getElementById('owImagesImgStartSlideshow').disabled = false;
      //document.getElementById('owImagesImgContextMenuSaveAll').disabled = true;
      break;
  }
  var popup = document.getElementById('owImagesImgContextMenu');
  popup.openPopup(null, null, anEvent.clientX, anEvent.clientY, false, null);
};

popupMenuCtrl.openSourceURL = function() {
  if (popupMenuCtrl.viewSourceWindow == null || popupMenuCtrl.viewSourceWindow.closed) {
    popupMenuCtrl.viewSourceWindow = window.open(popupMenuCtrl.target.sourceUrl, "outwit-images:viewSource", "");
  }
  else {
    popupMenuCtrl.viewSourceWindow.location.href = popupMenuCtrl.target.sourceUrl;
    popupMenuCtrl.viewSourceWindow.focus();
  }
};


popupMenuCtrl.startSlideshow = function(){
  var container = document.getElementById('owImagesContainer');
  container.startSlideshow(popupMenuCtrl.target.index);
};


popupMenuCtrl.openImageURL = function() {
  var width, height;
  [width, height] = popupMenuCtrl.target.size.split(/\s*x\s*/);
  width = Math.min(1024, window.screen.width, parseFloat(width) + 20);
  height = Math.min(768, window.screen.height, parseFloat(height) + 20);

  if (popupMenuCtrl.viewImageWindow == null || popupMenuCtrl.viewImageWindow.closed) {
    popupMenuCtrl.viewImageWindow = window.open(popupMenuCtrl.target.mediaUrl,
        "outwit-images:preview",
        "menubar=no,location=no,resizable=yes,scrollbars=yes,status=no,"
        + "chrome=yes,centerscreen,width=" + width + ",height=" + height + "");
  }
  else {
    popupMenuCtrl.viewImageWindow.location.href = popupMenuCtrl.target.mediaUrl;
    popupMenuCtrl.viewImageWindow.resizeTo(width, height);
    popupMenuCtrl.viewImageWindow.focus();
  }
};

popupMenuCtrl.remove = function() {
  popupMenuCtrl.target.remove();
};

popupMenuCtrl.saveImage = function() {
  var folder = ow.app.kernel.prefs.getString("oWImages.autocatch.autosave.folder", "");
  return outwitImages.download(popupMenuCtrl.target, folder || "default", (optionsCtrl.autoRename ? optionsCtrl.autoRenamePattern : ""));
};

popupMenuCtrl.saveImageAs = function(aLocation) {
  return outwitImages.download(popupMenuCtrl.target, "ask", "");
};

popupMenuCtrl.sendImageLink = function() {
  var message = "\r\n\r\n"
              + popupMenuCtrl.target.mediaUrl
              + "\r\n\r\n\r\n\r\n"
              + coreKit.localeService.getString('mailSignatureSeparator', 'chrome://outwit-modules-images/locale/main.properties')
              + "\r\n"
              + coreKit.localeService.getString('mailSignatureSingular', 'chrome://outwit-modules-images/locale/main.properties');
  var subject = coreKit.localeService.getString('mailSubject', 'chrome://outwit-modules-images/locale/main.properties');
  coreKit.MailService.sendMessage(subject, message);
};

popupMenuCtrl.copyImageLink = function() {
  var clipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"]
                                  .getService(Components.interfaces.nsIClipboardHelper);
  clipboardHelper.copyString(popupMenuCtrl.target.mediaUrl);
};

popupMenuCtrl.setImageAsDesktopBg = function() {
//  var width, height;
//  [width, height] = popupMenuCtrl.target.size.split(/\s*x\s*/);


  var shellSvc = Components.interfaces.nsIShellService;
  var shell = Components.classes["@mozilla.org/browser/shell-service;1"]
                        .getService(shellSvc);
/*
  var widthRatio = width / window.screen.width;
  var heightRatio = height / window.screen.height;
  var imageRatio = width / height;
  var screenRatio = window.screen.width / window.screen.height

  var bgType;
  if (widthRatio > 0.8 && heightRatio > 0.8) {
    bgType = shellSvc.BACKGROUND_STRETCH;
  }
*/
  var img = new Image();
  img.onload = function() {
    shell.setDesktopBackground(this, shellSvc.BACKGROUND_CENTER);
  //window.openDialog("chrome://browser/content/setDesktopBackground.xul", "", "centerscreen,chrome,dialog=no,resizable=no,dependent", this);
  };
  img.src = popupMenuCtrl.target.mediaUrl;

};

popupMenuCtrl.explore = function(){
  outwitImages.browserControls.runFromUrl(popupMenuCtrl.target.sourceUrl);
};


popupMenuCtrl.saveAllImagesOn = function(aLocation, alwaysAsk) {
  var fm = coreKit.fileManager;

  var fileToSave;

  var imageList;

  switch (popupMenuCtrl.from) {
    case "catch":
      if (alwaysAsk) {
        fileToSave = fm.filePicker.get(coreKit.localeService.getString('saveDialogTitle', 'chrome://outwit-modules-images/locale/main.properties'));
      }
      else if (!(fileToSave = optionsCtrl.checkSaveFolder(true))) fileToSave = optionsCtrl.chooseSaveFolder(false);
      
      imageList = document.getElementById('mediacatch').childNodes;
      break;
    case "thumbs":
      fileToSave = fm.filePicker.get(coreKit.localeService.getString('saveDialogTitle', 'chrome://outwit-modules-images/locale/main.properties'));
      imageList = document.getElementById('owImagesContainer').thumbs._itemsDisplayed;
      break;
  }

  if (!fileToSave) return;

  for (var i = 0; i < imageList.length; ++i) {
    outwitImages.download(imageList[i], fileToSave, (optionsCtrl.autoRename ? optionsCtrl.autoRenamePattern : ""));
  }
};
