/*
jQuery menu

Example:
$(document).ready(function()
{
    $('#jmenu').jmenu({animation:'fade',duration:100});
});

(c) 2010 Sawanna Team (http://sawanna.org)
*/

var jmenu={
	effect: 'fade',           /* default animation effect */
	duration: 400,         /* default duration */
	objectPending:null,
	set: function (settings)
	{
		try
		{
			// jesli uzytkownik uzywa powiekszenia w przegladarce IE8 to wylacz animacje
			var zoom = window.screen.deviceXDPI;
			if (typeof(zoom) != "undefined" && zoom != 96) {
				this.effect='none';
			}
			else if (settings.animation == 'show') {
				this.effect='show';
			}
			else if (settings.animation == 'slide') {
				this.effect='slide';
			}
			else if (settings.animation == 'fade') {
				this.effect='fade';
			}
		} catch (e) {}

		try
		{
			this.duration=settings.duration;
		} catch (e) {}
	},
	fix_pos:function(elem)
	{
		if ($(elem).parent('ul').parent('li').length)
		{
			$(elem).children('ul').eq(0).css({
				marginTop:-$(elem).height(),
				marginLeft:$(elem).width()
				});
		} else
{
			$(elem).children('ul').eq(0).css({
				'top':$(elem).offset().top+$(elem).height()-$('#jmenu').offset().top,
				'left':$(elem).offset().left-$('#jmenu').offset().left
				});
		}
	},
	show:function(elem)
	{
		if (this.effect=='fade') {
			$(elem).children('ul').eq(0).stop(1,1).fadeIn(this.duration);
		}
		else if (this.effect=='slide') {
			$(elem).children('ul').eq(0).stop(1,1).slideDown(this.duration);
		}
		else if (this.effect=='show') {
			$(elem).children('ul').eq(0).stop(1,1).show(this.duration);
		}
		else if (this.effect=='none') {
			$(elem).children('ul').eq(0).stop(1,1).css("display","block");
		}
	},
	hide: function(elem)
	{
		//$(elem).children('ul').eq(0).stop(1,1).fadeOut(100);
		//$(elem).children('ul').eq(0).stop(1,1).hide();
		$("ul",elem).stop(1,1).css("display","none");
	},
	hideTimeout: function(elem)
	{
		// jesli myszka nie powrocila na obszar otwartego menu
		if (!$(elem)[0].mouseReturned) {
			this.hide(elem);
			// sprawdza czy nie przeszla na inny obszar
			if (this.objectPending != null && this.objectPending != elem) {
				this.fix_pos(this.objectPending);
				this.show(this.objectPending);
				this.objectPending = null;
			}
		}
	}
}

jQuery.fn.jmenu=function(settings)
{
	jmenu.set(settings);

	$(this).find('li').each(function()
	{
		$(this).hover(
			function()
			{
				$(this)[0].mouseReturned = true;
				if ($("#jmenu ul").length == $("#jmenu ul:hidden").length) {
					jmenu.fix_pos(this);
					jmenu.show(this);
				}
				jmenu.objectPending = this;
			},
			function()
			{
				$(this)[0].mouseReturned = false;
				var c = this;
				setTimeout(function(){
					jmenu.hideTimeout(c)
					},200);
			}
			);
	});
}
