// HOVERINTENT
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity: 7,interval: 100,timeout: 0};cfg=$.extend(cfg,g?{over: f,out: g}: f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement : e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);

(function($) {
	$(".current-post-ancestor").addClass("current-menu-item");
	
	var navwidth = $("#main-navigation").width();
	var conwidth = $("#postwrapper").width() - 10;
	var newwidth = (conwidth - navwidth) / 2;
	$("#main-navigation").css({marginLeft: newwidth+"px"});
	
	var $el, leftPos, newWidth,
	$mainNav = $("ul.menu");

	$mainNav.append("<li id='magic-line'></li>");
	var $magicLine = $("#magic-line");
	 
	if ($("ul.menu .current-menu-item").length <= 0){
		$("ul.menu li:first").addClass("current-menu-item");
	};

	$magicLine
		.width($("ul.menu .current-menu-item").width())
		.css("left", $(".current-menu-item a").position().left)
		.data("origLeft", $magicLine.position().left)
		.data("origWidth", $magicLine.width());

	$("ul.menu li a").hover(function() {
		$el = $(this);
		if($el.attr('class')=='level1') {
			leftPos = $el.position().left;
		} else {
			leftPos = $el.parents('li').find('.level1').position().left;
		}
		newWidth = $el.parent().width();
		$magicLine.stop().animate({
			left: leftPos,
			width: newWidth
		});
	}, function() {
		$magicLine.stop().animate({
			left: $magicLine.data("origLeft"),
			width: $magicLine.data("origWidth")
		});
	});

	$(".home .postimage, .archive .postimage, .search .postimage").hover(function() {
		$(".readmore", this).fadeIn('fast');								   
		//$("img", this).stop().animate({opacity: 0.8}, 'fast');								   
	},
	function() {
		$(".readmore",this).fadeOut('fast');								   
		//$("img", this).stop().animate({opacity: 1}, 'fast');								   
	});
			
	$('.backtotop').click(function(){
		$('html, body').animate({scrollTop:0}, 'slow');
	});	
	
	$(".ind-post h2 a").hover(function() {
		$(this).stop().animate({opacity: 0.8}, 'slow');										   
	},
	function() {
		$(this).stop().animate({opacity: 1});										   
	});

	$("ul.menu li a").not("ul.menu li li a").each(function() {
		$(this).addClass('level1');
	});

	var config = {    
		over: makeTall, // function = onMouseOver callback (REQUIRED)    
		timeout: 500, // number = milliseconds delay before onMouseOut    
		out: makeShort // function = onMouseOut callback (REQUIRED)    
	};
	
	function makeTall() {
		var lm = $(this).position();										   	
		$('ul:first', this).slideDown().css({left: lm.left+'px'});
	}
	
	function makeShort() {
		$('ul:first', this).slideUp();										
	}
	
	$("ul.menu li").not("ul.menu li li").hoverIntent( config );
	
	var subconfig = {    
		over: submakeTall, // function = onMouseOver callback (REQUIRED)    
		timeout: 500, // number = milliseconds delay before onMouseOut    
		out: makeShort // function = onMouseOut callback (REQUIRED)    
	};
	
	function submakeTall() {
		$('ul:first', this).slideDown().css({left: '140px'});
	}
	
	$("ul.menu li li").hoverIntent( subconfig );
})(jQuery)