$(function(){
	getVideos("health news");
});
function getVideos(q){
	$("#gvi").html("<td colspan='6' align='center' >Loading Videos</td>");
	$.getJSON("http://ajax.googleapis.com/ajax/services/search/video?q="+encodeURIComponent(q)+"&v=1.0&rsz=6&key=ABQIAAAAGxnU77fcAA0dMCeqshPCVBQIY4PfspeyXsn7UP2f9ugP3KMZ7hRgoQ_rIOj9B4sFi442LQxWHP3RNA&callback=?",function(data){
		if(data.responseStatus==200){
			var results=data.responseData.results;
			if(results.length > 0){
				$("#gvi").empty();
				for(i=0;i<results.length;i++){
					
					$("#gvi").append("<td align='left' valign='middle'><img src='"+results[i].tbUrl+"' plurl='"+unescape(results[i].url)+"' width='138' height='82' style='border:1px solid #999;'/></td>");
					
				}
				//$("<tr id='gvhlp'><td colspan='5' align='center' >Click to play a video.</td></tr>").insertAfter("#gvi");
				 $("#gvi td img").each(function(){
					  var videoID = $(this).attr('plurl').match(/watch\?v=(.+)+/);
					  videoID = videoID[1];
					  $(this).qtip({
						 content: '<div id="youtube-embed-'+videoID+'">You need Flash player 8+ to view this video.</div>',
						 position: {corner: {tooltip: 'bottomMiddle', target: 'topMiddle'}},
						 show: {when:'click', solo: true},
						 hide: 'unfocus', 
						 style: {width: 425,height: 264,padding: 0,tip: true,name: 'dark'},
						 api: {
							onRender: function(){
							   var params = { allowScriptAccess: 'always', allowfullScreen: 'false' };
							   var attrs = { id: 'youtube-video-'+videoID };
							    swfobject.embedSWF('http://www.youtube.com/v/'+videoID+'&enablejsapi=1&playerapiid=youtube-api-'+videoID,
												 'youtube-embed-'+videoID, '425', '264', '8', null, null, params, attrs);
							

							},
							onHide: function(){
							   var playerAPI = this.elements.content.find('#youtube-video-'+videoID).get(0);
							   if(playerAPI && playerAPI.pauseVideo) playerAPI.pauseVideo();
							}
						 }
					 });
				});
			}
		}
	});
}


