/********************************************************************
 *
 * Fading slideshow jQuery plugin.
 *
 * All code in this file copyright Jez Cope, 2010.
 * Email jez@knighterrantdesign.co.uk for further information.
 *
 *******************************************************************/

(function($) {

$.fn.randomSlideshow = function(opts) {
  /* Global variables */
  var slides = [];
  var index = 0;
  var $nextButton = null;
  
  opts = $.extend({}, $.fn.randomSlideshow.defaults, opts);

  // Advance to the next slide
  function doNextSlide() {
    $(window).resize(); // Just to make sure everything is where it should be

    nextSlide = slides.shift();
    prevSlide = slides[slides.length - 1];
    slides.push(nextSlide);
    
    $(prevSlide).css({ zIndex: 0 });
    $(nextSlide).css({
        zIndex: 1,
        display: 'inline'
      });
  }
  
  // Resize slides when the window size changes
  function resizeSlides(event) {
    w = $(window).width();
    h = w * opts.aspectRatio
    
    $slideshowDiv.add(slides).css({
      width: w,
      height: h
    });
    
    if ($nextButton) {
      $nextButton.css({
        top: (h - $nextButton.height()) / 2,
        left: w - $nextButton.width() - 20
      });
    }
  }
  
  function fixIEScaling($slides) {
    if ($.browser.msie) {
      $slides.each(function(index, img) {
        var src = img.src;
        img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + Math.floor(Math.random() * 99999) + "', sizingMethod='scale')";
        img.src = opts.shim;
      });
    }
  }

  /* Main code starts here ***********************************************/

  var $slideshowDiv = $(this);

  // Detect whether we have a single array or an array-of-arrays
  if (typeof(opts.images[0]) == 'string')
    images = opts.images;
  else
    images = opts.images[Math.floor(Math.random() * opts.images.length)];
    
  // Create <img> element for each slide and add to the document
  slides = [];
  for (i = 0; i < images.length; i++) {
    img = new Image();
    img.src = images[i] + '?' + Math.floor(Math.random() * 99999);
    slides.push(img);
  }
  $slideshowDiv.html('').append(slides);
  $slides = $slideshowDiv.find('img');
  $slides.hide()  
    .css({
      position: 'absolute',
      top:      0,
      left:     0
  });
  $slideshowDiv.find('img').each(function (index) {
      var a = $('<a/>').attr('href', opts.urls[0][index]);
      $(this).wrap(a);
  });

  slides = $.makeArray($slides);
  
  fixIEScaling($slides);
  
  // Make sure the slideshow div is set up correctly for us
  $slideshowDiv.css({
    position: 'absolute',
    overflow: 'hidden',
    margin:   0
  });
  
  // Set up the next slide button, if we have one
  if (opts.nextButton) {
    $nextButton = $(opts.nextButton);
    $nextButton.click(doNextSlide);
  }
  
  // Set up window resize event and trigger it
  $(window).resize(resizeSlides);
  $(window).resize();
  
  if (opts.loader) {
    this.append('<img src="' + opts.loader + '" id="slideshow-loader">');
    $('#slideshow-loader').css({
      position: 'absolute',
      left: (this.width() - 220) / 2.0,
      top: (this.height() - 19) / 2.0
    });
    
    if ($slides[0].complete || $slides[0].readyState == 'complete') {
      $('#slideshow-loader').hide();
      doNextSlide();
    } else {
      $(slides[0]).bind('load', function(event) {
        $('#slideshow-loader').hide();
        doNextSlide();      
      });
    }
  } else {
    doNextSlide();
  }
};

$.fn.randomSlideshow.defaults = {
  loader:         null,
  shim:           'x.gif',
  aspectRatio:    9 / 16
};

// private function for debugging
function debug($obj) {
  if (window.console && window.console.log) {
    window.console.log($obj);
  }
}

})(jQuery);
