var com = com||{};
com.wo2online = com.wo2online||{};

/**
 * @package controller
 * @namespace com.wo2online
 */
com.wo2online.controller = function() {
  var menus = {};

  /**
   * Creates menus by taking an (html) id and implicitely binding it to its menu
   * sets event handlers for masking effect
   * @method createMenu
   * @param {Array} ids list of element ids to create menus for
   * @private
   */
  function createMenu(ids) {
    var mask = document.getElementById('mask');
    var m,i,id;
    for (i=0; (id=ids[i]); i++) {
      var m = new com.wo2online.Menu(id,'slidein_container_'+id,{
        animationInLength: parseFloat(window.location.hash.split('#')[1]),
        mask: mask
      });
      menus[id] = m;
      m.id = id;
      m.onbeforeshow = function() {
        mask.style.display = 'block';
        mask.style.height = document.documentElement.scrollHeight+'px';
        this.element.style.zIndex = 1000;
      };
      m.onaftershow = function() {
        com.wo2online.Cookie.drop('activeMenu','/');
        com.wo2online.Cookie.write('activeMenu',this.id,new Date(2050,0,0),'/');
      };
      m.onbeforehide = function() {
        this.element.style.zIndex = '';
      };
      m.onafterhide = function() {
        this.disabled = false;
        if (!com.wo2online.Menu.active()) {
          mask.style.display = 'none';
          com.wo2online.Cookie.drop('activeMenu','/');
        }
      };
    }
  }
  
  function restoreMenuState() {
    var activeMenu = com.wo2online.Cookie.read('activeMenu');
    if (activeMenu) {
      menus[activeMenu].disabled = true;
      menus[activeMenu].show();
      window.onload = function() {
        menus[activeMenu].slideOut();
      }
    }
  }

  /**
   * Initializes the controller
   * @method init
   * @private
   */
  function init() {
    createMenu(['1434','1899','7587']);
    restoreMenuState();
  }
  
  return {
    init: init
  };
}();

/**
 * returns found element or null
 * @method getElement
 * @namespace com.wo2online
 * @param {string|HTMLElement} idOrElement
 * @returns HTMLElement|null
 */
com.wo2online.getElement = function(idOrElement) {
  if (idOrElement.nodeType) {
    return idOrElement;
  } else if (typeof(idOrElement) === 'string') {
    return document.getElementById(idOrElement);
  } else {
    return null;
  }
};