/*

jObtyMenu - jQuery plugin, version 1.0b

Licensed under MIT License.
Copyright (c) 2009 Andrey Dotsenko
All rights reserved.

Depends on jquery.timers.js.

*/

$(function() {
	$(document).click(function() {
		$('[obtycontext_visible=1]').each(function() {
			$(this).hide($(this).data('options').hideAni);
		});
	});
});
(function($)
{
	jQuery.fn.obtymenu=function(opt)
	{
		var options=jQuery.extend({
			mainmenuClass:'jobtymenu',
			contextmenuClass:'jobtymenu_context',
			showAni:'slideDown',
			hideAni:'slideUp'
		},opt);
		this.data('options',options);
		jQuery.fn.obtyPopup=function(o)
		{
			var p=$(o).offset();
			this.oneTime(1,function() {
				$(this).css('top',(p.top+$(o).height())+'px').css('left',p.left+'px').attr('obtycontext_visible',1).stop(1,true).show(options.showAni);
			});
			return this;
		}
	
		$(this).addClass(options.mainmenuClass)
		.find('li').hover(function() {
			if ($(this).data('child_ul'))
			{
				$(this).parent().attr('submenu_opened',1).find('li').not(this).each(function() {if ($(this).data('child_ul')) {$(this).data('child_ul').stopTime().stop(1,true).hide(options.hideAni);}});
				var p=$(this).offset(),h=$(this).height(),w=$(this).width();
				var l0=0;
				l0=$(this).parent().find('li:first').offset().left;
				$(this).parent().find('>li').each(function() {if ($(this).offset().left>(l0+5)) {l0=true;}});
				if (l0===true) {w=0;} else {h=0;}
				$(this).data('child_ul').stopTime().css('top',(p.top+h)+'px').css('left',(p.left+w)+'px').stop(1,true).show(options.showAni);
			}
		},function() {
			if ($(this).data('child_ul'))
			{
				$(this).data('child_ul').oneTime(100,function() {
					if (!$(this).is('[is_hovered=1]'))
					{
						$(this).oneTime(100,function() {
							$(this).stop(1,true).hide(options.hideAni);
						});
					}
				});
			}
		});
		$(this).find('ul').hide();
		$(this).find('*').add(this).data('mainmenu',$(this)).hover(function() {$(this).attr('is_hovered',1);$(this).data('mainmenu').data('menu_active',1);},function() {$(this).attr('is_hovered',0);$(this).data('mainmenu').data('menu_active',0)});
		$(this).find('li:has(ul)').each(function() {$(this).data('child_ul',$(this).find('ul:first').data('parent_ul',$(this).parent()));});
		$(this).find('ul').addClass(options.contextmenuClass).appendTo($('body')).hover(function() {},function() {
			$(this).oneTime(100,function() {
//			alert($(this).attr('submenu_opened'));
				if ((!$(this).is('[is_hovered=1]'))&&($(this).attr('submenu_opened')!=1))
				{
					$(this).stop(1,true).hide(options.hideAni);
					if ($(this).data('parent_ul')) {$(this).data('parent_ul').attr('submenu_opened',0).trigger('mouseleave');}
				}
			});
		});
		return this;
	}
})(jQuery);

