$(document).ready(function() {

    //rotation speed and timer
    var speed = 6000;
    var run = setInterval('rotate()', speed);	
    var stop = clearInterval('rotate()', speed);
		
    //grab the width and calculate left value
    var item_width = $('#slides').outerWidth(); 
    var left_value = item_width * (-1); 
			
    //move the last item before first item, just in case user click prev button
    $('.slide:first, .slide2:first').before($('.slide:last, .slide2:last'));
		
    //set the default item to the correct position 
    $('#slides').css({
        'left' : left_value
    });

    jQuery('#prev').click(function() {
        jQuery('#slides').fadeOut('slow', function() {
            //move the last item and put it as first item
            jQuery('.slide:first, .slide2:first').before(jQuery('.slide:last, .slide2:last'));
            jQuery('#slides').fadeIn('slow');
            setButtonText();
	 
        });
	 
        //cancel the link behavior
        return false;
    }); 
	 
    //if user clicked on next button
    jQuery('#next').click(function() {
        jQuery('#slides').fadeOut('slow', function() {
            //move the last item and put it as first item
            jQuery('.slide:last, .slide2:last').after(jQuery('.slide:first, .slide2:first'));
            jQuery('#slides').fadeIn('slow');
            setButtonText();
					
        });

        //cancel the link behavior
        return false; 

    });     

    //funcao para pausar a sequencia quando clicar em um dos botoes (reinicar timer)
    jQuery('#next, #prev').hover(
        function() {
            clearInterval(run);
        },  
        function() {
            run = setInterval('rotate()', speed);	
        }
        );  
  
});  


function rotate() {
    jQuery('#next').click();
}

function stop() {
    jQuery('#next').click(stop);
}

function setButtonText()
{
    jQuery('#prev').attr('title',jQuery('.slide:last .title').html());
    jQuery('#next').attr('title',jQuery('.slide:nth-child(2) .title').html());
}


