jQuery.extend({
	
	classes : {},
	
	classify : function(name, object, parent){
		function klass(){	
			this.init.apply(this, arguments);
		}
		if (parent && jQuery.classes[parent]) {
			function subklass(){};
			subklass.prototype = jQuery.classes[parent].prototype;
			klass.prototype = new subklass;
		}
		for (var y in object)
			klass.prototype[y] = object[y];
		if (!klass.prototype.init)
			klass.prototype.init = function(){};
		klass.prototype.constructor = klass;
		jQuery.classes[name] = klass;
		jQuery.fn[name] = function(){
			return this.each(function(i, element){
				element.classBehaviors = element.classBehaviors || {};
				if (element.classBehaviors[name]) return;
				function behavior(){};
				function klassBehavior(){
					this.element = element;
					this.init.apply(this, arguments);
					for (var attr in this) {
						if (attr.indexOf("on") === 1) {
							var handler = attr.substr(2);
							if (jQuery.fn[handler])
								jQuery(this.element)[handler](this[attr].bind(this));
						}
					}
				}
				behavior.prototype = jQuery.classes[name].prototype;
				klassBehavior.prototype = new behavior;
				element.classBehaviors[name] = new klassBehavior();
				return element.classBehaviors[name];
			});
		};
		return klass;
	}
});

jQuery.classify("dhtmlmenu", {

	last_menu    : null,
  show_time    : 375,
	hide_time    : 250,
	hover_class  : "Hover",
	
	init : function(){
		this.submenus = {};
		this.menus = jQuery(">li", this.element);
		this.menus.each(function(i, menu){
			jQuery(menu).hover(this.mouseOverHandler.bind(this, menu), this.mouseOutHandler.bind(this, menu));
		}.bind(this));
	},
	
	mouseOverHandler : function(menu, e){
		clearTimeout(this.hide_timeout);
		clearTimeout(this.show_timeout);
		if (this.last_menu)
			this.showMenu(menu);
		else
    	this.show_timeout = setTimeout(this.showMenu.bind(this, menu), this.show_time);
	},
	
	mouseOutHandler : function(menu, e){
		clearTimeout(this.hide_timeout);
		clearTimeout(this.show_timeout);
		this.hide_timeout = setTimeout(this.hideMenu.bind(this, menu), this.hide_time);
	},
	
	showMenu : function(menu){
		if (this.last_menu !== null)
			this.hideMenu(this.last_menu);
		jQuery(menu).addClass(this.hover_class);
		this.last_menu = menu;
	},
	
	hideMenu : function(menu){
		jQuery(menu).removeClass(this.hover_class);
		this.last_menu = null;
	}
	
});

// dom ready
$(function() { 
	
	// DHTML navigation, awaiting activation
	
	jQuery("#navigation").dhtmlmenu();

});
