//Begin of JQuery code to pull in You Tube feedhttp://www.youtube.com/user/PHEgov#grid/user/A05C8BBFC398C518

$(document).ready(function() {            
	$.jGFeed('http://gdata.youtube.com/feeds/api/playlists/18EC132C295AFCE9?v=2',
	function(feeds){
		// Check for errors
		if(!feeds){
			// there was an error
			return false;
		}

		var html = '<div id="showVids_0">';

		// Writes the HTML to the designated div "#slideshowVideos"		
		for(var i=0; i<feeds.entries.length; i++){
			
			var entry = feeds.entries[i];

			// Adds a div around ever 3 videos to work with jQuery Cycle plugin
			if ((i) %3 == 0 && i != 0) {
				html += '</div> <div id="showVids_'+i+'" class="fullWidth">'
			}
			// HTML added to div that displays Videos
			html += '<div class="videoWrapper"><a href="' + entry.link + '" class="youTube" title="'+ entry.title +'"><img src="' + $.jYoutube(entry.link, 'large') + '" alt="'+ entry.title +'" /></a><h4>'+ entry.title +'</h4></div>';

		}
		html +='</div>';
		
		$('#slideshowVideos').html(html); // Outputs code
		$('.totalCount').html(i);
		
		// Fancybox code to display Youtube video popup
		$("a.youTube").click(function() {
			var ytVideo =this.href.replace(new RegExp("http://www.youtube.com/watch\\?v=", "i"), ""); 
			ytVideo= ytVideo.replace("&feature=youtube_gdata", "");
			
			$.fancybox({
					'padding'		: 0,
					'autoScale'		: false,
					'transitionIn'	: 'none',
					'transitionOut'	: 'none',
					'title'			: this.title,
					'width'			: 854,
					'height'		: 480,
					'centerOnScroll': true,
					'overlayColor': "#000",
					'href'			: 'flash/YoutubeAPI.swf',
					//'href' : 'player.swf?file=/uploadz/test.mp3&autostart=true',
					'type'			: 'swf',					
					'swf'			: {
										'wmode'		: 'transparent',
										'allowfullscreen'	: 'true',
										'flashvars' :'ytSwf=http://www.youtube.com/apiplayer?version=3&ytVideo='+ytVideo+'&ytTitle='+this.title+''
									  }
				});
				return false;
			});	
			// jQuery Cycle function
			$(function() {		
			$('#slideshowVideos').cycle({
				fx:      'scrollHorz',
				timeout:  0,
				prev:    '.prevVid',
				next:    '.nextVid',
				pager:   '#navVid',
				cleartype:  1,
				cleartypeNoBg:  true,
				after: onAfter
			});
			function onAfter(curr,next,opts) {
				var caption = (1 + opts.currSlide + opts.currSlide + opts.currSlide);
				$('.onVideo').html(caption);
				var index = opts.currSlide;
				$('.prevVid')[index == 0 ? 'hide' : 'show']();
				$('.nextVid')[index == opts.slideCount - 1 ? 'hide' : 'show']();
			}
		});		
		
	}, 22);
});
