$(document).ready(function() {
	

	function megaHoverOver(){
		var offset = $('#content').offset().left - $(this).offset().left ;
		$(this).find(".sub").stop().css('left', offset+9+'px').fadeTo('fast', 0.88).show();
		(function($) { 
			jQuery.fn.calcUlWidth = function() {
				var uls = $(this).find('ul').length,
					borders = $(this).find(".border-right, .border-left").length;
				var totalWidth = $('.sub').width();
				
				return (totalWidth - uls*11 - borders) / uls;
			};
		})(jQuery);
		
		$(this).find('ul').css('width', $(this).calcUlWidth());
	}
	
	function megaHoverOut(){
	 //return false;
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() {
		  $(this).hide(); 
	  });
	}


	var config = {    
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 100, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 500, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};

	$("#nav li .sub").css({'opacity':'0'});
	$("#nav li").hoverIntent(config);
	$("#nav ul .items")
		.mouseover(function(){$(this).addClass('hover');})
		.mouseout(function(){$(this).removeClass('hover');});
});
$(document).ready(function() {

	if($('#to-top').length>0 && $(window).width()>1070) {
		var toTopButton = $('#to-top');
		if($(window).scrollTop() != "0") { 
			toTopButton.stop().animate({bottom: '10px'}, 150); 
		}
		$(window).scroll(
			function()
			{
				if($(window).scrollTop() == "0") { 
					toTopButton.stop().animate({bottom: '-100px'}, 100);
				} else {
					toTopButton.stop().animate({bottom: '10px'}, 150);
				}
			});
		toTopButton.click(function()
		{ 
			$("html, body").animate( { scrollTop: 0 }, "slow");
			return false; 
		});
	}
});
