document.write('<link type="text/css" rel="stylesheet" media="screen" href="/css/menu_js.css" />');

$.fn.extend({
  menu: function(options) {
    return this.each(function() {
      new $.Menu(this, options);
    });
  }
});

$.Menu = function(root_menu, options)
{
  /*private variables which contains the current menu panel marker*/
  var img_selected;

  /*options handling section*/
  var opt = options || {};
  /**
   * callback function call on each click on a menu link, can be overriden
   */
  (opt.main_offset==undefined)? opt.main_offset = -46:opt.main_offset = opt.main_offset;
  opt.click_callback = opt.click_callback || function(e){
    if (0 == arguments.length)
    {e = window.event}

    if ('a' == this.tagName.toLowerCase())
    {
      this.blur();
      if(e.stopPropagation)
      {
        e.stopPropagation();
      }
      else
      {
        e.cancelBubble = true;
      }
      alert(this.href);
      return false;
    }
  };
  /*--------------*/

  /**
   * bind all link and list event
   */
  function setHandlers(level)
  {
    $('>li',level).each(function(){
      if (0 != $('>ul',this).length)
      {
        $(this).each(function(){
          $(this)[0].onclick = opt.click_callback;
          $(this).hover(
            function(e)
            {
              openRootMenu.apply(this);
            },
            function(e)
            {
              closeRootMenu.apply(this);
            }
          );
        });
        setHandlers($('>ul',this)[0]);
      }
      else
      {
        $(this).each(function(){
          $(this).hover(
            function(e)
            {
              $(this).addClass('onroot');
            },
            function(e)
            {
              $(this).removeClass('onroot');
            }
          );
          $(this)[0].onclick = opt.click_callback;
          $(this.parentNode)[0].onclick = opt.click_callback;
        });
      }
    });
  }
  /*--------------*/

  /**
   * handle mouse over a menu panel
   */
  function openRootMenu()
  {
    $(this).addClass('onroot');
    var menu = $('>ul',this)[0];
    var max_width = 0;
    $('>li>a',menu).each(function(){
      if (this.offsetWidth > max_width)
      {
        max_width = this.offsetWidth;
      }
    });
    img_selected = document.createElement('span');
    $(this)[0].appendChild(img_selected);
    $(img_selected).addClass('selected_level');
    img_selected.style.left = Math.round(this.offsetWidth/2 - 147/2) + 'px';
    menu.style.top = '38px';
    menu.style.left = (opt.main_offset - menu.parentNode.offsetLeft) + 'px';
    img_selected.style.visibility = 'visible';
  }
  /*--------------*/

  /**
   * handle mouse leaving a menu panel
   */
  function closeRootMenu()
  {
    $(this).removeClass('onroot');
    var previous_rect =  img_selected.previousSibling;
    if (1 == previous_rect.nodeType)
    {
      if ('shape' == previous_rect.tagName.toLowerCase())
      {
        first_rect = previous_rect.previousSibling;
        $(previous_rect).remove();
        if (1 == first_rect.nodeType)
        {
          if ('shape' == first_rect.tagName.toLowerCase())
          {
            $(first_rect).remove();
          }
        }
      }
    }
    $(img_selected).remove();
    $('>ul',this)[0].style.left = '-9999px';
  }
  /*--------------*/

  setHandlers(root_menu);
}

$(document).ready(function(){
  $('ul.menu_bar').menu({main_offset:-46});
});