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

$('[data-localmenu]')
	.addClass('active')

	.click(function () {
		var $this = $(this);
		var left = $this.offset().left;
		var top = $this.offset().top + $(this).height() + lmGap;

		var lmName = $this.attr('data-localmenu');
		var lm = $('#' + 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();


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


// 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;
}

});

