/*
    -===============-
    |azcat_panels.js|
    -===============-
    - This file is intended for use where ever azcat panels appear.  Specifically,
    that is anywhere the admin widget appears, for folder contents and pages based
    on folder contents, admin template navigation (for multiupload and about azcat)
    and edit form for versions actions.
*/

/*global AZCAT,YAHOO,recursivePurgeAndRemove,deleteItem,addMember,deleteMember,
         disableMember,enableMember,sendWelcomeEmail,renameItem,addPageItem,
         doTransition,sortContent,clipboard,doInfo,portal_url,exportContent,
         window */

/*jslint white: true, onevar: true, browser: true, on: true, undef: true,
         eqeqeq: true, plusplus: true, bitwise: true, regexp: true,
         newcap: true, immed: true, indent: 2, nomen: false */

AZCAT.namespace("dialog");

AZCAT.Dialog = function () {
  function convertToPixels(value) {
    return parseInt(value, 10);
  }

  function onResizeOrRenderPanel(e) {
    var maxHeight, maxWidth, scrollTop, height, width, newWidth;
    YAHOO.util.Dom.setStyle(this.body, "overflow", "auto");
    if (this.id !== "waitStatus" && this.element) {
      maxHeight = YAHOO.util.Dom.getViewportHeight() - 100;
      if (!this.has_an_overriding_width) {
        maxWidth = YAHOO.util.Dom.getViewportWidth() - 100;
        scrollTop = this.body.scrollTop;
        if (YAHOO.env.ua.ie === 6) {
          YAHOO.util.Dom.setStyle(this.body, "width", "auto");
        } else {
          YAHOO.util.Dom.setStyle(this.innerElement, "width", "auto");
        }
      }
      YAHOO.util.Dom.setStyle(this.body, "height", "auto");
      if (!this.has_an_overriding_width) {
        width = this.body.scrollWidth + (convertToPixels(YAHOO.util.Dom.getStyle(this.body, 'padding-left')) + convertToPixels(YAHOO.util.Dom.getStyle(this.body, 'padding-right')));
        YAHOO.util.Dom.setStyle(this.innerElement, "width", (width > maxWidth ? maxWidth : width) + "px");
        if (YAHOO.env.ua.ie === 6) {
          newWidth = (width > maxWidth ? maxWidth : width) - 20;
          newWidth = newWidth > 0 ? newWidth : 0;
          YAHOO.util.Dom.setStyle(this.body, "width", newWidth + "px");
        }
      }
      height = this.body.scrollHeight - (convertToPixels(YAHOO.util.Dom.getStyle(this.body, 'padding-top')) + convertToPixels(YAHOO.util.Dom.getStyle(this.body, 'padding-bottom'))) + 1;
      YAHOO.util.Dom.setStyle(this.body, "height", (height > maxHeight ? maxHeight : height) + "px");
      if (scrollTop) {
        this.body.scrollTop = scrollTop;
      }
    }
  }

  function onResizeOrRenderPanelFull(e) {
    var maxHeight, maxWidth;
    maxWidth = YAHOO.util.Dom.getViewportWidth() - 100;
    maxHeight = YAHOO.util.Dom.getViewportHeight() - 100;
    YAHOO.util.Dom.setStyle(this.body, "overflow", "auto");
    if (YAHOO.env.ua.ie === 6) {
      YAHOO.util.Dom.setStyle(this.body, "width", maxWidth + 'px');
    } else {
      YAHOO.util.Dom.setStyle(this.innerElement, "width",  maxWidth + 'px');
    }
    YAHOO.util.Dom.setStyle(this.body, "height", maxHeight + 'px');
    this.center();
  }

  function onBeforeHidePanel() {
    var myels = YAHOO.util.Dom.getElementsByClassName('bd', 'div', this.element);
    YAHOO.util.Dom.setStyle(myels[0], 'overflow', 'hidden');
    /* We subscribe destroy to hideEvent here, rather during
       initialization, because there are other functions that
       must run before destroy that are subscribed to hideEvent
       after the initialization */
    this.hideEvent.subscribe(this.destroy);
    myels = null;
  }

  function destroy() {
    YAHOO.widget.Panel.prototype.destroy.call(this);
    this.unsubscribeAll();
    YAHOO.util.Event.removeListener(window, "resize", onResizeOrRenderPanel);
    YAHOO.util.Event.removeListener(window, "resize", onResizeOrRenderPanelFull);
    if (this.dd) {
      recursivePurgeAndRemove(this.dd._domRef);
      this.dd._domRef = null;
    }
    this.close = null;
    this.innerElement = null;
    this.focusableElements = null;
    this.resizeMonitor = null;
  }

  function goFullScreen() {
    YAHOO.util.Event.removeListener(window, "resize", onResizeOrRenderPanel, this);
    this.changeBodyEvent.unsubscribe(onResizeOrRenderPanel, this);
    YAHOO.util.Event.addListener(window, "resize", onResizeOrRenderPanelFull, this, true);
    this.changeBodyEvent.subscribe(onResizeOrRenderPanelFull, this, true);
    onResizeOrRenderPanelFull.call(this);
  }

  function unFullScreen() {
    YAHOO.util.Event.removeListener(window, "resize", onResizeOrRenderPanelFull, this);
    this.changeBodyEvent.unsubscribe(onResizeOrRenderPanelFull, this);
    YAHOO.util.Event.addListener(window, "resize", onResizeOrRenderPanel, this, true);
    this.changeBodyEvent.subscribe(onResizeOrRenderPanel, this, true);
    onResizeOrRenderPanel.call(this);
  }

  function focusCloseButton() {
    if (this.close) {
      this.close.focus();
    }
  }

  return function (isMultiUpload, id, config) {
    var that, x, has_an_overriding_width,
        azcatConfig = {width: "30em",
                       fixedcenter: true,
                       constraintoviewport: true,
                       underlay: "none",
                       close: true,
                       visible: false,
                       draggable: true,
                       modal: true,
                       effect: {effect: YAHOO.widget.ContainerEffect.FADE, duration: 0.25}
                       };
    for (x in config) {
      if (config.hasOwnProperty(x)) {
        azcatConfig[x] = config[x];
        if (x === "width") {
          has_an_overriding_width = true;
        }
      }
    }

    that = new YAHOO.widget.Panel(id || "azcat_dialog", azcatConfig);
    if (has_an_overriding_width) {
      that.has_an_overriding_width = true;
    }
    that.renderEvent.subscribe(onResizeOrRenderPanel, that, true);
    that.renderEvent.subscribe(that.center, that, true);
    that.showEvent.subscribe(focusCloseButton, that, true);
    YAHOO.util.Event.on(window, "resize", onResizeOrRenderPanel, that, true);
    that.changeBodyEvent.subscribe(onResizeOrRenderPanel, that, true);
    that.beforeHideEvent.subscribe(onBeforeHidePanel, that, true);
    that.headerleft = '<div class="tl"></div><span>';
    that.headerright = '</span><div class="tr"></div>';
    that.footerleft = '<div class="bl"></div><span>';
    that.footerright = '</span><div class="br"></div>';
    that.destroy = destroy;
    that.goFullScreen = goFullScreen;
    that.unFullScreen = unFullScreen;
    that.closeEvent = new YAHOO.util.CustomEvent('close_dialog_event');
    return that;
  };
}();

AZCAT.dialog.actionCode = {
  'delete': function (e, dialogParams) {
    deleteItem(e, dialogParams);
    YAHOO.util.Event.stopEvent(e);
  },
  'addMember': function (e, dialogParams) {
    addMember(e, dialogParams);
    YAHOO.util.Event.stopEvent(e);
  },
  'deleteMember': function (e, dialogParams) {
    deleteMember(e, dialogParams);
    YAHOO.util.Event.stopEvent(e);
  },
  'disableMember': function (e, dialogParams) {
    disableMember(e, dialogParams);
    YAHOO.util.Event.stopEvent(e);
  },
  'enableMember': function (e, dialogParams) {
    enableMember(e, dialogParams);
    YAHOO.util.Event.stopEvent(e);
  },
  'welcomeEmail': function (e, dialogParams) {
    var x, xLen;
    dialogParams.cmf_uids = [];
    for (x = 0, xLen = dialogParams.ids.length; x < xLen; x += 1) {
      dialogParams.cmf_uids.push(AZCAT.siteman.dataView.getCMFUIDfromPath(dialogParams.ids[x]));
    }
    sendWelcomeEmail(e, dialogParams);
    YAHOO.util.Event.stopEvent(e);
  },
  'rename_items' : function (e, dialogParams) {
    renameItem(e, dialogParams);
    YAHOO.util.Event.stopEvent(e);
  },
  'addpage' : function (e, dialogParams) {
    addPageItem(e, dialogParams);
    YAHOO.util.Event.stopEvent(e);
  },
  'sortcontent' : function (e, dialogParams) {
    sortContent(e, dialogParams);
    YAHOO.util.Event.stopEvent(e);
  },
  'retract' : function (e, dialogParams) {
    doTransition(e, 'retract', dialogParams);
    YAHOO.util.Event.stopEvent(e);
  },
  'publish' : function (e, dialogParams) {
    doTransition(e, 'publish', dialogParams);
    YAHOO.util.Event.stopEvent(e);
  },
  'submit' : function (e, dialogParams) {
    doTransition(e, 'submit', dialogParams);
    YAHOO.util.Event.stopEvent(e);
  },
  'reject' : function (e, dialogParams) {
    doTransition(e, 'reject', dialogParams);
    YAHOO.util.Event.stopEvent(e);
  },
  'pastehere' : function (e, dialogParams) {
    AZCAT.siteman.dataView.pasteHere(e, dialogParams);
    YAHOO.util.Event.stopEvent(e);
  },
  'cut' : function (e, dialogParams) {
    var x, xLen;
    clipboard.items = [];
    for (x = 0, xLen = dialogParams.ids.length; x < xLen; x += 1) {
      clipboard.items.push(AZCAT.siteman.dataView.getCMFUIDfromPath(dialogParams.ids[x]));
    }
    clipboard.action = 'cut';
    AZCAT.siteman.dataView.insertPasteAction();
    YAHOO.util.Event.stopEvent(e);
  },
  'copy' : function (e, dialogParams) {
    var x, xLen;
    clipboard.items = [];
    for (x = 0, xLen = dialogParams.ids.length; x < xLen; x += 1) {
      clipboard.items.push(AZCAT.siteman.dataView.getCMFUIDfromPath(dialogParams.ids[x]));
    }
    clipboard.action = 'copy';
    AZCAT.siteman.dataView.insertPasteAction();
    YAHOO.util.Event.stopEvent(e);
  },
  'revert' : function (e, dialogParams) {
    AZCAT.editForm.image.doRevert(e, dialogParams);
    YAHOO.util.Event.stopEvent(e);
  },
  'info' : function (e, dialogParams) {
    doInfo(e, dialogParams);
    YAHOO.util.Event.stopEvent(e);
  },
  'preformat' : function (e, dialogParams) {
    var x, xLen, cmf_uid, y, yLen, fragment;
    YAHOO.util.Event.stopEvent(e);
    for (x = 0, xLen = dialogParams.ids.length; x < xLen; x += 1) {
      cmf_uid = AZCAT.siteman.dataView.getCMFUIDfromPath(dialogParams.ids[x]);
      cmf_uid = dialogParams.callback_scope._parent.data.getData(cmf_uid);
      YAHOO.util.Cookie.set("prefilter", cmf_uid.title, {"path" : "/"});
      for (y = 0, yLen = cmf_uid.actions.length; y < yLen; y += 1) {
        if (cmf_uid.actions[y].id === "preformat") {
          fragment = JSON.parse(cmf_uid.actions[y].onclick.replace("javascript:", "")).fragment;
          break;
        }
      }
      window.location.href = ([portal_url, "/site_manager#", fragment]).join("");
      return;
    }
  },
  'exportContent' : function (e, dialogParams) {
    AZCAT.exportContent = AZCAT.ExportContent(e, dialogParams);
    YAHOO.util.Event.stopEvent(e);
  }
};

AZCAT.dialog.actionConditions = {
  'delete': function (params) {
    if (params.id === "index_html") {
      return false;
    }
    if ((AZCAT.siteman.dataView.getViewType() === 'Tree' &&
         params.cmf_uid !== AZCAT.siteman.data.root_cmf_uid) ||
        AZCAT.siteman.dataView.getViewType() !== 'Tree') {
      return true;
    }
  },
  'cut': function (params) {
    if (params.id === "index_html") {
      return false;
    }
    if (AZCAT.siteman.dataView.getViewType() === 'Tree' &&
        params.cmf_uid !== AZCAT.siteman.data.root_cmf_uid) {
      return true;
    }
  },
  'copy': function (params) {
    if (params.id === "index_html") {
      return false;
    }
    if (AZCAT.siteman.dataView.getViewType() === 'Tree' &&
        params.cmf_uid !== AZCAT.siteman.data.root_cmf_uid) {
      return true;
    }
  }
};
