/*
 * slideshow.js
 *
 * A jQuery plugin that transforms a container with containers
 * in it into a fully interactive slideshow with animated
 * transitions.
 *
 * built 2009 by max thom stahl, for vsa partners, inc.
 */

var slideshow_interactions = 0;   // Counter for the number of slideshow interactions in the last second

jQuery.fn.slideshow = function (callback) {
  counter = 0;
  var cleanup = callback; // Kludge to get around Javascript's fast-and-loose scope system
  
  if(typeof callback == "undefined") {
    cleanup = function () { return true; }
  }
  
  this.each(function () {
    var slideshow = $(this);
    
    var pagination = $(".slideshow-pagination", slideshow);
    var next_button = $(".next", slideshow);
    var prev_button = $(".prev", slideshow);
    var navigation = $(".slideshow-navigation", slideshow);
    
    // Initially, since the slideshow starts at slide zero, the "previous" button starts disabled
    prev_button.addClass("disabled");
    
    // This function encapsulates some repeated functionality in the callback 
    // functions for next/prev/etc.
    var slide_to = function (i) {
      slides = $(".slideshow-slide", slideshow);
      $(".slideshow-inner", slideshow).animate({ left: "-" + $(slides).eq(i).attr("offset") + "px" }, 750, "swing");
      old_slide = $(slideshow).attr("slide");
      $(slideshow).attr("slide", i);
      
      $("li", $(navigation)).attr("class", "");
      $("li", $(navigation)).eq(i).attr("class", "current");
      
      if(i == $(".slideshow-slide", slideshow).length - 1) {
        next_button.addClass("disabled");
      }
      else {
        next_button.removeClass("disabled");
      }
      
      if(i == 0) {
        prev_button.addClass("disabled");
      }
      else {
        prev_button.removeClass("disabled");
      }
      
      cleanup(old_slide, i);    // This callback does different stuff on different pages
    };
  
    // Some instance variables that might be handy
    $(this).attr("slide", "0");

    // Set up navigational "dots"
    if(navigation.length > 0) {
      $(navigation)
      .empty()
      .append(
        $(".slideshow-slide", this).map(function (index) {
          // Transform each slide into an LI item, which we'll insert into the UL one level up
          return $(document.createElement("li"))
          .click(function () {
            slide_to(index);
          })
          .text(index)
          .wrapInner("<i></i>")
          .get(0);
        })
      );
      $("li:first", navigation).attr("class", "current");   // First pagination dot needs "current" class
    }
    
    // Set up "previous" button
    $(prev_button)
    .unbind()
    .attr("href", "javascript:void(0);")
    .attr("rel", "prev")
    .click(function () {
      if($(".slideshow-inner", slideshow).queue().length > 0) {
        return;
      }
      
      // Do nothing if this is the first slide
      if(parseInt($(slideshow).attr("slide")) == 0) {
        return;
      }
      
      slides = $(".slideshow-slide", $(slideshow));
      current_slide = parseInt($(slideshow).attr("slide"));
      next_slide = (current_slide + slides.length - 1) % (slides.length);

      slide_to(next_slide);
    });
    
    // Set up "next" button
    $(next_button)
    .unbind()
    .attr("href", "javascript:void(0);")
    .attr("rel", "next")
    .click(function () {
      if($(".slideshow-inner", slideshow).queue().length > 0) {
        return;
      }
      
      // Do nothing if this is the last slide
      if(parseInt($(slideshow).attr("slide")) + 1 == $(".slideshow-slide", slideshow).length) {
        return;
      }
      
      slides = $(".slideshow-slide", $(slideshow));
      current_slide = parseInt($(slideshow).attr("slide"));
      next_slide = (current_slide + 1) % (slides.length);
      
      slide_to(next_slide);
    });
    
    // Set up pagination if it's supposed to exist
    if(pagination) {
      $(pagination).text("Page 1 of " + $(".slideshow-slide", $(slideshow)).length);
    }
    
    var get_offsets = function () {
      offset = 0;   // Temporary variable for offset of current slide
      // alert("there are " + $(".slideshow-slide", $(slideshow)).length + " slideshow slides up in this hizzy.");
      $(".slideshow-slide", $(slideshow)).each(function () {
        // alert("Current slideshow slide offset = " + offset);
        // alert("Current element width is " + $(this).width() + "px");
        $(this).attr("offset", offset);
        offset += $(this).width();
      });
    };
    
    window.setTimeout(function () {
      offset = 0;   // Temporary variable for offset of current slide
      $(".slideshow-slide", $(slideshow)).each(function () {
        $(this).attr("offset", offset);
        offset += $(this).width();
      });
      
      if(typeof slideshow.attr('after') != undefined) {
        eval(slideshow.attr("after"));
      }
    }, 30);
  });
  
  return this;
};


