(function($){
	
	$.fn.ytLoader = function(options) {
	
		var defaults = {
			username: 'nonface',
			playlistID: 'AC29AC05B61072D9',
			listContainerID: 'videoListContainer',
			playerContainerID: 'null',
			width: '600',
			height: '460',
			color1: '3d3d3d',
			color2: '3d3d3d',
			border: '0',
			autoPlay: '1'
		};
		
		var options = $.extend(defaults, options);
		
		return this.each(function(){
		
			//var ajaxSendRequestUrl = 'http://gdata.youtube.com/feeds/api/users/' + options.username + '/uploads?alt=json',
			var ajaxSendRequestUrl = 'http://gdata.youtube.com/feeds/api/playlists/' + options.playlistID + '?alt=json-in-script&callback=?',
				listContainer = $('#' + options.listContainerID),
				playerContainer = $('#' + options.playerContainerID),
				color1 = options.color1,
				color2 = options.color2,
				autoPlay = options.autoPlay,
				border = options.border;
			
			$.getJSON(ajaxSendRequestUrl, function(data){
										
				listContainer.append('<ul id="videoGallery"></ul>');
									
				$.each(data.feed.entry, function(i, item){
											
					var videoTitle = item.title.$t,
						videoURL = item.media$group.media$content[0].url,
						videoThumb = item.media$group.media$thumbnail[3].url,
						videoDescription = item.media$group.media$description.$t;
												
					listContainer.find('ul#videoGallery').append(
						'<li class="videoInfo">' +
						'	<ul>' +
						'		<li class="videoThumb"><a href="' + videoURL + '"><span class="playOverlay">Play Video</span><img src="' + videoThumb + '"</a></li>' +
						'		<li class="videoTitle"><a href="' + videoURL + '">' + videoTitle + '</a></li>' +
						'		<li class="videoDescription">' + videoDescription + '</li>' +
						'	</ul>' +
						'</li>'
					);
						
				});
										
				listContainer.find('a').click(handleVideoLinkClick);		
										
			});
			
			function handleVideoLinkClick(e){
				e.preventDefault();
				
				$('body').append(
					'<div class="videoModal">' +
					'	<iframe style="background: transparent; width: 100%; height: 100%;"/>' +
					'	<div class="modalBg">' +
					'	</div>' +
					'	<div class="loading"><p>Loading Video...</p></div>' +
					'	<div class="modalContent">' +
					'	</div>' +
					'</div>'
				);
				
				var target = $(e.target);
				
				if(target.is('a')){
					
					var	_title = target.text(),
						_url = target.attr('href'),
						videoWidth = options.width,
						videoHeight = options.height,
						modal = $('.videoModal'),
						modalContent = $('.modalContent'),
						embedCode = '<object width="' + videoWidth + '" height="' + videoHeight + '">'+
									'	<param name="movie" value="' + _url + '?color1=' + color1 + '&color2=' + color2 + '&border=' + border +'&autoplay=' + autoPlay + '"></param>'+
									'	<embed src="' + _url + '?color1=' + color1 + '&color2=' + color2 + '&border=' + border + '&autoplay=' + autoPlay + '"'+
									'		type="application/x-shockwave-flash" width="' + videoWidth + '" height="' + videoHeight + '">'+
									'	</embed>'+
									'</object>';
	
						modalContent.height(videoHeight).width(videoWidth).css('margin-top' , '-' + videoHeight / 2 + 'px').css('margin-left' , '-' + videoWidth / 2 + 'px');
						modal.fadeIn('fast', function(){
							modalContent.append(embedCode).append('<a class="close" href="#">Close</a>');
							$('a.close').click(removeModal);
						});
				} else if (target.is('img') || target.is('span')){
					var	_url = target.parent().attr('href'),
						videoWidth = options.width,
						videoHeight = options.height,
						modal = $('.videoModal'),
						modalContent = $('.modalContent'),
						embedCode = '<object width="' + videoWidth + '" height="' + videoHeight + '">'+
									'	<param name="movie" value="' + _url + '?color1=' + color1 + '&color2=' + color2 + '&border=' + border +'&autoplay=' + autoPlay + '"></param>'+
									'	<embed src="' + _url + '?color1=' + color1 + '&color2=' + color2 + '&border=' + border + '&autoplay=' + autoPlay + '"'+
									'		type="application/x-shockwave-flash" width="' + videoWidth + '" height="' + videoHeight + '">'+
									'	</embed>'+
									'</object>';
	
						modalContent.height(videoHeight).width(videoWidth).css('margin-top' , '-' + videoHeight / 2 + 'px').css('margin-left' , '-' + videoWidth / 2 + 'px');
						modal.fadeIn('fast', function(){
							modalContent.append(embedCode).append('<a class="close" href="#">Close</a>');
							$('a.close').click(removeModal);
						});
				}
				
				$('.modalBg').click(function(){
					$('.videoModal, .videoModal a.close').fadeOut('fast').remove();
				});		
				// playerContainer.empty().append(embedCode);	
				
			};
			function removeModal(e) {
				e.preventDefault();
				
				$('.videoModal').fadeOut('fast').remove();
			};
			
		});
		
	};
	$.log = function(message) {
	  if(window.console) {
	     console.ytLoader(message);
	  } else {
	     alert(message);
	  }
	};
	
})(jQuery);
