/**
 * Muskelkater
 * main jQuery manipulation 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();
			}
		});
		// 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);
	});
	
	
	/**
	 * newsteaser
	 */
	
	$('#left .newsteaser').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	}).click(function() {
		window.location.href = $(this).find('a.readmore').attr('href');
	});
});


/**
 * 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;
};