//-----------------------------------------------------------------------
// Module name   : LmooMenuExpand
// Author        : Paul Battersby
// Creation Date : 11/07/09
//
//  This is meant to allow a single level of sub menu, in a side menu, that
//  survives reloading the page.
//
//  This uses Fx.Accordion and cookies to restore the previously revealed
//  sub menu. Currently this only allows one sub menu to be open at a time
//  which is fine because this is meant as part of a navigation menu
//
//  Usage:
//
//    new LmooMenuExpand("containerName", ".togglerClass", ".elementsClass");
//
//    <div id="container">
//      <div class="togglerClass">Menu 1</div>
//      <div class="elementsClass">
//        <a href="#">Sub item 1-a</a><br>
//        <a href="#">Sub item 1-b</a>
//      </div>
//      <div class="togglerClass">Menu 2</div>
//      <div class="elementsClass">
//        <a href="#">Sub item 2-a</a><br>
//        <a href="#">Sub item 2-b</a>
//      </div>
//    </div>
//
//  $Log: LmooMenuExpand.js $
//  Revision 1.7  2010-05-04 18:56:54-04  Battersby
//  - now extends Fx.Accordion
//
//  Revision 1.6  2010-04-26 11:20:03-04  Battersby
//  - specifying a container a contianer is now options - default = body
//
//  Revision 1.5  2010-04-21 12:42:20-04  Battersby
//  - added usage documentation
//
//  Revision 1.4  2010-03-24 00:10:28-04  Battersby
//  - no longer checks for Class already defined (jsLint doesn't like
//    testing a variable before it's defined)
//  - added radix to parseInt()
//
//  Revision 1.3  2010-03-23 22:57:41-04  Battersby
//  - was only working previously due to a bug
//  - no longer shows an animation when restoring previous expanded state,
//  opened menus now simply stay open
//
//  Revision 1.2  2009-11-08 08:51:04-04  Battersby
//  - made the selector search for togglers and elements more efficient by restricing
//  the search to within the container
//
//  Revision 1.1  2009-11-07 16:52:55-04  Battersby
//  Initial revision
//
//------------------------------------------------------------------------

//---------------------------- INCLUDE FILES -----------------------------

var LmooMenuExpand = new Class({
  Extends : Fx.Accordion,
  Implements: [Events, Options],

//----------------------------- CONSTANTS --------------------------------


//----------------------------- VARIABLES --------------------------------

  options : {
    objId : 0,         // needs to be a unique number or id name if more than one
                       // instantiation of this class will exist
                       // BUT note: Nested accordions will not work

    alwaysHide : true, // by default, allow all togglers to be closed
    display : -1,
    show : -1         // by default, don't expand anything at startup
  },

  accordion : null, // accordion object

//----------------------------- FUNCTIONS --------------------------------

  //************************************************************************
  // Name   : initialize (constructor)
  //
  //  (object) options - (optional) configuration options. See options declaration
  //                     above for the available options
  //
  // Returns : (nothing)
  //*************************************************************************
  initialize : function(containerName, togglerClass, elementsClass, options) {

    // if no container was given, use the body as the container
    if (!containerName) {
      containerName = $(document.body);
    } // endif

    // restrict the search for the togglers and elements to within the container class
    var togglers = $(containerName).getElements(togglerClass);
    var elements = $(containerName).getElements(elementsClass);
    var currIndex;

    // give each toggler an index property
    togglers.each(function(item,index) {
      item.setProperty("data-lmooMenuExpandIndex",index);
    });

    // initialized the given options
    this.setOptions(options);

    // if an element was previously opened
    currIndex = Cookie.read("lmooMenuExpandIndex" + this.options.objId);
    if (currIndex) {
      this.options.show = parseInt(currIndex,10);
    } // endif

    // initialize the accordion parent
    this.parent($(containerName),togglers, elements,this.options);

    // add our own code to the tail end of whatever the user configured
    // for these events
    this.addEvents({
      onActive : function(toggler,element) {
        var index = toggler.getProperty("data-lmooMenuExpandIndex");
        Cookie.write("lmooMenuExpandIndex" + this.options.objId,index);
      },

      onBackground : function(toggler,element) {
        // delete the cookie but only if we are closing the currently open element
        // such that there will be no open elements anymore
        if (Cookie.read("lmooMenuExpandIndex" + this.options.objId) == toggler.getProperty("data-lmooMenuExpandIndex")) {
          Cookie.dispose("lmooMenuExpandIndex" + this.options.objId);
        } // endif
      }
    });
  }

});

