

$(function(){

   if ($('#menu').length > 0){
      $('#menu').find('li').hover(
         function(){$(this).find('ul').fadeIn();},
         function(){$(this).find('ul').fadeOut();}
      );
   }

   $('.datePicker, .datepicker').each(function(){
      var dateOptions = $(this).attr('alt') ? eval('('+$(this).attr('alt')+')') : {};
      $(this).datepicker(dateOptions);
   });

   $('.thickbox, .shutterset').fancybox({titlePosition:'over'});
   $('.video_thickbox').fancybox({titleShow:false, type:'iframe', width:400, height:300});
   $('.thickbox_inline').fancybox({autoDimensions:false, titleShow:false, width:500, height:400});

   if ($('#midimg img').length > 1){
      setInterval( "slideSwitch()", 5000 );
   }

   // Ngggallery ajax
   nggGallery();


});


function nggGallery(){
   $('.ngg-imagebrowser').not('.fat').each(function(){
      var $imagebr = $(this);
      var id = $imagebr.attr('id');

      $imagebr.addClass('fat').find('.ngg-imagebrowser-nav a').click(function(){
         $imagebr.find('.pic img').replaceWith('caricamento...');
         var url = new String($(this).attr('href'));
         $.ajax({
            url  : '/nggajax.php',
            type : 'POST',
            data : {url:url, id:id},
            success: function(data){
               //console.debug(data);
               $imagebr.replaceWith(data);
               $('.ngg-imagebrowser').not('.fat').find('.pic a').fancybox({titlePosition:'over'});
               nggGallery()
            },
            dataType:'html'
         });




         return false;

      });

   });
}

function slideSwitch() {
    var $active = $('#midimg img.active');

    if ( $active.length == 0 ) $active = $('#midimg img:last');

    var $next =  $active.next().length ? $active.next()
        : $('#midimg img:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}
