$(function(){
	// Create slideshow
	$("#featureMain").slideshow({ autoRun:true, video:V, callback:function(slide){stopVideo(slide); resetSlide(slide);} });
	
	$("#teacherYear .videoLink a").click(function(){
		$("#teacherYear .paneLead").hide();
		$("#teacherYear .paneVideo").show();
		$('#vid3').biolavideoplayer({ 
			file: 'http://biolaedu.web.aplus.net/video/spotlight_michael_long_fp.mp4',
			fallback: 'http://biolaedu.web.aplus.net/video/spotlight_michael_long_fp.flv',
			image: 'http://www.biola.edu/academics/education/media/images/michael_long_preview.jpg',
			captions: 'http://biolaedu.web.aplus.net/video/spotlight_michael_long_fp_cc.xml',
			autostart: 'true',
			backcolor: '0x222222',
			frontcolor: '0xCCCCCC'
		});
		return false;
	});
	
	$("#centennialYear .videoLink a").click(function(){
		$("#centennialYear .paneLead").hide();
		$("#centennialYear .paneVideo").show();
		$('#vid4').biolavideoplayer({ 
			file: 'http://biolaedu.web.aplus.net/video/centennial_year_recap_480.mp4',
			image: 'http://biolaedu.web.aplus.net/video/centennial_year_recap_480_t.jpg',
			captions: 'http://biolaedu.web.aplus.net/video/centennial_year_recap_cc.xml',
			autostart: 'true',
			backcolor: '0x222222',
			frontcolor: '0xCCCCCC',
			minflashver: '9,0,115'
		});		
		return false;
	});	
	
	$("#legacyDocumentary .videoLink a").click(function(){
		$("#legacyDocumentary .paneLead").hide();
		$("#legacyDocumentary .paneVideo").show();
		$('#vid1').biolavideoplayer({
			file: 'http://biolaedu.web.aplus.net/video/biola_100doc_8min.mp4',
			fallback: 'http://biolaedu.web.aplus.net/video/biola_100doc_8min.flv',
			image: 'http://biolaedu.web.aplus.net/video/biola_100doc_8min_t.jpg',
			captions: 'http://biolaedu.web.aplus.net/video/biola_100doc_8min_cc.xml',
		  width: 710,
		  height: 279,
			autostart: 'true',
			backcolor: '0x000000',
			frontcolor: '0xCCCCCC'
		});
		return false;
	});
	
	$("#undergradWhy .videoLink a").click(function(){
		$("#undergradWhy .paneLead").hide();
		$("#undergradWhy .paneVideo").show();
		$('#vid2').biolavideoplayer({
			file: 'http://biolaedu.web.aplus.net/video/why_biola/why_4_impact.mp4',
			fallback: 'http://biolaedu.web.aplus.net/video/why_biola/why_4_impact.flv',
			image: '/undergrad/images/why/preview_impacting_the_world.jpg',
			captions: 'http://biolaedu.web.aplus.net/video/why_biola/why_4_impact_cc.xml',
			autostart: 'true',
			backcolor: '0x000000',
			frontcolor: '0xCCCCCC'			
		});		
		return false;
	});
	
});


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;
		// 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", "<<").click(function(){
			slidePrev(); return false; 
		});
		slides.each(function(i) {
			createNavItem("", i + 1).click(function(){ 
				slideTo(i); return false; 
			});
		});
		createNavItem("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");
			activeSlide = id;
			hilightNav(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");
		}
	};
})(jQuery);
