$(function() {
  $('#destination-gallery').each(function() {
    var slideshow = this;
    var folder = $(this).find('a.photos').attr('href').replace(/^.*folder=/, '');
    $.ajax({
      url:$(this).find('a.photos').attr('href'),
      dataType:'xml',
      success:function(xml) {
        $(xml).find('item').each(function(i) {
          var src = $(this).find('url').text();
          var dsc = $(this).find('description').text();
          var fld = $(this).find('folder').text();
          if (fld == folder) {
            html = '<div class="image" style="display: none"><img src="' + src + '" /><div class="background"></div><p>' + dsc + '</p></div>';
            $(slideshow).find('.images').append(html);
            $('<img src="' + src + '?s=tiny" />').appendTo('#destination-gallery .thumbnails').click(function() {
              $(slideshow).find('.image.active').fadeOut().removeClass('active');
              $(slideshow).find('.thumbnails img').removeClass('active');
              $(this).addClass('active');
              $(slideshow).find('.images .image').eq($(this).prevAll('img').length).addClass('active').fadeIn();
            });
          }
        });
        $(slideshow).find('.images img').eq(0).load(function() {
          $('#destination-gallery .thumbnails img').eq(0).click();
        });
      }
    });
  });
});

