var page = 1;
var query = "Pacific+Biosciences";
function jasoncalling(query,maxresults,startindex)
{
	//var feedUrl = 'http://gdata.youtube.com/feeds/api/users/PacificBiosciences/uploads?alt=json-in-script&callback=?&orderby=published&max-results='+maxresults+'&start-index='+startindex;
	var feedUrl = 'https://gdata.youtube.com/feeds/api/playlists/5C287C1600A28C16?alt=json-in-script&callback=?';
	
	$.getJSON(feedUrl, function(data) 
	{ 
		var videoloop = 0;
		$.each(data.feed.entry, function(i, item) 
		{

			if(videoloop == 0)
			{
				$('#video'+videoloop).addClass('thumbnailDiv');				
			}
			else
			{
				$('#video'+videoloop).addClass('thumbnailDivWithRule');	
			}	
					
			//$('#video'+videoloop).html(setThumbnailVideoPane(item));
			//if(videoloop == $(data.feed.entry).size()-1)

			videoloop++;
		});
    }); 
}

function getVideoLink(item)
{
	return 'http://www.youtube.com/v/'+getVideoId(item) + '?wmode=opaque&version=3&autohide=1&modestbranding=1&showsearch=0&hl=en_GB&fs=1';
}

function getVideoId(item)
{
	var video = item['id']['$t'];
	//var videoId = video.replace('http://gdata.youtube.com/feeds/api/videos/','');
	//if(videoId.indexOf('/') > -1)
//	{
		var videoImg = item['media$group']['media$thumbnail'];
		var videoId = videoImg[0].url;
		videoId = videoId.replace('http://i.ytimg.com/vi/','');
		videoId = videoId.replace('/0.jpg','');
	//}
	return videoId;
}


function setThumbnailVideoPane(item)
{
	var videoTitle = item['title']['$t'];
	var videoId = getVideoId(item);
	var videoDesc = item['content']['$t'];
	var videoDescTrunc = videoDesc;
	
	if(videoDescTrunc.length > 80)
	{
		videoDescTrunc = videoDesc.substring(0,60) + '...';
	}
	var videoStat = item['media$group']['media$content'];
	var videoImg = item['media$group']['media$thumbnail'];
	var videoDurationMin = Math.round(videoStat[0].duration/60)-1;
	var videoDurationSec = videoStat[0].duration % 60;
	if(videoDurationSec < 10)
	{
		videoDurationSec += '0';
	}
	var videoDuration = videoDurationMin + ':' + videoDurationSec;
	var divString = '<div style="cursor:pointer;" title="'+videoTitle+'" desc="'+escape(videoDesc)+'" name="'+getVideoLink(item)+'" id="videopics">';
	divString += '<table><tr><td><a target="top" href="/video-gallery/index.html?vid='+getVideoId(item)+'">';
	divString += '<div class="photooverlay">';		
	divString += '<div class="duration_block"><div class="caption"><div class="subcaption">Duration:</div>'+videoDuration+'</div></div>';  
	divString += '<img src="'+videoImg[0].url+'" align=left" width="90px" height="70px" id="videoslistpics" />';
	divString += '</div>';
	divString += '</td>';
	divString += '<td><div class="videoCaptionTitle">';
	divString += videoTitle;
	divString += '</div><div class="videoCaptionText">';
	divString += videoDescTrunc;
	divString += '</div></a></td>';
	divString += '</table>';
	divString += '</div>';
	return divString;
}


$(document).ready(function()
{

	$('#errorbox').hide();
	$('#errorbox').click(function()
	{
		$(this).hide(1000);
	});
	
	jasoncalling(query,2,page);
	
	$('#submitsearch').click(function()
	{
		page = 1;
		query += '+'+$('#searchbox').val();
		if(query == '') 
		{
			$('#errorbox').show(1000).html('Please enter some search query');
		}
		else 
		{
			jasoncalling(query,22,page) 
		}
	});
	

  

	$('#exitbutton').click(function()
	{
		$('#apDiv1').html('');
		$('#apDiv1').css("background", "red;");
		  
		$('#apDiv6').html('Please search something');
		$('.videodiv').html('');
	});
  
});

