//Image Preloader

$(function() {
	$('#post .text img').each( function() {
		
		var w = $(this).width();
		var h = $(this).height()+4;
		$(this).parent().append('<div class="image-loader" style="height:'+h+'px;width:'+w+'px" ><img src="/media/images/icon-loader-2.gif" width="32" height="32" style="padding-top:'+((h-32)/2)+'px; padding-left:'+((w-32)/2)+'px;" /></div>');
		//$(this).wrap('<span class="image-box"></span>').hide();
		$(this).hide();
	});
});

$(window).bind('load', function() {
     var i = 1;
     var imgs = $('img').length;
     var int = setInterval(function() {
     //console.log(i); check to make sure interval properly stops

     if(i >= imgs) clearInterval(int);
    
    
      $('div.image-loader').fadeOut(200, function() {
      
       $('#post .text img:hidden').eq(0).fadeIn(500);
       $(this).remove();
       
      });
     i++;
     }, 500);
});




$(document).ready(function() {

	
	checkFooterPosition();
	if($('#grid .style-1').length)
	{
		$('#grid .style-1').simpleCaptionOverlay();
	}
	
	if ($('.slashc-img-zoom-pan').length)
	{
		$('.slashc-img-zoom-pan').slashcImgZoomPan();
	}
	
	$('#grid').masonry({   columnWidth: 166, 
	  itemSelector: '.box'  });
	
	
	  
	var header = $('#header-ext');

	var topBar = $('#top-bar-ext');
	var headerHeight = header.height();
	
	var heightCache;
	
	$(window).resize(function() {
		checkFooterPosition();			
	 });		
	  
	$(window).scroll(function() {
	
		checkFooterPosition();
		
		var scrollDistance = $(this).scrollTop();
		
		//topBar.html(topBar.offset().top);
			
		if (headerHeight < scrollDistance) {
		    topBar.addClass('fixed');


		    //topBar.fadeIn(400);
		   // topBar.append('height='+headerHeight)
	
		}
		
		if (headerHeight > scrollDistance) 
		{
			//topBar.append('now');
			if(topBar.hasClass('fixed'))
			{
				topBar.removeClass('fixed');
			    
		    }
		    
		}
		
	});
	/*
	
	function checkFooterPosition()
	{
		
		$('#footer-ext').css('bottom','0');
		
	}	
	*/
function checkFooterPosition()
{
	
	$('#footer-ext').css('top','0');
	var footerBottom = $('#footer-ext').offset().top + $('#footer-ext').height();
	var footerOffset = $(window).height()-footerBottom-20;
	if(footerOffset>0)
	{
		$('#footer-ext').css('top',footerOffset);
		//$('#footer').prepend('mt='+footerOffset);
	}			
}		

$(window).resize(function() {
	//$('body').prepend('<p>resized</p>');
	checkFooterPosition();			
 });		
	

	function megaHoverOver(){
		$(this).find(".sub").stop().fadeTo('fast', 1).show();
			
		//Calculate width of all ul's
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function() {					
					rowWidth += $(this).width(); 
				});	
			};
		})(jQuery); 
		
		if ( $(this).find(".row").length > 0 ) { //If row exists...
			var biggestRow = 0;	
			//Calculate each row
			$(this).find(".row").each(function() {							   
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$(this).find(".sub").css({'width' :biggestRow});
			$(this).find(".row:last").css({'margin':'0'});
			
		} else { //If row does not exist...
			
			$(this).calcSubWidth();
			//Set Width
			$(this).find(".sub").css({'width' : rowWidth});
			
		}
	}
	
	function showMenuOverlay(){
		$("#menu-overlay").addClass('visible').stop().fadeTo('fast', 1).show();
	}
	

	function doNothing(){ 

	}
	
	
	function hideMenuOverlay(){ 
//		$("#header").html('TEST');
		
	  $("#menu-overlay").stop().fadeTo('fast', 0, function() {
		  $(this).hide(); 
	  });
	}


	
	var config2 = {    
		 sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 30, // number = milliseconds for onMouseOver polling interval    
		 over: showMenuOverlay, // function = onMouseOver callback (REQUIRED)    
		 timeout: 100, // number = milliseconds delay before onMouseOut    
		 out: doNothing // function = onMouseOut callback (REQUIRED)    
	};
		
	var configMenuOverlay = {    
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 30, // number = milliseconds for onMouseOver polling interval    
		 over: doNothing, // function = onMouseOver callback (REQUIRED)    
		 timeout: 100, // number = milliseconds delay before onMouseOut    
		 out: hideMenuOverlay // function = onMouseOut callback (REQUIRED)    
	};
	
	var configHeaderExt = {    
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 30, // number = milliseconds for onMouseOver polling interval    
		 over: hideMenuOverlay, // function = onMouseOver callback (REQUIRED)    
		 timeout: 100, // number = milliseconds delay before onMouseOut    
		 out: doNothing // function = onMouseOut callback (REQUIRED)    
	};	
	
	$("ul#topnav li .sub").css({'opacity':'0'});

	$("#header-ext").hoverIntent(configHeaderExt);
	$("ul#topnav li .menu").hoverIntent(config2);

	$("#menu-overlay").hoverIntent(configMenuOverlay);




// Menu Overlay Sub-List Toggle
$(function() {
	

	$('ul#menu-top-menu-left li a').click(function(event) {
		if($(this).parent().find("ul").length)
		{
				event.preventDefault();
				if(!($(this).parent().hasClass('expanded')))
				{
				  $("ul#menu-top-menu-left ul").toggle(false);
				$("ul#menu-top-menu-left li").removeClass('expanded');
				$(this).parent().addClass('expanded');
				  $(this).parent().find("ul").toggle('fast');
			  }
			  else
			  {
				  $(this).parent().find("ul").toggle('fast');
				$("ul#menu-top-menu-left li").removeClass('expanded');			  
			  }
			  return false;
		}
	});
	
		$('ul#menu-top-menu-right li').hover(function(event) {
			if($(this).find("ul").length)
			{
				event.preventDefault();
					
				$(this).find("ul").show();
	
				return false;
			}
	},function(event){
		if($(this).find("ul").length)
		{
			$(this).find("ul").hide();
		}
	});
	
	
	$("ul#menu-top-menu-right li a[title=disattivo]").click(function(event){event.preventDefault(); return false;});

});





});

// Google+

  (function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
  })();
