/*  Ajax.Menu JavaScript framework, version 1.0
 *  Copyright (c) 2005, Glenn Nilsson <glenn.nilsson@gmail.com>
 *
 *  Ajax.Menu is distributed under the terms of an Creative Commons
 *  Attribution license. In short words, you can use this however you like
 *  as long as you give me and the code credit. Read more at:
 *  http://creativecommons.org/licenses/by/2.5/
 *
 *  Requirements: Prototype framework <http://prototype.conio.net/>
 *
 *  For details, see: http://wailqill.com/code/ajax-menu/
 *
/*--------------------------------------------------------------------------*/

Ajax.Menu = Class.create();
Object.extend(Object.extend(Ajax.Menu.prototype, Ajax.Request.prototype), {
 initialize: function(id, url, options) {
  this.id = id;
  this.url = url;
  this.transport = Ajax.getTransport();
  this.addObservers();
 },
 addObservers: function() {
  var menu = $(this.id);
  if (menu) {
   var lis = menu.getElementsByTagName("li");
   var j = 0;
   for (var i=0; li=lis[i]; i++) {
      if (lis[i].getAttribute('is_menu', 2) == 1) {
      Event.observe(lis[j], "click", this.onClick.bindAsEventListener(this));
            j++;
         }
   }
  }
 },
 setIdentData: function() {
  this.options.nodeIdentKey = this.options.nodeIdentKey || this.options.nodeIdentType;
  var params = this.options.parameters || '';
  if (params.length > 0)
   params += "&";
  switch (this.options.nodeIdentType) {
   case "path":
    var re = new RegExp(this.options.nodeIdentKey + "=.*?(&|$)", "g");
    params = params.replace(re, "");
    var a = (as = this.li.getElementsByTagName("a")).length > 0 ? as[0] : null;
    if (a) {
     /*
     Thanks to:
     http://www.quirksmode.org/bugreports/archives/2005/02/getAttributeHREF_is_always_absolute.html
     */
     var path = a.getAttribute("href", 2).replace(/^[hf]t?tps?:\/\/[^\/]+/, '');
     params += this.options.nodeIdentKey + "=" + path;
    }
    break;
   case "id":
    params += this.options.nodeIdentKey + "=" + li.id;
    break;
  }
  this.options.parameters = params;
 },
 onClick: function( event ) {
  var li = Event.findElement(event, 'LI');
  Event.stop(event);
  switch (li.className) {
   case "collapsed":
    var ul = null;
    for (var i=0; node=li.childNodes[i]; i++) {
     if (node.nodeName.toLowerCase() == "ul") {
      ul = node;
      break;
     }
    }
    if (ul) {
     ul.style.display = "block";
     li.className = li.className.replace(/collapsed/, "expanded");
    } else {
     this.li = li;
    }
                new Ajax.Request('/reports/set_left_menu_session',
                                       {asynchronous:true, evalScripts:true, parameters: 'state=expanded&id='+ li.id});
                                return false;
    break;
   case "expanded":
    var uls = li.getElementsByTagName("ul");
    for (var i=0; ul=uls[i]; i++) {
     ul.style.display = "none";
     ul.parentNode.className = ul.parentNode.className.replace(/expanded/, "collapsed");

    }
    li.className = li.className.replace(/expanded/, "collapsed");

                                 new Ajax.Request('/reports/set_left_menu_session',
                                       {asynchronous:true, evalScripts:true, parameters: 'state=collapsed&id='+ li.id});
                                 return false;
    break;
  }
 }
});
