// SlideIO ... jQuery plugin for slide in and out slideshow
// Version 0.3.1
// April 28, 2011
//
// By Rob Watts, http://magicedit.com
// Provided under the MIT license. Feel free to use it however you like.
//
// Full documentation at: http://magicedit.com/SlideIOASimpleInOutS.cl
//
// Example:
// $("ul.slideIO").slideIO();
//
// To override defaults, pass in a defaults object:
// $("ul.slideIO").slideIO({
//    inSpeed: 100,
//    outSpeed: 100,
//    delay: 5000
// });



function swapSlide($slides, options) {
  var count = $slides.length;
  var newIndex = 0; // Index of new slide
  var $currentSlide = $slides.filter(".currentSlide");
  var currentIndex = 0;


  // See if a slide has been selected yet and set the index
  if ( $currentSlide.length ) {
    currentIndex = $slides.index($currentSlide);

    // Determine if on last slide, and if not, increment
    if ( currentIndex < ( count-1 ) )
      newIndex = currentIndex + 1;
    else {
      newIndex = 0;
    }
  }
  else {
    $currentSlide = $slides.eq(0);
  }

  $currentSlide
    .removeClass("currentSlide")
    .animate({
      left: options.left,
	  top: options.top,
	  opacity: options.outOpacity
	  }, options.outSpeed, options.easeOut, function() {
	$slides
	  .eq(newIndex)
	  .addClass("currentSlide")
	  .delay(options.wait)
	  .animate({
	    left: "0",
		top: "0",
		opacity: options.inOpacity
		}, options.inSpeed, options.easeIn);
      });

}


jQuery.fn.slideIO = function(options) {
  var $slides = this.children();
  var slideInterval;
  var settings;

  // Set up defaults
  settings = jQuery.extend({
    inSpeed: 500,                            // Time to slide in (ms)
	outSpeed: 300,                       // Time to slide out (ms)
	delay: 7000,                         // How long to display each slide (ms)
	showFirst: false,                    // Set whether the first slide appears immediately or slides in
	wait: 500,                           // Time to wait before sliding in
	easeIn: "swing",                     // Easing to use when moving the slide in
	easeOut: "swing",                    // Easing to use when moving the slide out
	inOpacity: 1,                        // Opacity of slide when shown
	outOpacity: 1,                       // Opacity of slide when hidden (should be > 0).
	mousePause: true,                    // Pause sliding when mouse is over the slide
	left: $slides.eq(0).css("left"),     // The "left" value when the slide is out
	top: $slides.eq(0).css("top")        // The "top" value when the slide is out
	}, options);

  if (settings.showFirst) {
    // Position the first panel
    $slides
      .first()
      .css({
	left: "0",
	    top: "0",
	    opacity: settings.inOpacity
	    })
      .addClass("currentSlide");
  }

  if ( !settings.showFirst )
    swapSlide($slides, settings);

  // Set up auto switching if more than one slide
  if ( $slides.length > 1 ) {
    slideInterval = setInterval(function() {
	swapSlide($slides, settings);
      },
      settings.delay
      );

    if ( settings.mousePause ) {
      $slides.hover(
		    function () {
		      clearInterval(slideInterval);
		    },
		    function () {
		      slideInterval = setInterval(function() {
			  swapSlide($slides, settings);
			},
			settings.delay
			);
		    });
    }
  }

};

