var lmGap = 6;  //px
var lmDelay = 300;  //ms

jQuery.fn.bindWithDelay = function (event, action, target) {
	var lmTarget;
	if (target) lmTarget = target;

	var prevent;
	if (event == 'mouseenter') prevent = 'mouseleave';
	if (event == 'mouseleave') prevent = 'mouseenter';

	this
		.unbind(event)
		.bind(event, function () {
			var lm = lmTarget || $(this);
			lm[0].lmTimer = setTimeout(function () {action(lm);}, lmDelay);
			return true;
		})
		.unbind(prevent)
		.bind(prevent, function () {
			var lm = lmTarget || $(this);
			clearTimeout(lm[0].lmTimer);
			return true;
		});
	return this;
};

function endLocalMenu(lm) {
		lm.fadeOut('def', function () {$(this).remove();});
}

function showSub(li) {
	li.bindWithDelay('mouseleave', hideSub);

	var span = li.children('span');
	span.addClass('subhigh');

	var RX = span.offset().left;
	var ry = span.position().top;
	var rw = span.outerWidth();
	var sw = li.children('ul').width();

	var sx = RX + rw + lmGap + sw < $(window).width() ? rw + lmGap : - lmGap - sw;

	li.children('ul')
		.css({'position': 'absolute', 'left': sx, 'top': ry})
		.fadeIn('def');
}

function hideSub(li) {
	li.bindWithDelay('mouseenter', showSub);
	li.children('span').removeClass('subhigh');
	li.children('ul').fadeOut('def');
}




$(document).ready(function () {
// noscript utani html commentek felebresztese

$('noscript').each(function () {
	var node = this.nextSibling;
	while (node != null) {
		if (node.nodeType == 8) {
			node.parentNode.removeChild(node);
			$(this).after($(node.nodeValue));
			break;
		}
		if (node.nodeType != 3) break;
		node = node.nextSibling;
	}
});

// helyi menuk

var term = $('<div/>');
term.load('term.mnu');

$('a[href="term.php"]').addClass('lm!term');

$('[class*="lm!"]')
	.addClass('active')

	.click(function () {
		var left, top;
		if ($(this).parent().is('#topTerm')) {
			left = $(this).offset().left;
			top = $(this).offset().top + $(this).height() + 16;

		} else {
			left = $(this).offset().left;
			top = $(this).offset().top + $(this).height() + lmGap;
		}

		var lmName = this.className.match(/lm!(\w+)\b/)[1];
		var lm;
		if (lmName.substring(0,4) == 'term') {
			lm = term.find('#' + lmName);
		} else {
			lm = $('#cmenu #' + lmName);
		}
		lm = lm.clone();
		lm
			.hide()
			.appendTo('body')
			.addClass('submenu')
			.css({'position': 'absolute', 'left': left, 'top': top})
			.fadeIn('def');

		$(this).bindWithDelay('mouseleave', endLocalMenu, lm);
		lm.bindWithDelay('mouseleave', endLocalMenu);

		lm.find('ul').hide()
			.prev().addClass('active').end()
			.parent().bindWithDelay('mouseenter', showSub);

		return false;
	});



// kategoria menu

$('#cmenu span')
	.addClass('active')
	.addClass('subclose')
	.click(function() {
		$(this).toggleClass('subclose')
		.next().slideToggle();
	})
	.next().hide();

window.location.pathname.match(/\/([^\/]+)$/);
$('#cmenu a[href="' + RegExp.$1 + '"]')
	.addClass('match')
	.parents('ul').show()
		.prev('span').removeClass('subclose');


// kat.menu fuggoleges kovetes, korrigalt abszolut pozicio

if($('#cmenu').length) {

	var cmenuLimitY = 4;
	var cmenuTop = parseInt($('#cmenu').css('top'));

	$(window).scroll(function () {
		var trunkY = $('#trunk').offset().top;
		var scrollY = $(window).scrollTop();
		if (trunkY + cmenuTop - scrollY < cmenuLimitY) {
			$('#cmenu').css('top', scrollY - trunkY + cmenuLimitY);
		} else {
			$('#cmenu').css('top', cmenuTop);
		}
	});
}


// tablazatsorok szinezese

$('table:not(.plain)').each(function () {
	var s = false;
	var r = 1;
	$(this).find('tr').each(function () {
		if (!$(this).hasClass('plain')) {
			r = r - 1;
			if (r == 0) s = !s;
			if (s) $(this).addClass('dark');

			var rspan = maxspan($(this));
			if (rspan > r) r = rspan;
		} else {
			s = false;
			r = 1;
		}
	});
});


function maxspan(row) {
	var max = 1;
	var span;
	row.children().each(function () {
		span = $(this).attr('rowspan');
		if (span > max) max = span;
	});
	return max;
}


// fuggoleges kitoltes

$(window).resize(function () {
	var dy = $(window).height() - $('body').innerHeight();
	$('#content').css('min-height', $('#content').height() + dy);
});

$(window).trigger('resize');

});


