$(function(){
	// Create slideshow
	$("#FlashDiv").slideshow({ autoRun:true, video:V, callback:function(slide){stopVideo(slide); resetSlide(slide);} });
	
	 
	
	 
	
	$("#legacyDocumentary .videoLink a").click(function(){
		$("#legacyDocumentary .paneLead").hide();
		$("#legacyDocumentary .paneVideo").show();
		//clearInterval(slideInterval);
			//resetProgressBar();
			
		$('#vid1').biolavideoplayer({
			file: '',
			fallback: '',
			image: '',
		  width: 600,
		  height: 263,
			autostart: 'false',
			backcolor: '0x000000',
			
			frontcolor: '0xCCCCCC'
		});
 
		return true;
	});
	
 
	
});


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:10000, 
			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;
		// 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, 1000) });
		}
		// Set container width to sum of slide widths, plus a little extra
		container.width((w * slides.length) + 4);
		
		// Create Navigation
		createNavItem("prev", "prev").click(function(){
			slidePrev(); return false; 
		});
		slides.each(function(i) {
			createNavItem("", i + 1).click(function(){ 
				slideTo(i); return false; 
			});
		});
		createNavItem("next", "next").click(function(){
		if(activeSlide == 9)
			 {
			 stopAutoRun();
			 }
			 else{
		   
			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");
			activeSlide = id;
			hilightNav(id + 1);
			container.animate({left:(-w * id)}, 300, "linear", function(){ activateSlide(id+1); });
			}
	 	
		function slideNext() {
			if(activeSlide==9)
			 {
			 stopAutoRun();
			 }
			 else{
			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() {
		if(activeSlide ==9){
		
		return $("<div class='progressBar'><div class='bar'></div></div>").insertAfter(nav);
		}
		else{
			return $("<div class='progressBar'><div class='bar'></div></div>").insertAfter(nav);
			}
		}		
		function runProgressBar() {
		if(activeSlide==9)
			 { 
			progressBar.find(".progressBar").addClass("c");
			// progressBar.find(".bar").animate({width:100}, (options.interval-1000), "", function(){ progressBar.find(".bar").width(0);slideNext(); });}
			 }
			 else{
			progressBar.find(".bar").animate({width:100}, (options.interval-1000), "linear", function(){ progressBar.find(".bar").width(100); slideNext(); });}
			
		}		
		function resetProgressBar() {
			progressBar.find(".bar").stop().width(0);
		}		
		function activateSlide(id) {
			slides.eq(id-1).addClass("active");
		}
	};
})(jQuery);