// $Id: x_menu.js,v 1.1.1.1 2007/01/17 17:07:34 andy Exp $

xMenu = function() {
	this.init = function( id) {
		var main_obj = this
		var menu = document.getElementById(id)

		var parent_tag = this.parent_tag || "ul"
		var child_tag = this.child_tag || "li"

		var menu_max_width = this.menu_max_width || 500
		var show_delay		= this.show_delay || 100
		var hide_delay		= this.hide_delay || 500
		var class_prefix	= this.class_prefix || "l"
		var class_over	= this.class_over || "over"
		var curren_opened_on_level = []
		this.registerEvent = function( obj, event_, func) {
			document.all?obj.attachEvent( "on".concat( event_), func):obj.addEventListener( event_, func, false);
		}
		this.registerEvent( menu, "mouseover",
			function( e) {
				var obj = (e && e.target) || (event && event.srcElement)
				do {
					if ( obj.tagName.toLowerCase() == child_tag ) main_obj.show_child( obj)
				} while( !(menu==obj) && (obj = obj.parentNode) )
			}
		)
		this.registerEvent( menu, "mouseout",
			function( e) {
				var obj = (e && e.target) || (event && event.srcElement)
				do {
					if (  obj.tagName.toLowerCase() == child_tag  ) {main_obj.hide_child( obj)}
				} while( !(menu==obj) && (obj = obj.parentNode) )
			}
		)
		this.show_child = function( obj) {
			if ( !obj.level ) {
				obj.level = 0;
				var o = obj;
				while( (o = o.parentNode) && menu.id != o.id ) {
					if ( o.tagName.toLowerCase() == parent_tag.toLowerCase() ) {
						obj.level++;
					}
				}
			}
			var ul;
			if ( ul = obj.getElementsByTagName( parent_tag)[0] ) {
				if ( obj.offsetLeft-(-ul.offsetWidth) > menu_max_width ) {
					ul.style.left = menu_max_width - ul.offsetWidth - obj.offsetLeft
				} else {
					ul.style.left = 0
				}
			}
			clearTimeout( obj.hide_timeout_id);
			clearTimeout( obj.show_timeout_id);
			if ( curren_opened_on_level[obj.level] != obj ) {
				if ( curren_opened_on_level[obj.level] ) {
					clearTimeout( curren_opened_on_level[obj.level].show_timeout_id);
					clearTimeout( curren_opened_on_level[obj.level].hide_timeout_id);
					curren_opened_on_level[obj.level].className="";
				}
				curren_opened_on_level[obj.level] = obj;
			}
			obj.show_timeout_id = setTimeout(function(){obj.className=class_over;},show_delay);
		}
		this.hide_child = function( obj) {
			clearTimeout( obj.show_timeout_id);
			obj.hide_timeout_id = setTimeout( function(){obj.className="";}, hide_delay);
		}
	}
}
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
