/* jquery YouTube grabber, from systemseven (http://www.systemsevendesigns.com) released on blog : webdevkungfu.com 
   Edited and repurposed
*/
(function($){
	$.fn.youTube = function(options) {
		var defaults = {
			videoHeader: null			/* header to display above video list*/
			,results : 50				/* number of results to return / display */
			,orderBy : null				/* what to order the results by, if null, results display if the order they appear on the playlist */
			,query  : null				/* query to run against youTube videos */
			,author : null				/* author of the video */
			,showRatings : true			/* Display the rating stars assigns a class of 00,05,10,15,20,25,30,35,40,45,50 */
			,showNumRatings : true		/* Displays the actual number of ratings */
			,showViews : false			/* show number of views for the video */
			,showDescription : true		/* show a clipped version of the video description */
			,descriptionLength : 150	/* length of the description to bring back, will append ... to the end */
			,thumbnail : 'small'		/* small: 120x90 | large: 480 x 360 */
			,baseurl : '?v='
			,onComplete : true
			,pagination : true			/* turns on/off the pagination */
			,numberPerPage : 3			/* displays a paginated list of pages */
			,currentPlay : 'null'		/* Checks current playing item to find attached title and description */
		};
		
		options = $.extend(defaults, options);
		
		return this.each(function(){
			var container = $(this);
			requestYouTubeVideos();
			
			function requestYouTubeVideos() {
				var requestUrl = 'http://gdata.youtube.com/feeds/api/videos?alt=json&max-results='+options.results;
				if(options.author != null){
					requestUrl += '&author='+options.author;
				}
				if(options.orderBy != null){
					requestUrl += '&orderby='+options.orderBy;
				}
				if(options.query != null){
					requestUrl += '&q='+options.query;
				}
				
				$.ajax({
					type: "GET",
					url: requestUrl+"&callback=?",
					dataType: 'jsonp',
					success: onYouTubeSuccess,
					error: onYouTubeError
				})
			}
			
			function onYouTubeSuccess(result){
				var feed = result.feed;
				var rfeed = feed.entry || [];
				var relVideos = [];
				if(rfeed.length > 0){
					$(rfeed).each(function(i) {
						relVideos[i] = [];
						relVideos[i].id = stripFeature(rfeed[i].link[0].href.substring(rfeed[i].link[0].href.indexOf('=')+1,rfeed[i].link[0].href.length));
						relVideos[i].url = rfeed[i].link[0].href.match("[\\?&]v=([^&#]*)");
						relVideos[i].title = rfeed[i].title.$t;
						var m = Math.floor(rfeed[i].media$group.yt$duration.seconds / 60);
						var s = rfeed[i].media$group.yt$duration.seconds % 60;
						if(s<10){
							s = '0'+s;
						}
						relVideos[i].duration = m+':'+ s;
						if (options.thumbnail == 'large') {
							var index = rfeed[i].media$group.media$thumbnail.length-1
						}else{
							var index = 0;
						}
						relVideos[i].thumbnail = rfeed[i].media$group.media$thumbnail[index].url;
						relVideos[i].description = rfeed[i].media$group.media$description.$t;
						relVideos[i].author = rfeed[i].author[0].name.$t;

						if(rfeed[i].yt$statistics){
							relVideos[i].views = rfeed[i].yt$statistics.viewCount;
						}
						else if(!rfeed[i].yt$statistics){
							relVideos[i].views = '0';
						}
						if(rfeed[i].gd$rating){
							relVideos[i].rating = rfeed[i].gd$rating.average;
							relVideos[i].numRaters = rfeed[i].gd$rating.numRaters;
						}
						else if(!rfeed[i].gd$rating){
							relVideos[i].rating = '0.00';
							relVideos[i].numRaters = '0';
						}
					}).ready(function(){
						/* relVideos.sort(arraySort); */
						if (relVideos.length > 0) {
							var page_count = 0;
							container.append('<div id="youtube-page'+page_count+'" class="ypage">');
							var current_page = $('#youtube-page'+page_count);
							$(relVideos).each(function(i){

								if(relVideos[i].id == options.currentPlay){
									$("#title").append(relVideos[i].title);
									$("#desc").append(relVideos[i].description);
								}
								
								numRatings = (relVideos[i].numRaters != 1) ? 'ratings' : 'rating';
								numStars = getStarRating(relVideos[i].rating);
								
								current_page.append('<div class="video-item">');
								videoItem = current_page.find('.video-item:last');
								
								videoItem.append('<div class="video-thumb"><a href="'+options.baseurl+relVideos[i].id+'" title="'+relVideos[i].title+'"><img src="' + relVideos[i].thumbnail + '" alt="' + relVideos[i].title + '" /></a><span class="duration">'+relVideos[i].duration+'</span></div>');
								
								videoItem.append('<div class="video-info">');
								videoInfo = videoItem.find('.video-info');
								videoInfo.append('<a href="'+options.baseurl+relVideos[i].id+'" title="'+relVideos[i].title+'" class="video-title">' + relVideos[i].title +'</a>');
								if(options.showDescription){
									videoInfo.append('<span class="video-desc">'+setDescription(relVideos[i].description)+'</span>');
								}
								if (options.showRatings) {
									videoInfo.append('<div class="video-rating-'+numStars+'"></div>');
								}
								if (options.showNumRatings){
									videoInfo.append('<div class="video-num-ratings">'+relVideos[i].numRaters+' ratings</div>');
								}
								if (options.showViews) {
									videoInfo.append('<p class="video-num-views">' + relVideos[i].views + ' views</p>');
								}
								
								// make the page
								if(options.pagination){
									if(((i+1)%options.numberPerPage) == 0 && i != (relVideos.length)+1){
										page_count++;
										// create a new 
										container.append('<div id="youtube-page'+page_count+'" class="ypage">');
										current_page = $('#youtube-page'+page_count);
									}
								}
							});
							if (options.linkAction == 'modal') {
								$('.modalvideo').videoDialog({});
							}
						}
					});
				}else{
					/* if we have no youtube videos returned, let's hide the container */
					container.hide();
				}
				/*if(typeof callbackFnk == '')    */
				if(options.onComplete != null){
					if($.isFunction){
						$(function(){
							youTubeComplete();
						});
					}
				}
			}
			
			function onYouTubeError(result){
				container.append('<p>Error connecting to YouTube</p>');
			}
			
			function setDescription(desc){
				if(desc.length > options.descriptionLength){
				return desc.substring(0,options.descriptionLength) + '...'
				}else{
				return desc;
				}
			}      
			
			/*
			function arraySort(a,b){
				if (a.title < b.title){ 
					return -1; 
				}
				else if (a.title > b.title){
					return 1; 
				}
				else{
					return 0;
				}
			}
			*/
			
			function stripFeature(vidID){
				var featureLoc = vidID.indexOf('&feature=youtube_gdata');
				if(featureLoc >= 0){
					return vidID.substring(0,featureLoc);
				} else {
					return vidID;  
				}
			}
			
			function getStarRating(rating){
				if(		 rating >= 5.00					  )	{ return '50'; }
				else if( rating >= 4.50 && rating <= 4.99 )	{ return '45'; }
				else if( rating >= 4.00 && rating <= 4.49 )	{ return '40'; }
				else if( rating >= 3.50 && rating <= 3.99 )	{ return '35'; }
				else if( rating >= 3.00 && rating <= 3.49 )	{ return '30'; }
				else if( rating >= 2.50 && rating <= 2.99 )	{ return '25'; }
				else if( rating >= 2.00 && rating <= 2.49 )	{ return '20'; }
				else if( rating >= 1.50 && rating <= 1.99 )	{ return '15'; }
				else if( rating >= 1.00 && rating <= 1.49 )	{ return '10'; }
				else if( rating >= 0.50 && rating <= 0.99 )	{ return '05'; }
				else if( rating == 0.00					  )	{ return '00'; }
			}
			
		});
	};
})(jQuery);
