// rotació de misstages de promoció
$(document).ready(function() {
  // Using each as an 'if' and containing stuff inside a private namespace.
  $('.testimonis-wrapper').each(function() {
    var $this = $(this);

    var totalHeight = $this.height();
    
    // Set up the rotator.
    var currentHeadline = 0, oldHeadline = 0;
    var hiddenPosition = totalHeight   + 10;
    $('div.testimoni:eq(' + currentHeadline + ')').css('top','0');
    var headlineCount = $('div.testimoni').length;
    var headlineTimeout;
    var rotateInProgress = false;

    // Rotator function.
    var headlineRotate = function() {
     if (!rotateInProgress) {
        rotateInProgress = true;
        headlineTimeout = false;

        currentHeadline = (oldHeadline + 1) % headlineCount;
        $('div.testimoni:eq(' + oldHeadline + ')')
        .animate({top: -hiddenPosition}, 'slow', function() {
          $(this).css('top',hiddenPosition);
        });
        $('div.testimoni:eq(' + currentHeadline + ')')
        .animate({top: 0},'slow', function() {
          rotateInProgress = false;
          if (!headlineTimeout) {
            headlineTimeout = setTimeout(headlineRotate, 15000);
          }
        });
        oldHeadline = currentHeadline;
      }
    };
    headlineTimeout = setTimeout(headlineRotate, 15000);
  }); 
  var newsRotate = function() {
  	$('div.new').css('left', '630px');
	  $('div.new').animate({left: '0px'},15000, function() {
	  	setTimeout(newsRotate, 3000);
	  });
	};
	newsRotate();
});