/**
 * Some usefull tricks to the DOM
 * Just call "resetFn()" in your documentReady callback. 
 */

// Make resetFn global!
var resetFn;

// jQuery Compatibility Wrapper
;(function($) { resetFn = function() {
	
	
	/**
	 * Adds "first" and "last" classes to ULs items.
	 */
	var ulFix = function() {
		
		var $this = $(this);
		
		$this.find('>li:first').addClass('first');
		$this.find('>li:last').addClass('last');
		
	}; $('ul').each(ulFix);
	
	
	
	/**
	 * Adjust content dimensions to fit the viewport area.
	 */
	var $header 			= $('body>header');
	var $footer 			= $('body>footer');
	var $content 			= $('#page-content');
	var $contentWrap		= $('#page-content>.page-wrap');
	
	// Init content scrollbars and transform output into an array to manage updates into the adjustContentFunction.
	var articleMaxHeight 	= 250;
	var articleScrolls 		= [];
	
	$(".page-wrap article").each(function(){
		
		// Prevent small screen to use niceScroll
		if ( $(window).width() > 768 && $(this).outerHeight() > articleMaxHeight ) {
			
			$(this).css({
				maxHeight:	'none',
				height: 	articleMaxHeight,
				overflow: 	'hidden'
			});
			
			// Aggira il problema delle FAQ contraibili in relazione al plugin niceScroll()
			if ( $('body').attr('id') == 'faq' ) { $(this).html('<div style="height:1500px">'+$(this).html()+'</div>'); }
			
			articleScrolls.push( $(this).niceScroll() );
			
		}
	});
	
	var adjustContentDimensions = function() {
		
		if ( $(window).width() > 768 ) {
		
			var contentHeight = $(window).height() - $header.outerHeight() - $footer.outerHeight();
			
			$content.css({
				top: $header.outerHeight()
			});
			
			$contentWrap.css({
				height: contentHeight
			});
			
			// Update content scrollbars
			$.each(articleScrolls,function(){ this.updateScrollBar(); });
		
		} else {
			
			
			
		}
		
	}; adjustContentDimensions();
	
	
	
	/**
	 * Window Resize Events
	 */
	var onWindowResizeTimeout;
	$(window).resize(function(){
		
		// Real time operations:
		
		// Delayed Events:
		// Performance optimization for operations that not require real time application.
		clearTimeout(onWindowResizeTimeout);
		onWindowResizeTimeout = setTimeout(function(){
			
			adjustContentDimensions();
				
		},300);
		
	});
	
} 






$(document).ready(function(){
			
	// Apply DOM reset utilities defined in "reset.js"
	resetFn();
	
	
	
	/**
	 * Main menu behavior with submenu.
	 */
	if ( $(window).width() > 768 ) $('#main-menu ul, #lang-menu ul').superfish({speed:'fast'});
	
	
	
	
	
	/**
	 * Gallery di sfondo pagina.
	 * Slideshow automatico se c' pi di un'immagine.
	 */
	$('#bg').slideShowInit({
		delay: 		5000,
		duration: 	1500
	});
	
	if ( $('#bg img').length > 1 ) $('#bg').slideShowStart();
	
	
	
	
	
	/**
	 * Photogallery initialization
	 */
	if ( $(window).width() > 768 ) $('#gallery-list').galleryInit();
	
	
	/**
	 * FAQ Toggler
	 */
	var faqToggle = function() {
		
		var $this = $(this);
		var $next = $(this).next();
		
		if ( $next.is(':visible') ) {
			$next.slideUp();
			$this.removeClass('faq-title-open');
		} else {
			$next.slideDown();
			$this.addClass('faq-title-open');
		}
	
	// Bind event to content handlers.
	}; $('.jcms-box-collapsible-title').bind('click',faqToggle);
	
	
	/**
	 * Footer Logo Menu
	 */
	$('.crosslink').each(function(){
		
		var $this 		= $(this);
		var $handler 	= $this.find('.crosslink-handler');
		var $content 	= $handler.next();
		var hider 		= null;
		
		$handler.bind('mouseenter',function(){
			clearTimeout(hider);
			$content.fadeIn();
		});
		
		$content.bind('mouseenter',function(){
			clearTimeout(hider);
			$content.fadeIn();
		});
		
		$this.bind('mouseleave',function(){
			hider = setTimeout(function(){$content.fadeOut()},200);
		});
		
		
	});
	
});






})(jQuery);
