/* ****
	FileName:		slideshow.js
	Author:			Don Stier, Maplegate Technologies, LLC, 
		from Sitepoint, "JQuery Novice to Ninja"
	Date:				3/29/2011
**** */
$(document).ready(function(){
	//$('#slideshowbox').prepend('<span id="slideshowbox-loading">Loading....</span>');
	//$('#slideshowbox-loading').css('visibility','visible');
	GALLERY.load();
  var numberOfPics = $('#slideshowbox img').length;
  var next = Math.floor(Math.random() * numberOfPics);
  rotatePics(next);
	//$('#slideshowbox-loading').fadeOut(5000);
});

function rotatePics(currentPhoto) {
  var numberOfPhotos = $('#slideshowbox img').length;
  currentPhoto = currentPhoto % numberOfPhotos;
  $('#slideshowbox img').eq(currentPhoto).fadeOut('slow',function() {
    $('#slideshowbox img').each(function(i) {
      $(this).css(
        'zIndex', ((numberOfPhotos - i) + currentPhoto) % numberOfPhotos
      );
    });
    $(this).show();
    setTimeout(function() {rotatePics(++currentPhoto);}, 4000);
  });
}

var GALLERY = {
  container: '#slideshowbox',
  delay: 5000, 
  load: function() {
		var sGallery = $('#slideshowbox p:first').text();
		var dir = 'slideshow/' + sGallery + '/';
    var _gallery = this;
    $.ajax({ 
      type:"get", 
			dataType:"text",
			url: dir + sGallery + '.txt',
      success: function(data){ 
				//var iList = data.substr(2);
        var images = data.split('|');
        $.each(images, function() {
          _gallery.display(this, dir);
        });
      }
    });
  },
  display: function(image_url, dir) {
    $('<img></img>')
      .attr('src', dir + image_url)
      .hide()
      .load(function() { 
        $(this).fadeIn();
      })
      .appendTo('#slideshowbox');
  }
}
