$(document).ready(function(){
	
	setBg(500);
	setPage();
	
	$('#toggle-highlight').toggle(
		function(){
			hw = 230 * $('#highlights div').length + 30;
			hw = 720;
			//alert(hw);
			$('#highlights').stop().animate({width:hw, 'padding-right':15}, 750, 'easeInOutExpo', function(){
				$('#toggle-highlight').attr('src', '../../site/img/close.png');
			});
		},
		function(){
			$('#highlights').stop().animate({width:0, 'padding-right':0}, 750, 'easeInOutExpo', function(){
				$('#toggle-highlight').attr('src', '../../site/img/open.png');
			});
		}
	);
	
	$('.slideshow .pager span:eq(0)').text('01');
	totalImg = ($('.gallery li').length > 9) ? $('.slideshow .gallery li').length : '0' + $('.slideshow .gallery li').length ;
	$('.slideshow .pager span:eq(1)').text(totalImg);
	
	$('.slideshow .gallery').cycle({
	    fx:     'fade', 
	    speed:  '500', 
	    timeout: 0, 
	    next:   '.next', 
	    prev:   '.prev',
	    nowrap:	true,
	    onPrevNextEvent: function(is,i,el) {
			//alert(is + "|" + i + "|" + el);
			total = $('.slideshow .gallery li').length;
			currImg = (i > 8) ? i+1 : '0' + (i+1) ;
			$('.slideshow .pager span:eq(0)').text(currImg);
			
	    	if (i > 0) {
	            $('.slideshow .prev').css({visibility:'visible'});
	        }
	        else {
	            $('.slideshow .prev').css({visibility:'hidden'}); 
	        }
    	    
	        if (++i < total ) {
	            $('.slideshow .next').css({visibility:'visible'});
	        }
	        else {
	            $('.slideshow .next').css({visibility:'hidden'}); 
	        }    	        
	    }
		        	    
	});

	total = $('.slideshow .gallery li').length;
	if(total < 2){
		$('.slideshow .next').css({visibility:'hidden'});
	}
	$('.slideshow .prev').css({visibility:'hidden'});
	
	/*
	$('#m-locanda').click(function(e){
		$(this).next().find('a:eq(0)').trigger('click');
		//alert($(this).next().find('a:eq(0)').text());
		return false;
	});
	*/
	
	$('#highlights div:last').css({'width':200});
	

	$('#m-contatti').fancybox({
		'cyclic':			false,
		'padding':			10,
		'overlayColor': 	'#000',
    	'width':			580,
    	'height':			480,
    	'type':				'iframe'
	});

	$('#credits').fancybox({
		'cyclic':			false,
		'padding':			0,
		'overlayColor': 	'#000',
    	'width':			250,
    	'height':			500,
    	'type':				'iframe'
	});
	
});



(function($,sr){
	 
	  // debouncing function from John Hann
	  // http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/
	  var debounce = function (func, threshold, execAsap) {
	      var timeout;
	 
	      return function debounced () {
	          var obj = this, args = arguments;
	          function delayed () {
	              if (!execAsap)
	                  func.apply(obj, args);
	              timeout = null; 
	          };
	 
	          if (timeout)
	              clearTimeout(timeout);
	          else if (execAsap)
	              func.apply(obj, args);
	 
	          timeout = setTimeout(delayed, threshold || 100); 
	      };
	  }
		// smartresize 
		jQuery.fn[sr] = function(fn){  return fn ? this.bind('resize', debounce(fn)) : this.trigger(sr); };
	 
	})(jQuery,'smartresize');

$(window).smartresize(function(){
	setBg(500);
	setPage();
});

function setBg(ms){
	//alert($('html').width() + 'x' + $('html').height() );
        
    var ratio = 1280/950;
    if( $('html').width() / $('html').height() > ratio){
        $('#bg img').css({height: 'auto', width: '100%'});
        /*
        centerH = ($('#bg img').height() - $('html').height()) / 2;
        $('#bg img').animate({top: '-' + centerH + 'px'}, ms, 'easeOutQuart');
        */
        //alert(centerH + "|" + $('#bg img').height() + "|" + $('html').height());
    }else{
        $('#bg img').css({width: 'auto', height: '100%' });
        /*
        centerW = $('#bg img').width() - $('html').width() / 2;
        $('#bg img').animate({left: '-' + centerw + 'px'}, ms, 'easeOutQuart');
        */
        
    }

		//setBodyScroll();
}

function setPage(){
	$('#page').css({'min-height': $('body').height() - 32 - 64 - 48 });
	if( ($('#page').height() + 32 + 64 +48) < $('html').height()){
		$('#page').css({position:'fixed', bottom: 0});
	}else{
		$('#page').css({position:'relative'});
	}
	
}
