function YouTubeSearch(q){
	if(q != ''){
		'http://gdata.youtube.com/feeds/api/videosvq=' + q + '&max-results=10&alt=json';
		var	url	=	'./json/remote.php?url='+Base64.encode('http://gdata.youtube.com/feeds/api/videos?vq=' + q + '&max-results=10&alt=json');
	} else{
		var	url	=	'./json/remote.php?url='+Base64.encode('http://gdata.youtube.com/feeds/api/videos?max-results=10&alt=json');
	}
	YouTubeVideos(url);
}
function YouTubeVideos(url){
	if(url != ""){
		$.getJSON(url, function(json){
				//Process JSON Data
					//Empty Result Box
					$("#SearchResults > *").remove();
					//Doument Title
					document.title	=	'YouTube Downloader - Search "'+ document.getElementById('q').value +'"';
				//Loop Through ALl Results
				$.each(json.feed.entry, function(i, entry){
					//Access to each individual result
					//alert(entry.title.$t);
						//Build HTML For Result
						var html;
						var video_id	=	entry.link[0].href.replace('http://www.youtube.com/watch?v=', '');
							//str_replace('http://www.youtube.com/watch?v003d', '', entry.link[0].href);
						html =	'<div class="Result" onclick="ShowVideo(\''+video_id+'\')">';
						html+=	'<div class="Icon"><img src="./icon.php?video_id='+video_id+'&thumb=1" width="130" height="97" /></div>';
						html+=	'<h3 class="Title">'+entry.title.$t+'</h3>';
						html+=	'<p class="Content">'+entry.content.$t+'</p>';
						//html+=	'<p class="Links"><span class="Link" onclick="WatchVideoOverlay(\''+video_id+'\')">'+video_id+'</span><span class="Link"></span></p>';
						html+=	'</div>';
					$("#SearchResults").hide();
					$("#SearchResults").append(html);
					//Append Search Entry to #SearchResults
					}
				);//End Loop through results
					//Show Result Box
					$("#VideoBox > *").remove();
					$("#VideoBox").hide();
					$("#SearchResults").slideDown("slow");
			}//End JSON Processing
		);
	} else{
		alert("Error: Invalid Query URL");
	}
}
function ShowVideo(video_id){
	if(video_id	==	""){
		alert("Error: Invalid Video ID");
	} else{
		//Play Video In Overlay
			//$("#VideoBox").hide();
		//Get Video Info
			var url	=	'./json/remote.php?url='+Base64.encode('http://gdata.youtube.com/feeds/api/videos/'+video_id+'?alt=json');
			$.getJSON(url, function(json){
					var	video;
					//Doument Title
					document.title	=	'YouTube Downloader - Watching "'+json.entry.title.$t+'"';
					video =	'<div id="BackToResults" onclick="$(\'#VideoBox\').hide();$(\'VideoBox > *\').remove();$(\'#SearchResults\').slideDown();document.title=\'YouTube Downloader - Search\'">Back to Search Results</div>';
					video+=	'<h3>'+json.entry.title.$t+'</h3>';
					video+=	'<object width="425" height="344">';
					video+=	'<param name="movie" value="http://youtube.com/swf/l.swf?video_id='+video_id+'&rel=1"></param>';
					video+=	'<param name="wmode" value="transparent"></param>';
					video+=	'<embed src="http://youtube.com/swf/l.swf?video_id='+video_id+'&rel=1" type="application/x-shockwave-flash" wmode="transparent" width="425" height="344"></embed>';
					video+=	'</object>';
					video+=	'<div>'+json.entry.content.$t+'</div>';
					video+=	'<ul>';
					video+=	'<li><strong>Views:</strong> '+json.entry.yt$statistics.viewCount+'</li>';
					video+=	'</ul>';
					video+=	'<div id="DownloadLink" onclick="DownloadVideo(\''+video_id+'\')">Download</div>';
						//Hide Search Results
						$("#SearchResults").slideUp();
					$("#VideoBox").html(video);
					$("#VideoBox").show();
				//jQuery.facebox(video);
				}
			);//End JSON Query
	}
}
function DownloadVideo(video_id){
	if(video_id	==	""){
		alert("Error: Invalid Video ID");
	} else{
		//Get Video Info
			var url	=	'./json/download.php?video_id='+video_id;
			$.getJSON(url, function(json){
					//Check JSON has No Error
					if(json.status	==	"200"){
						var	video;
						video =	'<h3>'+json.title+'</h3>';
						video+=	'<div id="DownloadIcon"><img src="./icon.php?video_id='+json.video_id+'&thumb=1" width="130" height="97" /></div>';
						video+=	'<ul>';
						//Loop through all download links
						$.each(json.download, function(i, download){
								video+=	'<li><a href="'+download.url+'">'+download.description+'</li>';
							}
						);
						//video+=	'<li><a href="'+json.url+'">Flash Video (.flv)</a></li>';
						video+=	'</ul>';
						jQuery.facebox(video);
					} else{
						//JSON Error
						alert("Error Code: "+json.status);
					}
				}
			);//End JSON Query
	}
}

function LoadingImage(id){
	$("#"+id).slideUp("slow", function(){
	   	$("#"+id).html("<div style=\"text-align:center;\"><img src=\"./images/loading.gif\" /></div>");
		$("#"+id).slideDown();
	});
	
}

jQuery(document).ready(function($) {
  $('a[rel*=facebox]').facebox()
}) 