function photos_initCallback(carousel, state) {
  if (state == 'init') { 
    phoCarousel = carousel;
  }
}

function photos_itemLoadCallback(carousel, state){
  if (carousel.prevFirst != null) {
    // Remove the last visible items to keep the list small
    for (var i = carousel.prevFirst; i <= carousel.prevLast; i++) {
      // jCarousel takes care not to remove visible items
      carousel.remove(i);
    }
  }
  
  var per_page = carousel.last - carousel.first + 1;
  var currPage = 0;
  var f, l;
  var cr = carousel;
  
  for (var i = carousel.first; i <= carousel.last; i++) {
    var page = Math.ceil(i / per_page);
    
    if (currPage != page) {
      currPage = page;
      
      f = ((page - 1) * per_page) + 1;
      l = f + per_page - 1;
      
      f = f < carousel.first ? carousel.first : f;
      l = l > carousel.last ? carousel.last : l;
      
      if (carousel.has(f, l)) {
        continue;
      }
      
      photos_makeRequest(carousel, f, l, per_page, page);
    }
  }
};

function photos_makeRequest(carousel, first, last, per_page, page){
  // Lock carousel until request has been made
  carousel.lock();
  
  // Get Album ID
  fb_album_id = $('#photo').attr('class');

  $.ajax({
    dataType: "json",
    url: '/loader/photos/' + page + '/' + per_page + '/' + fb_album_id,
    success: function(data) {
      photos_itemAddCallback(carousel, first, last, data, page);
    }
  });
};

function photos_itemAddCallback(carousel, first, last, data, page){
  // Unlock
  carousel.unlock();
  
  // Set size
  carousel.size(data.total);
  
  var photos = data.photos;

  var per_page = carousel.last - carousel.first + 1;
  
  for (var i = first; i <= last; i++) {
    var pos = i - 1;
    var idx = Math.round(((pos / per_page) - Math.floor(pos / per_page)) * per_page);
    carousel.add(i, photos_getItemHTML(photos[idx]));
  }
};

/**
 * Global item html creation helper.
 */
function photos_getItemHTML(photo){

  // Generate the HTML
  var html = '';
  html += '<div class="fb_thumb_holder">';
  html += '  <a href="' + photo.source + '" alt="' + photo.height + '" rel="' + photo.width + '" title="' + photo.name + '">';
  html += '    <div class="fb_thumb" style="background-image: url(&quot;' + photo.picture + '&quot;);"></div>';
  html += '  </a>';
  html += '</div>';
   
   return html;
};


