$.fn.slideshow = function(options) {
	var defaults = { 
		nav:"#slideshowNav",
		autoRun:false,
		interval:8000,
		fadeSpeed:500
	};
	// extend our default options with those provided
	var options = $.extend(defaults, options);
	var nav = $("body").find(options.nav);
	var activeSlide = 0;
	var slideInterval;
	var slideContainer = this.find(".slides");
	var slides = this.find(".slide");
	var numSlides = slides.length;
	var firstSlideDiv = slides.eq(0);
	var firstSlideImg = slides.eq(0).find("img");
	var firstSlideAnchor = slides.eq(0).find("a");
	
	// save first slide at bottom of container for later access
	slideContainer.append('<div class="slide">'+firstSlideDiv.html()+'</div>');
	slides = slideContainer.find(".slide"); // re-populate slides object
	
	// set up auto run, if enabled
	if (options.autoRun) {
		stopAutoRun();
		// wait until all images on page are loaded
		$(window).load(function() {
			stopAutoRun();
			slideInterval = setTimeout(startAutoRun, 2000);
		});
		this.hover(stopAutoRun, function(){ slideInterval = setTimeout(startAutoRun, 2000) });
		//nav.hover(stopAutoRun, function(){ slideInterval = setTimeout(startAutoRun, 2000) });
	}
	
	// override default behavior of nav links - click brings up slide - if slide already active then goes to page
	nav.find("a").click(function(event){
		event.preventDefault();
	});
	nav.find("div").eq(0).click(function(){
		if(activeSlide == 0) window.location=$(this).find("a").attr("href");
		slideTo(0);stopAutoRun();startAutoRun();
	});
	nav.find("div").eq(1).click(function(){
		if(activeSlide == 1) window.location=$(this).find("a").attr("href");
		slideTo(1);stopAutoRun();startAutoRun();
	});
	nav.find("div").eq(2).click(function(){
		if(activeSlide == 2) window.location=$(this).find("a").attr("href");
		slideTo(2);stopAutoRun();startAutoRun();
	});
	nav.find("div").eq(3).click(function(){
		if(activeSlide == 3) window.location=$(this).find("a").attr("href");
		slideTo(3);stopAutoRun();startAutoRun();
	});
	nav.find("div").eq(4).click(function(){
		if(activeSlide == 4) window.location=$(this).find("a").attr("href");
		slideTo(4);stopAutoRun();startAutoRun();
	});

	// activate first item by default
	activateSlide(1);
	hilightNav(1);

	// functions
	function slideTo(id) {
		slides.removeClass("active");
		activeSlide = id;
		setTimeout(function(){hilightNav(id + 1)}, 100)
		imageFadeHack(activeSlide+1);
		activateSlide(id+1);
	}
	
	function slideNext() {
		slideTo((activeSlide + 1) % numSlides);
	}
	
	function slidePrev() {
		slideTo((activeSlide + (numSlides-1)) % numSlides);
	}
	
	function hilightNav(id) {
		nav.find("div").removeClass("slideshowNavDescActive");
		nav.find("div").eq(id-1).addClass("slideshowNavDescActive");
	}

	function startAutoRun() {
		clearInterval(slideInterval);
		slideInterval = setInterval(function() { slideNext(); },options.interval);
	}

	function stopAutoRun() {
		clearInterval(slideInterval);
	}

	function imageFadeHack(currentSlide) {
		// this will fade one image to another without using CSS positioning
		// that means drop down menus will still work in IE7
		var slideInterval2;
		var slideContainerRef = currentSlide - 1;
		if(slideContainerRef == 0) slideContainerRef = numSlides;
		
		// set background of div to new image
		firstSlideDiv.css('backgroundImage','url('+slides.eq(slideContainerRef).find("img").attr("src")+')');
		// fade out top image to reveal new image in background
		firstSlideImg.animate({opacity:0}, options.fadeSpeed, "linear");
		// change the link and alt text also
		firstSlideAnchor.attr('href',slides.eq(slideContainerRef).find("a").attr("href"));
		firstSlideImg.attr('alt',slides.eq(slideContainerRef).find("img").attr("alt"));
		slideInterval2 = setTimeout(function(){
			// after a short delay, change main image to same one in background
			firstSlideImg.attr('src',slides.eq(slideContainerRef).find("img").attr("src"));
			// now fade it back in so it's safe to swap in a new background image and repeat
			// note: user doesn't see this animation since the image and background are identical
			firstSlideImg.animate({opacity:1}, 10, "linear");
		},options.fadeSpeed+10);
	}

	function activateSlide(id) {
		slides.eq(id-1).addClass("active");
	}
}
