// $Id: jquerymenu.js,v 1.6 2009/11/16 08:44:48 aaronhawkins Exp $
Drupal.behaviors.jquerymenu = function(context) {
  /* show and hide "edit this menu item buttons */
  /*jqm_showit = function() {
    $(this).children('.jqm_link_edit').fadeIn();
  }
  jqm_hideit = function() {
    $(this).children('.jqm_link_edit').fadeOut();
  }
  $('ul.jquerymenu li').hover(jqm_showit, jqm_hideit);*/

  /* sb patch */
  /* init debugging - set debug = false to disable debugging */
  var debug = false;
  if (debug) { 
    $('body').append('<div id="debug-trace-display" style="position:fixed; bottom:0; right:0; width:5	00px; height:auto; max-heigt:100%; overflow:auto; background-color:#eeeebb; display:block;"></div>'); 
  }  
  jqm_debug = function(message) {
    /* debugging - quote this line too, when debugging is disabled (for performance only) */
    if (debug) { 
      $('#debug-trace-display').append("<div>"+message+"</div>"); // +"\n");
    }
  }
  jqm_debug('Debug output:');
  /* /debugging */


  /* This method captures menu changes (i.e. menu items get folded and unfolded).
     All changes are stored in a cookie called "jqmState" (modify by changing variable "cookieName").
     The cookie contains a list of all opened items. 
  */
  jqm_cookieName = 'jqmState';

  jqm_getState = function() {
    var cookie = "";
    if (document.cookie.length>0) {
      var c_start=document.cookie.indexOf(jqm_cookieName + "=");
      if (c_start!=-1) {
	c_start=c_start + jqm_cookieName.length+1 ;
	var c_end=document.cookie.indexOf(";",c_start);
	if (c_end==-1) c_end=document.cookie.length
	cookie = unescape(document.cookie.substring(c_start,c_end));
      }
    }

    /* parse cookie */
    var nodes = new Array();
    if (cookie.length > 0) {
      nodes = cookie.split(',');
    }

    return nodes;
  }

  jqm_state = function(menuItemId, open) {
    /* sanitary code */
    if (typeof(menuItemId) == "string") {
      menuItemId = new Number(menuItemId);
    }
    if (typeof(menuItemId) == "object") {
      menuItemId = $(menuItemId).attr('id');
    }
    //if (typeof(menuItemId) != "number" && isNaN(menuItemId)) {
    if (!menuItemId || typeof(menuItemId) != "string" || menuItemId.length < 1) {
      jqm_debug('invalid menuItemId: '+menuItemId);
      return;
    }
    
    
    /* init */
    jqm_debug("change jqm state for "+menuItemId+" to be opened: "+open);
    var cookieName = jqm_cookieName; //'jqmState';
    var cookieExpireDate = new Date();
    cookieExpireDate.setDate(cookieExpireDate.getDate() + 1);

    /* load cookie data */
    var nodes = jqm_getState();
    jqm_debug("old nodes: "+nodes);

    /* is node in already? */
    var nodeIndex = -1;
    for (var x = 0; x < nodes.length && nodeIndex < 0; x++) {
      if (menuItemId == nodes[x]) {
	nodeIndex = x;
      }
    }

    if ((open && (nodeIndex >= 0)) || (!open && (nodeIndex < 0))) {
      /* nothing to do */
      return;
    }

    if (open) {
      /* add node to storage */
      nodes.push(menuItemId);
    }
    else {
      nodes.splice(nodeIndex, 1);
    }
    jqm_debug("new nodes: "+nodes);

    /* store cookie */
    var cookie = cookieName + "=" + escape(nodes.join(',')) + "; path=/; expires=" + cookieExpireDate.toGMTString();
    jqm_debug("new cookie "+cookie);
    document.cookie = cookie;
  }

  /* restore menu setting (i.e. folding of each item) based on the stored cookie */
  jqm_restoreMenu = function() {
    var nodes = jqm_getCookie();
    for (var x = 0; x < nodes.length && nodeIndex < 0; x++) {
      
    }
  }
  /* /sb patch */

  var nodesToBeRestored = jqm_getState();
  $('ul.jquerymenu:not(.jquerymenu-processed)', context).addClass('jquerymenu-processed').each(function(){
    $(this).find("li.parent span.parent")/* sb patch */.each(function() {
      /* restore state */
      momma = $(this).parent();
      jqm_debug('r-test: '+ $(momma).attr("id") +' in '+ nodesToBeRestored.join() +' ? '+ jQuery.inArray($(momma).attr("id"), nodesToBeRestored));
      if (jQuery.inArray($(momma).attr("id"), nodesToBeRestored) > -1) {
	  jqm_debug('# restore '+$(momma).attr("id"));
	  $($(this).siblings('ul').children()).show();
	  //$(momma).children('ul').slideDown('700');
	  $(momma).removeClass('closed').addClass('open');
	  $(this).removeClass('closed').addClass('open');
      }
    })/* /sb patch */.click(function(){
      momma = $(this).parent();
      /* sb patch */
      var toBeOpened = $(momma).hasClass('closed');
      jqm_state($(momma).attr("id"), toBeOpened);
      /* /sb patch */
      if (toBeOpened){
        $($(this).siblings('ul').children()).hide().fadeIn('3000');
        $(momma).children('ul').slideDown('700');
        $(momma).removeClass('closed').addClass('open');
        $(this).removeClass('closed').addClass('open');
        jqm_state($(momma), true);
      }
      else{
        $(momma).children('ul').slideUp('700');
        $($(this).siblings('ul').children()).fadeOut('3000');
        $(momma).removeClass('open').addClass('closed');
        $(this).removeClass('open').addClass('closed');
        jqm_state($(momma), false);
      }
    });
  });

}
