jQuery.fn.ytplaylist = function(options) {
 
 
  return this.each(function() {
							
   		var selector = $(this);
		

//		
		
		//grab a WUTube id from a (clean, no querystring) url (thanks to http://jquery-howto.blogspot.com/2009/05/jyoutube-jquery-youtube-thumbnail.html)
		function WUvideoID(url) {
			var ytid = url.match("([^&#]*)");
//			var ytid = url;
			ytid = ytid[1];
			VideoDirectory = 'WU_web_videos/';
			videoId = VideoDirectory+ytid;
			//	window.alert(videoId);

			return videoId;

		};
	
		
		//load inital video
		$(document).ready(function() {
		var firstVid = selector.children("li:first-child").addClass("currentvideo").children("a").attr("href");
		firstVid = 'WU_web_videos/'+firstVid;
					//	window.alert(firstVid);
		
		//Flowplayer
		
		flowplayer("player", 
					{
						key: '#$a6c5180e048dbeadc33',
						src: "flowplayer/flowplayer.commercial-3.2.2.swf",
						wmode: 'opaque',
							logo: {
								url: 'flowplayer/logo_WU_videoplayer.png',
								fullscreenOnly: false,
								displayTime: 2000
							}
						
					}, 
					{
					clip: {
						url: firstVid,
						provider: 'rtmp',
						autoPlay: false, 
						autoBuffering: true
						},
						
					plugins: 
					{
						rtmp: 
						{ 
							url: 'flowplayer/flowplayer.rtmp-3.2.1.swf', 
//							netConnectionUrl: 'rtmp://10.7.10.154/westernu/WU_web_videos/',  // This is the rtmp url to the stream
//							netConnectionUrl: 'rtmp://weblist.westernu.edu/westernu/WU_web_videos/',  // This is the rtmp url to the stream
							netConnectionUrl: 'rtmp://webmedia.westernu.edu/westernu/WU_web_videos/',  // This is the rtmp url to the stream
							objectEncoding: 0
						}
					}  	
						
				// streaming plugins are configured normally under the plugins node
					/* Psuedo stream
					,
					plugins: 
						lighttpd: {
							url: 'flowplayer/flowplayer.pseudostreaming-3.2.2.swf'
						}
					}		
					*/
				/*	 Miary Re-direct after view	
					onFinish: function() { 
					window.location = "https://wsprod.westernu.edu/cc/telegifts/index.jsp"; 
					} 
				*/
					}
					);
		

//	flowplayer("player", "flowplayer/flowplayer-3.2.1.swf", firstVid);
																										  
								   
								   })
	//	document.write(firstVid);
	//	$("#"+options.holderId+"").html(flowplayer("player", "flowplayer/flowplayer-3.2.1.swf", WUvideoID($(firstVid).attr("href"))));
		
		//load video on request
		selector.children("li").children("a").click(function() {
			
				$("#"+options.holderId+"").html(flowplayer("player", {
				   src: "flowplayer/flowplayer.commercial-3.2.2.swf",
				   key: '#$a6c5180e048dbeadc33',
					 wmode: 'opaque'
				   }, 
				   
				    {
					clip: {
						url:  WUvideoID($(this).attr("href")),
						provider: 'rtmp',						
						autoPlay: false,
						autoBuffering: true
							},
						
					plugins: 
					{
						rtmp: 
						{ 
							url: 'flowplayer/flowplayer.rtmp-3.2.1.swf', 
//							netConnectionUrl: 'rtmp://10.7.10.154/westernu/WU_web_videos/',  // This is the rtmp url to the stream
//							netConnectionUrl: 'rtmp://weblist.westernu.edu/westernu/WU_web_videos/',  // This is the rtmp url to the stream
							netConnectionUrl: 'rtmp://webmedia.westernu.edu/westernu/WU_web_videos/',  // This is the rtmp url to the stream							
							objectEncoding: 0
						}
					}  
					}
				   
				  
				   
				   
				   ));
				
				$(this).parent().parent("ul").find("li.currentvideo").removeClass("currentvideo");
				$(this).parent("li").addClass("currentvideo");												 
			
			
			return false;  //stops the reloading
		});
		
				//Remove white space for images
				function white_space(image_name)
				{
					 return image_name.split(' ').join('');
				}
			
				//do we want thumbs with that?
				if(options.addThumbs) {
					
					selector.children().each(function(i){
													  
						var replacedText = $(this).text();
						
						var thumbUrl = WUvideoID($(this).children("a").attr("href"))+".png";
						
						
						$(this).children("a").empty().html("<img src='"+white_space(thumbUrl)+"' alt='"+replacedText+"' />"+replacedText).attr("title", replacedText);
						
					});	
					
				}
			
   
  });
 
};	
