$(function() {
  $('#home-slideshow').each(function() {
    var slideshow = this;
    $.ajax({
      url:'/contents.xml?type=image&folder=/galleries/home',
      dataType:'xml',
      success:function(xml) {
        $(xml).find('item').each(function(i) {
          var title = $(this).find('title').text();
          var link = $(this).find('link').text();
          var src = $(this).find('url').text();
          var dsc = $(this).find('description').text();
          html = '<div class="image" style="display: none"><img src="' + src + '" /><div class="background"></div><h3>' + title + '</h3><p>' + dsc + '</p><a class="more" href="' + link + '">Read more</a></div>';
          $(slideshow).find('.images').append(html);
          $(slideshow).find('.indicators').append('<span>&#x25CF;</span>');
        });
        $(slideshow).find('.images img').eq(0).load(function() {
          advance_slideshow();
        });
        window.setInterval('advance_slideshow()', 10000);
      }
    });
  });
});

function advance_slideshow() {
  $('#home-slideshow').each(function() {
    var images = $(this).find('.image');
    var gallery_index = images.filter(':visible').eq(0).prevAll('.image').length;
    $('#home-slideshow .indicators span').removeClass('active');
    if (images.eq(gallery_index).filter(':visible').animate({ opacity:0.0, left:'-696px' }, function() { $(this).hide() }).length > 0) {
      gallery_index = (gallery_index + 1) % images.length;
    }
    $('#home-slideshow .indicators span').eq(gallery_index).addClass('active');
    images.eq(gallery_index).css({ display:'block', opacity:0.0, left:'696px' }).animate({ opacity:1.0, left:'0px' });
  });
}
