// $Id: activemenu.js,v 1.1 2007-04-07 05:34:07 jkamante Exp $

Drupal.activemenuAutoAttach = function () {
  // The elements supported. Each can designate a different uri.
  var menus = Drupal.settings.activemenu;

  for (var menu in menus) {
    $(menu + ' li.expanded:not(.activemenu-processed)').each(function () {
      Drupal.preventSelect(this);
      $(this)
        .click(function (e) {
          // Only toggle if this is the element that was clicked.
          // Otherwise, a parent li element might be toggled too.
          if (this == Drupal.getTarget(e)) {
						if($(this).hasClass('collapsed')) {
							$(this)
							.toggleClass('collapsed').toggleClass('expanded')
							.find('ul').slideDown("slow");
						}
						else if($(this).hasClass('expanded'))
						{
							$(this)
							.toggleClass('collapsed').toggleClass('expanded')
							.find('ul').slideUp("slow");
						}
          }
        })
        .addClass('activemenu-processed')
        .find('ul')
        .slideDown('fast', function () {
          $(this).css(
            {height: ''}
          );
        });
      });
    $(menu + ' li.collapsed:not(.activemenu-processed)').each(function() {
      if ($(this).children('ul').length > 0) {
        return;
      }
      var path = Drupal.getPath(this.firstChild.getAttribute('href', 2));
      var url = Drupal.url(menus[menu]);
      var elt = this;
      Drupal.preventSelect(this);
      $(this)
        .click(function (e) {
	//alert(e.target.nodeName)
	
var offset = Drupal.mousePosition(e).x - Drupal.absolutePosition(this).x;
          // Determine if we are in the selection area.
	 // the below test seemed to check that the click was to the left of the item. disabled that check to allow the expander to appear on the right instead
	 // dman 2007-04-07
	  if (e.target.nodeName=='LI' || offset < (0 + parseInt($(this).css('padding-left')))) {
	   // Ajax GET request for autocompletion
            $.ajax({
              type: 'POST',
              url: url,
              data: 'path=' + path,
              success: function (data) {
                if ($(elt).children('ul').length > 0) {
                  return;
                }
                data = Drupal.parseJson(data);
                $(elt)
                  .append(data.content)
                  .removeClass('collapsed')
                  .addClass('expanded')
                  .unclick()
                  .click(function (e) {
                    // Only toggle if this is the element that was clicked.
                    // Otherwise, a parent li element might be toggled too.
                    if (this == Drupal.getTarget(e)) {
											if($(this).hasClass('collapsed')) {
												$(this)
												.toggleClass('collapsed').toggleClass('expanded')
												.find('ul').slideDown("slow");
											}
											else if($(this).hasClass('expanded'))
											{
												$(this)
												.toggleClass('collapsed').toggleClass('expanded')
												.find('ul').slideUp("slow");
											}
										}
                  })
                  .find('ul')
                  .slideDown();
                Drupal.activemenuAutoAttach();
              },
              error: function (xmlhttp) {
                alert('An HTTP error '+ xmlhttp.status +' occured.\n' + url);
              }
            });
            return false;
          }
        })
        .addClass('activemenu-processed');
    });
  }
}

// Global Killswitch
if (Drupal.jsEnabled) {
  $(document).ready(Drupal.activemenuAutoAttach);
}

