(function($) {
  var slides = [
	{
	  "artist": "Laetitia Schlesser-Gamelin", 
	  "image": "/wp-content/uploads/gravity_forms/5/2010/09/SCHLESSER-GAMELIN---RESINE-SUR-TOILE---50X50CM---2010---001---Copie.jpg", 
	  "publisher": "", 
	  "title": "Untitled"
	},
	{
	  "artist": "Irwin Montlack", 
	  "image": "/wp-content/uploads/gravity_forms/5/2010/09/trio-a.jpg", 
	  "publisher": "", 
	  "title": "String Trio"
	},
	{
	  "artist": "Claudette McDermott", 
	  "image": "/wp-content/uploads/gravity_forms/5/2010/09/CM-Texture-Abstract.jpg", 
	  "publisher": "Poetry On Canvas Pleasanton", 
	  "title": "Nude - Textured Abstract"
	},
	{
	  "artist": "Anna Razumovskaya", 
	  "image": "/wp-content/uploads/gravity_forms/5/2010/08/4942882622_276bb3d6a3_o.jpg", 
	  "publisher": "ANNA ART Publishing", 
	  "title": "Fusion"
	},
	{
	  "artist": "Richard Riverin", 
	  "image": "/wp-content/uploads/gravity_forms/5/2010/08/4942296717_d247ff8c5e_b.jpg", 
	  "publisher": "La Belle Image", 
	  "title": "Sailboat Fantasy"
	},
	{
	  "artist": "Ivan Alifan", 
	  "image": "/wp-content/uploads/gravity_forms/5/2010/08/4942296513_c470976f35_b.jpg", 
	  "publisher": "ANNA ART Publishing", 
	  "title": "Memories Smell Like Flowers"
	},
	{
	  "artist": "Anna Kornycheva", 
	  "image": "/wp-content/uploads/gravity_forms/5/2010/08/4924576898_392ca087f5_b.jpg", 
	  "publisher": "", 
	  "title": "Mental order: Branta canadensis"
	},
	{
	  "artist": "Oksana Prokopenko", 
	  "image": "/wp-content/uploads/gravity_forms/5/2010/08/4942296855_e289c5d7ba_o.jpg", 
	  "publisher": "", 
	  "title": "Mary and Elizabeth"
	},
	{
	  "artist": "Steve Horan", 
	  "image": "/wp-content/uploads/gravity_forms/5/2010/08/4911422280_d8e1fbc91a_b.jpg", 
	  "publisher": "", 
	  "title": "China Town Lights"
	},
	{
	  "artist": "Tenold Peterson", 
	  "image": "/wp-content/uploads/gravity_forms/5/2010/08/4910792591_f433b51cea_b.jpg", 
	  "publisher": "", 
	  "title": "Spider Chrysanthemum Dance"
	},
	{
	  "artist": "Valérie Girone (VAL)", 
	  "image": "/wp-content/uploads/gravity_forms/5/2010/08/4907413689_f2ff6229f8_b.jpg", 
	  "publisher": "", 
	  "title": "Wakanda"
	},
	{
	  "artist": "Corey West", 
	  "image": "/wp-content/uploads/gravity_forms/5/2010/08/4808703817_b908dfd5b1_b.jpg", 
	  "publisher": "", 
	  "title": "State of Mind"
	},
	{
	  "artist": "Leon Oks", 
	  "image": "/wp-content/uploads/gravity_forms/5/2010/08/4908004434_ed365b9c75_b.jpg", 
	  "publisher": "[SOLO]", 
	  "title": "Dream"
	},
	{
	  "artist": "Anahit Etemadi", 
	  "image": "/wp-content/uploads/gravity_forms/5/2010/08/4908004602_4a000c0227_o.jpg", 
	  "publisher": "The Academy of Spherical Art", 
	  "title": "Gomgashteh"
	},
	{
	  "artist": "Martin Quen", 
	  "image": "/wp-content/uploads/gravity_forms/5/2010/08/4029593185_16371f1a1f_o1.jpg", 
	  "publisher": "Deljou Art Group", 
	  "title": "Pavement"
	},
	{
	  "artist": "Vincent George", 
	  "image": "/wp-content/uploads/gravity_forms/5/2010/08/4030348636_6d56cb16d8_o1.jpg", 
	  "publisher": "Deljou Art Group", 
	  "title": "City Lights"
	},
	{
	  "artist": "Craig Alan", 
	  "image": "/wp-content/uploads/gravity_forms/5/2010/08/3962140685_8031fd573f_o1.jpg", 
	  "publisher": "", 
	  "title": "Populus 3"
	},
	{
	  "artist": "Cecil K", 
	  "image": "/wp-content/uploads/gravity_forms/5/2010/08/4029591677_3c4c8f1d21_o1.jpg", 
	  "publisher": "Deljou Art Group", 
	  "title": "Masquerade"
	},
	{
	  "artist": "Chas Harold", 
	  "image": "/wp-content/uploads/gravity_forms/5/2010/08/4332879435_47b2c66e1d_o1.jpg", 
	  "publisher": "", 
	  "title": "Red Dawn"
	},
	{
	  "artist": "Joshua Gage", 
	  "image": "/wp-content/uploads/gravity_forms/5/2010/08/4333619956_69b48355fa_o1.jpg", 
	  "publisher": "", 
	  "title": "Free Thought"
	},
	{
	  "artist": "Marks", 
	  "image": "/wp-content/uploads/gravity_forms/5/2010/08/4333619846_66d47c026a_o1.jpg", 
	  "publisher": "", 
	  "title": "Surface Creation"
	},
	{
	  "artist": "Ken Orton", 
	  "image": "/wp-content/uploads/gravity_forms/5/2010/08/4626372651_14faa7519b_o1.jpg", 
	  "publisher": "", 
	  "title": "Eclipse of The Greys"
	},
	{
	  "artist": "Hamilton Aguiar", 
	  "image": "/wp-content/uploads/gravity_forms/5/2010/08/4265973545_b2bb08904e_o1.jpg", 
	  "publisher": "Nan Miller Gallery", 
	  "title": "Trio"
	},
	{
	  "artist": "Irina Koulikov", 
	  "image": "/wp-content/uploads/gravity_forms/5/2010/08/3995682421_957e26ed8b_o1.jpg", 
	  "publisher": "Progressive Fine Art", 
	  "title": "Untitled"
	},
	{
	  "artist": "Virginia Wood", 
	  "image": "/wp-content/uploads/gravity_forms/5/2010/08/3996445146_a37ded6fb1_o.jpg", 
	  "publisher": "Progressive Fine Art", 
	  "title": "Untitled"
	},
	{
	  "artist": "Victor Nemo", 
	  "image": "/wp-content/uploads/gravity_forms/5/2010/08/3995683935_9e8440c266_o1.jpg", 
	  "publisher": "Progressive Fine Art", 
	  "title": "Untitled"
	}
  ];//end

  function shuffle(array) {
    var tmp, current, top = array.length;

    if(top) while(--top) {
        current = Math.floor(Math.random() * (top + 1));
        tmp = array[current];
        array[current] = array[top];
        array[top] = tmp;
    }

    return array;
  }
  slides = shuffle(slides);
  
  function nextSlide($container) {
    var $slides = $container.children();
    var $active = $slides.filter('.active');
    var $next =  $active.next().length ? $active.next() : $slides.eq(0);
    
    $active.addClass('last-active');
    $next.css({opacity: 0.0})
      .addClass('active')
      .animate({opacity: 1.0}, 1000, function() {
          $active.removeClass('active last-active').css('display', '');
      });
    
    //preload the slide after the one that fades in
    var position = $slides.index($next);
    if(position < slides.length-1 && $next.next().length == 0) {
      $container.append(createSlide(position+1));
    }
  }
  
  function createSlide(index) {
    var slide = slides[index];
    return $("<div class='galleryItem'></div>")
      .append(
        $("<div class='galleryImage'></div>")
          .css('background-image', 'url(' + slide['image'] + ')'),
        "<div class='galleryCaptionFold'></div>",
        $("<div class='galleryCaption'></div>").append(
          $("<div class='artist'></div>").text(slide['artist']),
          $("<div class='title'></div>").text(slide['title']),
          $("<div class='publisher'></div>").text(slide['publisher'])
        )
      ).show().css('opacity', 0);
  }
  
  $(function() {
    //initialize the slideshow
    var container = $('#featuredArt').empty().append(createSlide(0).css('opacity', 1), createSlide(1));
    
    $('#featuredArt').children()
      .filter(':first').addClass('active') //show the first slide
      .next().show();                      //and preload the next
    
    setInterval(function() { nextSlide(container) }, 5000);
  });
})(jQuery);