/*************************************/
/* (C) 2008 Bluedesk E-Business B.V. */
/*																	 */
/* Author: R.Muller									 */
/* Description: click based 				 */
/*************************************/


/* mootools 1.2 module */

var bdFxHlMenu = {

  _DEBUG: false,
  fxMenuHeight: null,
  fxMenuHeightClose: null,
  fnCloseMenu: null,

  initialize: function() {

    if (this._DEBUG) {
      var oDebug = new Element('div', {
        'styles': {
          'position': 'absolute',
          'border': '1px solid red',
          'left': '1px',
          'top': '300px',
          'z-index': '300',
          'min-width': '20px',
          'min-height': '20px'
        },
        'events': {
          'click': function() {
            //aaa
          },
          'mousedown': function() {
            //aaa
          }
        },
        'id': 'mnuDebugInfo'
      });


      oDebug.inject($('body'));

    }

    // Searching the menu items
    if ($('menu_main')) {
      this.fxMenuHeight = new Fx.Tween($('menu_main'), { duration: '500'});
      this.fxMenuHeightClose = new Fx.Tween($('menu_main'), { duration: '900'});

      //add link click, enter and leave events to all items A
      $$('#menu_main li a').each(function(e, i) {
        e.addEvents({
          'click': bdFxHlMenu.item_link,
          'mouseenter': bdFxHlMenu.item_mouseenter,
          'mouseleave': bdFxHlMenu.item_mouseleave
        });

      });

      //add open menu click event to LI
      $$('#menu_main li').each(function(e, i) {
        e.addEvents({
          'click': bdFxHlMenu.item_open
        });

      });

      $$('#menu_main ul.submenu').each(function(e, i) {
      e.set('morph', { duration: '500', transition: Fx.Transitions.Sine.easeOut });
        e.get('morph').start({
          'opacity': 0
        });
        //e.fade('hide');
        e.addEvent('blur', bdFxHlMenu.close_submenu);
      });

      $(document.body).addEvent('click', bdFxHlMenu.close_submenu);
    }

  },


  // Show elements
  item_open: function() {
    
    bdFxHlMenu.close_submenu();

    if (bdFxHlMenu._DEBUG)
      $('mnuDebugInfo').appendText('mouseenter ');


    var oUl = this.getElement('ul.submenu');

    var coordsMain = $('menu_main').getCoordinates();

    // Show if exists
    if (oUl !== null) {
      var coordsUl = oUl.getCoordinates();

      var iNewHeight = 0;
      $$('#menu_main li[class="Item_hover"] > ul.submenu, #menu_level1').each(function(e, i) {
        if (e != oUl)

          iNewHeight += e.getSize().y;

        if (bdFxHlMenu._DEBUG)
          $('mnuDebugInfo').appendText(e.getSize().y + ' ');
      });
      //set to front and fade in
      oUl.get('morph').start({
        'opacity': 1,
        'z-index': 20
      });
      oUl.addClass('submenu_open');
      bdFxHlMenu.fxMenuHeight.cancel();
      bdFxHlMenu.fxMenuHeightClose.cancel();
      bdFxHlMenu.fxMenuHeight.start('height', iNewHeight + coordsUl.height);


      if (bdFxHlMenu._DEBUG)
        $('mnuDebugInfo').appendText(iNewHeight + coordsUl.height + ' ');

      if (bdFxHlMenu._DEBUG)
        $('mnuDebugInfo').appendText('showsub ');
    }

    //stop event do no fall through document click a href
    return false;

  },
  item_mouseleave: function() {

    this.removeClass('Item_hover');


  },
  item_mouseenter: function() {

    this.addClass('Item_hover');

  },
  close_submenu: function(e) {
    var oUl = $(document.body).getElement('ul.submenu_open');

    if (oUl != null) {

      var coordsUl = oUl.getCoordinates();
      //oUl.setStyle('visibility', 'hidden');

      var iNewHeight = 0;

      $$('#menu_main li[class="Item_hover"] > ul.submenu, #menu_level1').each(function(e, i) {
        if (e != oUl)
          iNewHeight += e.getSize().y;
      });

      oUl.get('morph').start({
        'opacity': 0,
        'z-index': 10
      });
      oUl.removeClass('submenu_open');

      bdFxHlMenu.fxMenuHeight.cancel();
      bdFxHlMenu.fxMenuHeightClose.cancel();
      bdFxHlMenu.fxMenuHeightClose.start('height', iNewHeight);


    }
  },
  item_link: function() {
  
    if (this.getProperty('href') && this.getProperty('href') != '' && this.getNext('ul.submenu')==null) {
      location.href = this.getProperty('href');
    }
  }
};

	
window.addEvent('domready', function(){
    
    bdFxHlMenu.initialize();
    
});