function mycarousel_itemLoadCallback(carousel, state) {
    if (carousel.has(carousel.first, carousel.last)) {
        return;
    }
	
	$('#loading').show();
	carousel.lock();
	
    jQuery.get(
		url_itemLoadCallback,
        {
            first: carousel.first,
            last:  carousel.last,
			state: state
        },
        function(xml) {
            mycarousel_itemAddCallback(carousel, carousel.first, carousel.last, xml);
			$('#loading').hide();
			carousel.unlock();
        },
        'xml'
    );
};

function mycarousel_itemAddCallback(carousel, first, last, xml)
{
    carousel.size(parseInt(jQuery('total', xml).text()));

    jQuery('item', xml).each(function(i) {
        carousel.add(first + i, jQuery(this).text());
    });
};

function retornaAjax(id) {	
  $('#pilula').fadeOut('slow', function() {
    $('#loading2').fadeIn('fast');
  });

  jQuery.get(
    url_retornaAjax,
    {
      id: id,
      ajax: true
    },
    function(data) {
      $('#pilula').stop(true, true);
      $('#loading2').stop(true, true);

      $('#pilula').html(data);

      addthis.init();
      addthis.toolbox('#toolbox');

		  $("#player_" + id).jmp3({
		      filepath: webroot + "mp3/pilulas/",
		      swfpath: webroot + "swf/"
		  });
		    
      $('#loading2').hide();
      $('#pilula').fadeIn('slow');
    }
  );

  return false;
}

jQuery(document).ready(function() {	

  jQuery('#pilulas').jcarousel({
	scroll: 4,
      itemLoadCallback: mycarousel_itemLoadCallback
  });

	// Evita seleção quando as setas são clicadas rapidamente
	$.extend($.fn.disableTextSelect = function() {
        return this.each(function(){
            if($.browser.mozilla){//Firefox
                $(this).css('MozUserSelect','none');
            }else if($.browser.msie){//IE
                $(this).bind('selectstart',function(){return false;});
            }else{//Opera, etc.
                $(this).mousedown(function(){return false;});
            }
        });
    });
	$('#slider *').disableTextSelect();
		
	jQuery('#slider a').live('click', function() {
		retornaAjax(this.id);
	});
	retornaAjax(id);
});

