/*
 *	jQuery YouTube Feed
 *	by Thrive Mediarts
 *	rev 20100929
 *	
 *	Based on jQuery YouTube grabber, from systemseven (http://www.systemsevendesigns.com), released on blog: webdevkungfu.com.
 */
 
(function($){
	$.fn.youTube = function(options) {
		var defaults = {
			results   : 3,
			query     : null,
			thumbnail : 'small'
		};
		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.query != null){
					requestUrl += '&q='+options.query;
				}
				$.ajax({
				    type: 'GET',
				    url: requestUrl,
				    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;
						relVideos[i].title = rfeed[i].title.$t;
						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].views = '0';
						relVideos[i].rating = '0.00';
						relVideos[i].numRaters = '0';
						
						  }).ready(function(){
								relVideos.sort(arraySort);
								if (relVideos.length > 0) {
									$(relVideos).each(function(i){
										container.append('<div class="video-item">');
										videoItem = container.find('.video-item:last');
										videoItem.append('<div class="video-thumb" style="background:url('+relVideos[i].thumbnail+');"><a href="'+relVideos[i].url+'&q=san+diego+junior+theatre" target="_blank" title="'+relVideos[i].title+'"><img src="http://juniortheatre.com/images/social/play-video.png" width="110" height="83" border="0" alt="' + relVideos[i].description + '" /></a></div>');
									});
									if (options.linkAction == 'modal') {
										$('.modalvideo').videoDialog({});
									}
								}
						  });							
						}else{
							/* if we have no youtube videos returned, let's hide the container */
							container.hide();
						}
						
			}
			
			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;	
				}
			}

			
		});
	};
})(jQuery);
