(function($){

$(document).ready(function(){
	
	$(window).resize(showboxSizer);
	
	// Delay to be compliant with chrome and safari.
	setTimeout(function(){
		showboxSizer();
		$('#showbox img').presize();
	},50)
	
	// Start up slide show.
	if ( $('#showbox img').length > 1 ) {
		$('#showbox').pslide({
			delay: 		2500,
			duration: 	1000
		});
	}
	
	
	
	/**
	 * Hider Bar
	 */
	
	var hiderPosition = null;
	
	
	$('html.no-csstransitions #hider').hover(function(){
		
		hiderPosition = $(this).css('left');
		
		$(this).animate({
			left: '322px'
		},400);
		
	},function(){
		
		$(this).animate({
			left: hiderPosition
		},400);
		
	});
	
	
	
	/**
	 * Main Menu
	 */
	
	$('#mmenu a.selected').parents().addClass('selected');
	
	$('#mmenu li.selected ul').show();
	
	$('#mmenu>li>a').click(function(e){
		
		var $ul = $(this).parent().find('>ul');
		var $li = $ul.parent();
		
		if ( !$ul.length ) return true;
		
		if ( $ul.is(':visible') ) {
			$ul.slideUp();
			$li.removeClass('selected');
			
		} else {
			
			// Chiusura degli altri menu
			$('#mmenu').find('ul:visible').each(function(){
				$(this).slideUp().parent().removeClass('selected');
			});
			
			$ul.slideDown();
			$li.addClass('selected');
			
		}
		
		e.preventDefault();
	
	});
	
	/*
	//if ( Modernizr.touch ) {
	if ( true ) {
		
		$('window').bind('scroll',function(e){
			console.log(e);
		});
		
	}
	*/ 
	
	
	
	
	if ( $.prettyPhoto ) {
		
		$('.jcms-imageitem a').attr('rel','lightbox[ppgal]');
		
		$("a[rel^='lightbox']").prettyPhoto({
			theme: "dark_rounded",
			show_title: false,
			opacity: 0.2,
			slideshow:5000, 
			autoplay_slideshow:true,
			social_tools: ''
		});
		
	}
	
	
	if ( $.fancybox ) {
		
		$('.jcms-imageitem a').attr('rel','lightboxGallery');
		$(".jcms-imageitem a").fancybox();
		
	}
	
	
	/**
	 * iPad Fix
	 */
	//stick the footer at the bottom of the page if we're on an iPad/iPhone due to viewport/page bugs in mobile webkit
	if ( navigator.platform == 'iPad' ) {
		
		$("body").addClass('ios');
		
		myStageL = new iScroll('stagel');
		myStageR = new iScroll('stager');
		
		
	};

	
});



/**
 * It sets up container size to fit viewport.
 */
function showboxSizer() {
	
	$('#showbox').height( $(window).height() );
	$('#showbox').width( $(window).width() - $('#stagel').width() );
		
} // EndOf: "stageSizer()" ###


})(jQuery);
