var Class = {
  create: function() {
    return function() {
      this.initialize.apply(this, arguments);
    }
  }
}

Object.extend = function(destination, source) {
  for (property in source) {
    destination[property] = source[property];
  }
  return destination;
}

Function.prototype.bindAsEventListener = function(object) {
  var __method = this;
  return function(event) {
    return __method.call(object, event || window.event);
  }
}


if (!window.Event) {
  var Event = new Object();
}

Object.extend(Event, {
  KEY_BACKSPACE: 8,
  KEY_TAB:       9,
  KEY_RETURN:   13,
  KEY_ESC:      27,
  KEY_LEFT:     37,
  KEY_UP:       38,
  KEY_RIGHT:    39,
  KEY_DOWN:     40,
  KEY_DELETE:   46,

  element: function(event) {
    return event.target || event.srcElement;
  },

  isLeftClick: function(event) {
    return (((event.which) && (event.which == 1)) ||
            ((event.button) && (event.button == 1)));
  },

  pointerX: function(event) {
    return event.pageX || (event.clientX +
      (document.documentElement.scrollLeft || document.body.scrollLeft));
  },

  pointerY: function(event) {
    return event.pageY || (event.clientY +
      (document.documentElement.scrollTop || document.body.scrollTop));
  },

  stop: function(event) {
    if (event.preventDefault) {
      event.preventDefault();
      event.stopPropagation();
    } else {
      event.returnValue = false;
      event.cancelBubble = true;
    }
  },

  observers: false,

  _observeAndCache: function(element, name, observer, useCapture) {
    if (!this.observers) this.observers = [];
    if (element.addEventListener) {
      this.observers.push([element, name, observer, useCapture]);
      element.addEventListener(name, observer, useCapture);
    } else if (element.attachEvent) {
      this.observers.push([element, name, observer, useCapture]);
      element.attachEvent('on' + name, observer);
    }
  },

  unloadCache: function() {
    if (!Event.observers) return;
    for (var i = 0; i < Event.observers.length; i++) {
      Event.stopObserving.apply(this, Event.observers[i]);
      Event.observers[i][0] = null;
    }
    Event.observers = false;
  },

  observe: function(element, name, observer, useCapture) {
    useCapture = useCapture || false;

    if (name == 'keypress' &&
        (navigator.appVersion.match(/Konqueror|Safari|KHTML/)
        || element.attachEvent))
      name = 'keydown';

    this._observeAndCache(element, name, observer, useCapture);
  },

  stopObserving: function(element, name, observer, useCapture) {
    var element = $(element);
    useCapture = useCapture || false;

    if (name == 'keypress' &&
        (navigator.appVersion.match(/Konqueror|Safari|KHTML/)
        || element.detachEvent))
      name = 'keydown';

    if (element.removeEventListener) {
      element.removeEventListener(name, observer, useCapture);
    } else if (element.detachEvent) {
      element.detachEvent('on' + name, observer);
    }
  }
});
Event.observe(window, 'unload', Event.unloadCache, false);


var Menu = Class.create();
Menu.prototype = {
  initialize: function(ulMenu) {
    this.parent = null;
    this.child = [];
    this.ulMenu = ulMenu;
  },
  setParent: function(parent) {
    this.parent = parent;
    parent.child.push(this);
  },
  show: function() {
    this.ulMenu.style.display = 'block';
    this.ulMenu.style.visibility = 'visible';
  },
  hide: function() {
    for ( var i = 0; i < this.child.length; i++ ) {
      this.child[i].hide();  
    }
    if ( this.parent != null ) {
	    this.ulMenu.style.display = 'none';
      this.ulMenu.style.visibility = 'hidden';
    }
  }
}

var MenuBehaviour = Class.create();
MenuBehaviour.prototype = {
  initialize: function(menuId) {
    var ulMenu = document.getElementById(menuId);
    if (ulMenu) {
      this.rootMenu = new Menu(ulMenu);
      this._injectBehaviour(this.rootMenu);
    }
  },
  _injectBehaviour: function(menu) {
    var elUl = menu.ulMenu;
    for ( var i=0; i<elUl.childNodes.length; i++ ) {
      if ( elUl.childNodes[i].nodeType == 1 && elUl.childNodes[i].nodeName == 'LI' ) {

	      var li = elUl.childNodes[i];
        Event.observe(li,'mouseout',menu.hide.bindAsEventListener(menu));

        var hasUl = false;
        for ( var j=0; j<li.childNodes.length; j++ ) {
          if ( li.childNodes[j].nodeType == 1 && li.childNodes[j].nodeName == 'UL' ) {
            var sousMenu = new Menu(li.childNodes[j]);
            sousMenu.setParent(menu);
            this._injectBehaviour(sousMenu);
            hasUl = true;
          }
        }
        if (hasUl) {
          Event.observe(li,'mouseover',sousMenu.show.bindAsEventListener(sousMenu));
        }
      }
    }
  }
}

// ONLOAD EVENT ***********************
$(document).ready(function(){
	var nav2 = new MenuBehaviour('nav');
	//nav.init();
});



