(function($) {
	$.fn.treeMenu = function(o) {
		options = {
			'onclick': function() {},
                        'callback': function() {}
		};
		
		if (o) $.extend(options, o);
		
		this.each(function() {		
			var m = $(this);
			m.find('.tm_opt').hover(
				function() {
					clearTimeout(m.data('menuTimer'));
					m.removeData('menuTimer');
					$(this).siblings().find('.tm_sub').hide();					
					$(this).addClass('tm_opthvr');
					$(this).find('.tm_sub:first').show();					
				},
				function() {					
					$(this).removeClass('tm_opthvr');
					if (m.find('.tm_opthvr').size() == 0) {
						var timer = setTimeout(function() {
							m.find('.tm_sub').hide();
						}, 1000);
						m.data('menuTimer', timer);
					}
				}
			);
			//m.find('.tm_opt').bind('mousedown', options['onclick']);
			m.find('.tm_opt').bind('__click', options['onclick']);
                        m.find('.tm_opt').bind('__callback', options['callback']);
			m.find('.tm_opt').bind('click', function(e) {
				e.stopPropagation();
				$(this).trigger('__click');					
                                $(this).trigger('__callback');
				$('.tm_opt').removeClass('tm_opthvr');
				$('.tm_sub').hide();
			});
		});
		$(document).click(function() { 
			$('.tm_sub').hide();
		});
		return this;
	}
})(jQuery);