$(document).ready(function() {
	
	// Header scroller
	for(i=1;i<=3;i++) 
		header_vertical_scroll("ul.slideshow.big > li:nth-child("+i+")", i*500);
	
	// Carousels
	carousel();
	
	// Fancybox
	$("a.img_thumbnail").fancybox();
	$("a.gmap").fancybox({
		type: 'image'
	});
	
	// Fixes
	$(".text a[rel*='attachment']").click(function() {
		return false;
	});	
	
	$('.slideshow img, .carousel img, ul.aziende img, .img_thumbnail img').css({opacity: 0});	
	THBF_preload();	
});

/** Vertical scroll */
function header_vertical_scroll(selector, delay)
{
	$(selector).cycle({
		fx: 	'scrollDown',
		speed:  1500,
		timeout:10000,
		delay:  delay
	});
	
	$('.slideshow img').one('load', function() {
		$(this).delay(250).animate({opacity:1}, 350);		
			}).each(function() {
				if(this.complete)
					$(this).trigger('load');
	});
}

/** Horizontal scroll */
function carousel()
{
	$('.carousel ul').cycle({
		fx:     'scrollHorz',
		speed:  '600',
		timeout:10000,
		next:   '#next',
		prev:   '#prev'		
	});
	
	if($(".carousel ul li").length == 1) {
		$("#next").hide();
		$("#prev").hide();
	}
}

/** img preload */
function THBF_preload() 
{
	$('.carousel img, ul.aziende img, .img_thumbnail img').one('load', function() {
		$(this).delay(250).animate({opacity:1}, 350);		
			}).each(function() {
				if(this.complete)
					$(this).trigger('load');
	});	
}	
