/*
 * 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.
 * edits 2009 - 2010 by ryan laverty for shure incorporated
 */

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_");
    if($('.slideshow-slide').length == 1) {
			next_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);
			if($(slideshow).hasClass("no-slide")) {
				//$(".slideshow-inner", slideshow).css({ left: "-" + $(slides).eq(i).attr("offset") + "px" });
				//setTimeout(function() {
					$(slides).hide();
					$(slides).eq(i).show();
				//}, 600);
			}
			else { 
				$(".slideshow-inner", slideshow).animate({ left: "-" + $(slides).eq(i).attr("offset") + "px" }, 500);
			}
			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"
		$(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);
					return false;
				})
				.text(index)
				.wrapInner("<a href='#'><i></i></a>")
				.get(0);
			})
		);
		$("li:first", navigation).attr("class", "current");		// First pagination dot needs "current" class
		
		// Set up "previous" button
		$(prev_button)
		.unbind()
		.attr("href", "#")
		.attr("rel", "prev")
		.click(function () {
			if($(".slideshow-inner", slideshow).queue().length > 0) {
				return false;
			}
			
			// Do nothing if this is the first slide
      		if(parseInt($(slideshow).attr("slide")) == 0) {
        		return false;
      		}
			
			slides = $(".slideshow-slide", $(slideshow));
			current_slide = parseInt($(slideshow).attr("slide"));
			next_slide = (current_slide + slides.length - 1) % (slides.length);

			slide_to(next_slide);
			return false;
		});
		
		// Set up "next" button
		$(next_button)
		.unbind()
		.attr("href", "#")
		.attr("rel", "next")
		.click(function () {
			if($(".slideshow-inner", slideshow).queue().length > 0) {
				return false;
			}
			
      		// Do nothing if this is the last slide
      		if(parseInt($(slideshow).attr("slide")) + 1 == $(".slideshow-slide", slideshow).length) {
        		return false;
      		}
			
			slides = $(".slideshow-slide", $(slideshow));
			current_slide = parseInt($(slideshow).attr("slide"));
			next_slide = (current_slide + 1) % (slides.length);
			
			slide_to(next_slide);
			return false;
		});
		
		// 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
	    if(!$(slideshow).hasClass("no-slide")) {
	    	$(".slideshow-slide", $(slideshow)).each(function () {
	      		$(this).attr("offset", offset);
	      		offset += $(this).width();
	    	});
	    }

	    if(slideshow.attr('after') != undefined) {
        eval(slideshow.attr("after"));
	    }
		}, 30);
  });
  
  return this;
};
