/**
 * Gasthof Hotel Post
 * main js
 *
 * (c) 2009 EZdesign.de
 */


var navTimeout = false;

$(document).ready(function() {
	
	/**
	 * navigation
	 */
	
	// mouse over for first level
	$('#mainnav > li:has(ul) > a').hover(function() {
		// timeout
		if (navTimeout != false) {
			window.clearTimeout(navTimeout);
			navTimeout = false;
		}
		// mark as hover
		$('#mainnav > li.hover').removeClass('hover');
		var hoverLi = $(this).parent();
		// close sublevels, that are not parent of current item
		$('#mainnav ul:visible').each(function() {
			if (!hoverLi.isChildOf($(this), 'li')) {
				$(this).hide();
			}
		});
		// move sub ul
		var subUl = hoverLi.find('ul');
		var offset = hoverLi.offset();
		subUl.css({
			left: (offset.left+parseInt((hoverLi.outerWidth(false)-150)/2))+'px',
			top: (offset.top+27)+'px'
		});
		
		// open sublevel
		hoverLi.addClass('hover');
	}, function() {
		// timeout
		window.clearTimeout(navTimeout);
		navTimeout = window.setTimeout(function() {
			navTimeout = false;
			$('#mainnav .hover').removeClass('hover');
		}, 400);
	}).parent().find('li').hover(function() {
		// timeout
		if (navTimeout != false) {
			window.clearTimeout(navTimeout);
			navTimeout = false;
		}
	}, function() {
		// timeout
		window.clearTimeout(navTimeout);
		navTimeout = window.setTimeout(function() {
			navTimeout = false;
			$('#mainnav .hover').removeClass('hover');
		}, 400);
	});
	
	$('#mainnav > li:not(:has(ul)) > a').mouseover(function() {
		window.clearTimeout(navTimeout);
		$('#mainnav .hover').removeClass('hover');
	});
	
	/**
	 * teasers
	 */
	
	$('div.teaserboxTeaser').click(function() {
		var link = $(this).find('a.teaserboxReadMore');
		if (link.length == 0) {
			link = $(this).find('a.dishOfTheDay');
		}
		window.location.href = link.attr('href');
	}).css('cursor', 'pointer');
});


/**
 * jQuery plugin
 * checks, whether a node is a child of another node
 */

jQuery.compareId  = 1;
jQuery.fn.compare = function(compareWith) {
	var el = $(this);
	if (!el.attr('id')) {
		el.attr('id', 'jQuery_compare_'+jQuery.compareId);
		jQuery.compareId++;     
	}
	if (el.attr('id') == compareWith.attr('id')) {
		return true;
	}
	return false;
}

jQuery.fn.isChildOf = function(parentNode, selector) {
	var selector = selector ? selector : '*';
	if (parentNode) {
		var children = $(selector, parentNode);
		var len      = children.length;
		for (var i = 0; i < len; i++) {
			if ($(children[i]).compare($(this))) {
				return true;
			}
		}
	}
	return false;
};