jQuery.fn.pulse = function( prop, speed, times, easing, callback ) {
    if ( isNaN(times) ) {
        callback = easing;
        easing = times;
        times = 1;
    }
    
    var optall = jQuery.speed(speed, easing, callback),
        queue = optall.queue !== false,
        largest = 0;
        
    for (var p in prop) {
        largest = Math.max(prop[p].length, largest);
    }
    
    optall.times = optall.times || times;
    
    return this[queue?'queue':'each'](function(){
        
        var counts = {},
            opt = jQuery.extend({}, optall),
            self = jQuery(this);
            
        pulse();
        
        function pulse() {
            
            var propsSingle = {},
                doAnimate = false;
            
            for (var p in prop) {
                
                // Make sure counter is setup for current prop
                counts[p] = counts[p] || {runs:0,cur:-1};
                
                // Set "cur" to reflect new position in pulse array
                if ( counts[p].cur < prop[p].length - 1 ) {
                    ++counts[p].cur;
                } else {
                    // Reset to beginning of pulse array
                    counts[p].cur = 0;
                    ++counts[p].runs;
                }
                
                if ( prop[p].length === largest ) {
                    doAnimate = opt.times > counts[p].runs;
                }
                
                propsSingle[p] = prop[p][counts[p].cur];
                
            }
            
            opt.complete = pulse;
            opt.queue = false;
            
            if (doAnimate) {
                self.animate(propsSingle, opt);
            } else {
                optall.complete.call(self[0]);
            }   
        }     
    });   
};

$(document).ready(function()
{
	/*PULSE*/
	$('#pi em').pulse({opacity: [0.5,1]}, {duration: 500, times: 999999});
	$("#didascalia p#1").css("display", "inline");
	/*SLIDER IMMAGINI*/
	var margine=0;
	var i=0;
	var limite=0;
	pid=1;
	$("#slider img").each(function(){i++;limite=436*(i-1);});
	$("#slider").css("width", limite+436);
	  $("#next a").click(function (event){
		if(margine > -limite){margine=margine-436;}
		else{margine=0;}
		$("#slider").animate({ marginLeft: margine}, 500 );
		if(pid < 6){pid++;}
		else{pid=1;}
		$("#didascalia p").css("display", "none");
		$("#didascalia p#" + pid).fadeIn(500);
		event.preventDefault();
		$(this).blur();
	  });
	  $("#prev a").click(function (event){
		if(margine == 0){margine=-limite;}
		else{margine=margine+436;}
		$("#slider").animate({ marginLeft: margine}, 500 );
		if(pid > 1){pid--;}
		else{pid=6;}
		$("#didascalia p").fadeOut(0),
		$("#didascalia p#" + pid).fadeIn(500);
		event.preventDefault();
		$(this).blur();
	  });
	  /*SCROLLING*/
	  $("a.top, a.topleft").click(function(event){
	      $('html, body').animate({scrollTop: 0}, 1000);
		  event.preventDefault();
	  });
	  $("#top-content li").click(function(event){
		  var classe=$(this).attr("class");
		  var scrolling=$("#"+ classe).offset().top;
		  $('html, body').animate({scrollTop: scrolling}, 1000);
		  return false;
	  });
	  /*SELECT FORM DEFISCALIZZAZIONE*/
	  $("#fasceeta").fadeOut(0);
	  $("#valori").slideUp(0);
	  $("#label").addClass("down");
	  $("#label").click(function(){
	      var classe=$(this).attr("class");
		  if(classe=="down")
		  {
		      $("#valori").slideDown(500);
			  $(this).addClass("up") 
			         .removeClass("down")
	      }
		  else
		  {
			  $("#valori").slideUp(250);
			  $(this).addClass("down")
			         .removeClass("up")
		  }
	  });
	  $("#valori li").click(function(){
		  var hiddenvalue=$(this).attr("id");
		  if(hiddenvalue==5){$("#fasceeta").fadeIn(500);}
		  else{$("#fasceeta").fadeOut(500);}
		  $("#tipo").val(hiddenvalue);
          var testo=$(this).html();
		  $("#valori").slideUp(250);
		  $("#label").html(testo)
		             .addClass("down")
					 .removeClass("up");
	  });
	  /*INFO*/
	  $("#infobox").fadeOut(0);
	  $("#infobutton").click(function(){
		  var classe=$(this).attr("class");
	      if(classe=="active")
		  {
		      $("#infobox").fadeOut(500);
			  $(this).removeClass("active").css("background","none");
	      }
		  else
		  {
			  $("#infobox").fadeIn(500);
			  $(this).addClass("active").css("background","url(immagini/infobutton.jpg)");;
		  }
	  });
	  /*ETA*/
	  
	  
});



