$(function(){
	// Create slideshow
	$("#featureMain").slideshow({ autoRun:true, video:V, callback:function(slide){stopVideo(slide); resetSlide(slide);} });
});


var V = { state:null };
// Automatically called by video when state changes
function getUpdate(type, pr1, pr2, swf) {
	if (type == 'state') { V.state = pr1; }
}

// This is mostly for IE
function stopVideo(slide) {
	//If a video has been playing, stop it
	if (slide.find(".paneVideo").css('display') == "block") {
		slide.find(".paneVideo .video").html("");
	}	
}

function resetSlide(slide) {
	if (slide.get(0).id == "teacherYear" || slide.get(0).id == "stepsHope" || slide.get(0).id == "legacyDocumentary" || slide.get(0).id == "undergradWhy" || slide.get(0).id == "centennialYear") {
		slide.find(".paneLead").show();
		slide.find(".paneVideo").hide();
	}
}

(function($) {
	$.fn.slideshow = function(options) {
		var defaults = { 
			nav:"> .navSlides", 
			autoRun:false, 
			interval:8000, 
			video:{state:null}, 
			callback:null 
		};
	  // Extend our default options with those provided.
	  var options = $.extend(defaults, options);
		var w = this.width();
		var nav = $("<ul></ul>").appendTo(this.find(options.nav));
		var container = this.find(".slides");
		var slides = this.find(".slide");
		var activeSlide = 0;
		var slideInterval;
		slides.hide();
		
		// Set up auto run, if enabled
		if (options.autoRun && !(/iPhone/i.test(navigator.userAgent))) {
			var progressBar = createProgressBar();
			startAutoRun();
			this.hover(stopAutoRun, function(){ slideInterval = setTimeout(startAutoRun, 2000) });
		}
		// Set container width to sum of slide widths, plus a little extra
		//container.width((w * slides.length) + 4);
		
		// Create Navigation
		createNavItem("prev", "Previous").click(function(){
			slidePrev(); return false; 
		});
		slides.each(function(i) {
			createNavItem("", i + 1).click(function(){ 
				slideTo(i); return false; 
			});
		});
		createNavItem("next", "Next").click(function(){
			slideNext(); return false;
		});

		// Activate first item by default
		activateSlide(1);
		hilightNav(1);
		
		// Functions
		function createNavItem(className, text) {
			return $("<li><a href='#'>"+text+"</a></li>").addClass(className).appendTo(nav);
		}		
		function slideTo(id) {
			if (options.callback != null) { options.callback.call(this, slides.eq(activeSlide)); };
			slides.removeClass("active");
			slides.fadeOut("fast");
			activeSlide = id;
			hilightNav(id + 1);
			activateSlide(id+1);
			//container.animate({left:(-w * id)}, 300, "linear", function(){ activateSlide(id+1); });
		}		
		function slideNext() {
			slideTo((activeSlide + 1) % slides.length);
		}		
		function slidePrev() {
			slideTo((activeSlide + (slides.length-1)) % slides.length);
		}
		function hilightNav(id) {
			nav.find("li").removeClass("active");
			nav.find(":nth-child("+(id+1)+")").addClass("active");
		}		
		function startAutoRun() {
			// Prevent auto-run if a video pane is active
			if (slides.eq(activeSlide).find(".paneVideo").css('display') == "block") return false;
			
			runProgressBar();
			slideInterval = setInterval(function(){ runProgressBar(); }, options.interval);
		}		
		function stopAutoRun() {
			clearInterval(slideInterval);
			resetProgressBar();
		}		
		function createProgressBar() {
			return $("<div class='progressBar'><div class='bar'></div></div>").insertAfter(nav);
		}		
		function runProgressBar() {
			progressBar.find(".bar").animate({width:100}, (options.interval-1000), "linear", function(){ progressBar.find(".bar").width(0); slideNext(); });
		}		
		function resetProgressBar() {
			progressBar.find(".bar").stop().width(0);
		}		
		function activateSlide(id) {
			slides.eq(id-1).addClass("active").fadeIn("slow");
		}
	};
})(jQuery);

