$(document).ready(function() {
  
  // Submenu
  $('.hover').click( function(event) {
    event.preventDefault();
    $('.submenu_active').fadeOut(250);
    $('.submenu_active').removeClass('submenu_active');
    var $menu = $( $(this).attr('href') );
    $menu.fadeIn(500);
    $menu.addClass('submenu_active');
  });
  
  $('.submenu_active').live('mouseleave', function(event) {
    $('.submenu_active').delay(900).fadeOut(250);
  });
  
  $('.submenu_active').live('mouseenter', function(event) {
    $('.submenu_active').clearQueue();
  });
  
  
  // Scroll Top
  $('a[href=#top]').click( function(event) {
    event.preventDefault();
    $('html, body').animate({scrollTop: 0}, 800);
  });
  
  
  // Placeholder
  $('input:text').placeholder();
  
  
  // Newsletter
  $('#iscriviti a').click( function(e) {
    e.preventDefault();
    $('#iscriviti .hidden').slideToggle(250);
  });
  
	$('.newsletter_action').click( function() {
	  var $mail = $( $(this).attr('rel') );
	  var action = '';
	  var actionName = '';
	  
	  if ($(this).hasClass('subscribe')) {
	    action = 'http://newsletter.basili.co/external/subscribe.cfm?jsoncallback=?';
	    actionName = 'Iscrizione';
	  };
	  
	  if ($(this).hasClass('unsubscribe')) {
	    action = 'http://newsletter.basili.co/external/unsubscribe.cfm?jsoncallback=?';
	    actionName = 'Cancellazione';
	  };
	  
    $.getJSON( action, {
      
      email : $mail.val(),
      user : 'user_247',
      list : '1',
      key : '4EE4DD21-D24F-D79C-AD7D9E04CF389E94',
      confirm : true
      
    }, function(result) {
      if ( result.errorcode > 0 ) {
        alert( result.message );
      } else {
        alert( actionName+' avvenuta con successo.' );
        $mail.val('').attr('disabled','disabled');
      }
    });
	});
	

});


;(function($) {
/**
 * Run slideshow with image download on demand
 *
 * @example jQuery("img").slideshow();
 *
 * @lastmod 2011-01-13 19.21
 *
 * @name slideshow
 * @type jQuery
 * @param Object	settings
 *								
 * @return jQuery
 * @author Dharma Ferrari (http://www.dharmaferrari.com)
 */
$.fn.slideshow = function(imageList, settings)
{
	settings = $.extend({}, $.fn.slideshow.defaults, settings);
	
	if(settings.shuffle) {
	  imageList.sort(function() {return 0.5 - Math.random()});
	}
	
  return this.each( function()
  {
    var $this = $(this);
	  var parent = $this.parent();
    var imageListIndex = 0;
    
    $this.hide();
    
    /**
     * Bind image load fired on change of src attribute
     *
     */
    $this.load(function () {
      $this.fadeIn(settings.fadeDuration * 1000);
      parent.toggleClass(settings.loadingClass);
      
      // swap only if there are more than one pictures
      if(imageList.length > 1) {
        setTimeout(function() {
          if(imageListIndex >= imageList.length-1) {imageListIndex = 0;}
          else {imageListIndex++;}
      
          $this.fadeOut(settings.fadeDuration * 1000, function() {
            parent.toggleClass(settings.loadingClass);
            $this.attr('src', imageList[imageListIndex]);
          });
        },
        settings.imageStillDuration * 1000);
      }
      
    }).error(function () {
      parent.removeClass(settings.loadingClass);
      parent.append('<div id="jssError">' + settings.error404Msg + '</div>');
    }).attr('src', imageList[imageListIndex]);
    
	});
};

$.fn.slideshow.defaults = {
  fadeDuration : .5,
  imageStillDuration : 5,
  loadingClass : 'ss_loading',
  error404Msg : '404: Errore nel caricamento',
  shuffle : false
};

})(jQuery);

