
/*
 * functionality: insert a flowplayer 
 * 	in a specific place on a page
 *  set the cue points
 *  toggle show/hide the transcript
 * 
 */




jQuery.fn.setFlowPlayer = function (opts){
	var cuePoints = opts.chapters || "";
	var cueThePointies = new Array();
	var transcriptplaying = opts.playmarker || "";
	var divid = opts.divid || "flowplayer";
	var swffile = opts.swf || "http://www.darwinproject.ac.uk/wp-content/plugins/flowplayer/flowplayer-3.1.5.swf";
	var transcriptmarker = opts.transcriptmarker || "ol#flowplaytranscript";
	var transcriptchildmarker = opts.transcriptchildmarker || "li";
	var transcripttoggle = opts.transcripttoggle || "div.flowplayertoggle";
	var transcripttoggletextless = opts.transcripttoggletextless || "[Hide Transcripts]";
	var transcripttoggletextmore = opts.transcripttoggletextmore || "[Show Transcripts]";
	var transcriptchpter = opts.transcriptchpter || "div h5";
	var flowplayerbarmsg = opts.flowplayerbarmsg || "#FlowPlayerBar";
	var flowplayerbarnext = opts.flowplayerbarnext || "#spkVidPlayNext";
	var flowplayerbarprev = opts.flowplayerbarprev || "#spkVidPlayPrevious";

	var lighttpdurl = opts.lighttpdurl || 'http://www.darwinproject.ac.uk/wp-content/plugins/flowplayer/flowplayer.pseudostreaming-3.1.3.swf';
	var controlurl = opts.controlurl || 'http://www.darwinproject.ac.uk/wp-content/plugins/flowplayer/flowplayer.controls-3.1.5.swf';
	var controlbackgroundColor= opts.controlbackgroundColor || '#17A93E';
	var controlbackgroundGradient= opts.controlbackgroundGradient || [0,0];
	var controltimeFontColor= opts.controltimeFontColor || '#FFFFFF';
	var controldurationColor= opts.controldurationColor || '#FFFFFF';
	var controlbuttonColor= opts.controlbuttonColor || '#0F7029';
	var controlbuttonOverColor= opts.controlbuttonOverColor || '#0F7029';
	var controlbufferColor= opts.controlbufferColor || '#0F7029';
	var controlfullscreen= opts.controlfullscreen || true;

	/* use jQuery instead of $ as something in the audio files in interviews does bad things otherwise */
	/* also wait until the document has loaded before initialising anything */
	jQuery(document).ready(function(){
		$(transcripttoggle).setToggler(
			 {
			 	moretext: transcripttoggletextmore,
			 	lesstext: transcripttoggletextless,
			        isbutton:false 
			}
		);
	
		if(cuePoints){
			cueThePointies = cuePoints.split(',');
		}
	
		var chapterNumbers = new Array();
		for( var i in cueThePointies){
			var temp = cueThePointies[i] * 1000; // change sec to milliseconds
			cueThePointies[i] = temp;
			chapterNumbers[temp] = (i/1) + 1; //(make chapter numbers start at 1)
		}
		
		var player = flowplayer(divid, 
	    	    {
	    	        src: swffile,
	    	        backgroundColor:"#ff0000",
	    	        showFullScreenButton: true
	    	    },
	    	    {
	    	    	// configure clip to use "lighthttpd" plugin for providing video data
	    	    	clip: {
	    		    	provider: 'lighttpd',
	        		    // Define cuepoints:
	        		    onCuepoint: [
	        		                 // each number represents seconds in the timeline 
	        		                 cueThePointies,
	        		                 //[1,10],
	        		                 // this function is triggered when each cuepoint is entered 
	        		                 function(clip, cuepoint) {
	        		                	 //set generic chapterNum variable
	        		                	 this.chapterNum = chapterNumbers[cuepoint];
	        		                	 
	        		                	 //update chapter numbers in bar and transcripts
	        		                	 updateBar(this.chapterNum);
	        		                	} 
	        		                 ]
	    	    	},

	    	        // disable default controls 
	    	        plugins: {	
	        		    lighttpd: {
	    		        url: lighttpdurl
	    		    	},
	    	    		controls: {
    	                	url: controlurl,
    	                	backgroundColor: controlbackgroundColor,
    	                	backgroundGradient: controlbackgroundGradient,
    	                	timeFontColor: controltimeFontColor,
    	                	durationColor: controldurationColor,
    	                	buttonColor: controlbuttonColor,
    	                	buttonOverColor: controlbuttonOverColor,
    	                	bufferColor: controlbufferColor,
    	                	fullscreen: controlfullscreen
    	                	
    	            	}
    	           } 
	        	}
	        ); 
			
		/* Update chapter bar if it exists */
		function updateBar(chapterNum){
			player.chapterNum = chapterNum;
			if($(flowplayerbarmsg)){
				$(flowplayerbarmsg).html("Current chapter:  " + chapterNum + " out of " + cueThePointies.length + ".");
			}
			var genstyle = transcriptmarker+" "+transcriptchildmarker;
			var style = genstyle+":nth-child("+ chapterNum +")";
   			var bkgdimg = "url("+transcriptplaying+")"
   			if($(style)){//style the transcript
   				$(genstyle).css("background-image",""); // clear all previous
   				$(style).css("background-image",bkgdimg);
   				$(style).css("background-repeat","no-repeat");
   			}
		}
		/* link titles in the transcripts to the chapters */
		var style = transcriptmarker+" "+transcriptchildmarker;
		var chaptermarker = style+" "+transcriptchpter;
		$(chaptermarker).each(function(index) {
			if(index < cueThePointies.length){
			$(this).click(
					function(){
						var cuepoint = cueThePointies[index]/1000;
						var chapterNum = index/1 + 1;
						player.seek(cuepoint); // move the movie to the timestamp
           			 	updateBar(chapterNum);
						
					}
			);
			$(this).mouseover(
					function (){
						$(this).css("text-decoration","underline");
					}
			    );
			$(this).mouseout(
			    	function (){
			    		$(this).css("text-decoration","none");
			    	}
			    );
			}
		});
		
		/* add prev/next links in controlbar (If there) */
		jQuery(flowplayerbarprev).click(
	    		function(){
	    			if(player.chapterNum>=1){
	    				chapter = (player.chapterNum/1) - 1;
	    			}
	    			else{
	    				chapter = 1;
	    			}
	    			index = (chapter/1) -1;
    				player.seek(cueThePointies[index]/1000);
       			 	updateBar(chapter);
	    		}
	    	);
	    jQuery(flowplayerbarnext).click(
	    		function(){
	    			if(player.chapterNum < cueThePointies.length){
	    				chapter = (player.chapterNum/1) + 1;
	    			}
	    			else{
	    				chapter = cueThePointies.length;
	    			}
	    			index = (chapter/1) -1;
    				player.seek(cueThePointies[index]/1000);
       			 	updateBar(chapter);
	    		}
	    	)
		
	});
	
};
