// YouTube.js for PSPTube
// Made By JK108

function YouTube_CheckURL( url, option )
{
	if (url.match("http://uk.youtube.com/watch") == "http://uk.youtube.com/watch"){
		return 1;
	} else{
		return 0;
	}
}

/*
 Get a YouTube URL
*/
function YouTube_GetURL( url, option )
{
	var id = url.match( /^http:\/\/uk\.youtube\.com\/watch\?v=(.*)/ );
	if(id == null) { return null; /* ID not valid */ }
	id = id[1];
	var urltoken = "http://www.youtube.com/api2_rest?method=youtube.videos.get_video_token&video_id=" + id;
        var contents = GetContents(urltoken);
	var token = contents.match( /<t>(.*)<\/t>/ );
       	var token = token[1];
       	var video_url = "http://youtube.com/get_video?video_id=" + id + "&t=" + token;
	if(PSPTube.YouTubeHighQuality == 1){
        	var contents = GetContents(url);
		if(contents == null) { return null; /* failed to get page */ }
		if(contents.match("return false;\">watch in HD</a>") != null ||
		   contents.match("\", \"fmt_map\": \"6/") != null ||
		   contents.match("\", \"fmt_map\": \"22/") != null){
			video_url += "&fmt=6";
		}
	}
	return video_url;
}

function YouTube_Search( keyword, start_index, length, option )
{
	var result = new Object();
        result.keyword   = keyword;	// Search
	result.VideoInfo = new Array();	// Results in sequence
	result.start     = start_index;	// Index of first result
	result.end       = 0;
	result.total     = -1;


	result.start = (((result.start - 1) / 10) * 20) + 1;


	if(result.start < 20){
	var contents = GetContents( "http://uk.youtube.com/results?search_query=" + keyword.replace(/ /g, "+") + "&search_type=&aq=f" );
	} else{
	var num = 1 + ((result.start - 1) / 20);
	var contents = GetContents( "http://uk.youtube.com/results?search_query=" + keyword.replace(/ /g, "+") + "&page=" + num );
	}
	if(contents == null) { return null; /* failed to get page */ }


	if(contents.match("No videos found for <span >") == "No videos found for <span >"){ result.total = 0; return result; }
	if(contents.match("</strong> of <strong>millions</strong>") == "</strong> of <strong>millions</strong>"){
	total = 1000000;
	} else{
	var pos = contents.indexOf('</strong> of about <strong>');
	contents = contents.substring(pos + 27, contents.length);
	pos = contents.indexOf("</strong>");
	var total = contents.substring(0, pos).replace(/,/g, "");
	}
	result.total = total;


	while(1){
	pos = contents.indexOf("<a id=\"video-url-");

	if(pos != -1){
		//Get URL
		contents = contents.substring(pos + 17, contents.length);
		pos = contents.indexOf("\"");
		var url = "http://uk.youtube.com/watch?v=" + contents.substring(0, pos);
		//Title
		pos = contents.indexOf(" title=\"");
		contents = contents.substring(pos + 8, contents.length);
		pos = contents.indexOf("\"");
		var title = ImproveText(contents.substring(0, pos));
		//Get Thumbnail
		pos = contents.indexOf(" src=\"");
		contents = contents.substring(pos + 6, contents.length);
		pos = contents.indexOf("\"");
		var thumb_url = contents.substring(0, pos);
		if(thumb_url.substring(thumb_url.length - 4, thumb_url.length) == ".gif"){
			pos = contents.indexOf(" thumb=\"");
			contents = contents.substring(pos + 8, contents.length);
			pos = contents.indexOf("\"");
			var thumb_url = contents.substring(0, pos);
		}
		//Length
		pos = contents.indexOf("<span id=\"video-run-time-");
		contents = contents.substring(pos + 25, contents.length);
		pos = contents.indexOf("\">");
		contents = contents.substring(pos + 2, contents.length);
		pos = contents.indexOf("</span>");
		var length = contents.substring(0, pos);
		var length = TimestringToSeconds(length);
		//Description
		pos = contents.indexOf("class=\"video-description\">");
		contents = contents.substring(pos + 32, contents.length);
		pos = contents.indexOf("\n");
		var description = ImproveText(contents.substring(0, pos));
		if(description == null || description == ""){description = title;}
		description = splitTo2Lines(description, 57);
		//Views
		pos = contents.indexOf("class=\"video-view-count\">");
		contents = contents.substring(pos + 25, contents.length);
		pos = contents.indexOf(" views");
		var views = parseInt(contents.substring(0, pos).replace(/,/g, ""));

		info = new Object();
		info.URL = url;
		info.ThumbnailURL = thumb_url;
		info.Title = title;
		info.Description = description;
		info.ViewCount = views;
		info.LengthSeconds = length;
		info.SaveFilename = ToFilename(title);
		info.attr = 7;
		result.VideoInfo.push( info );
		if(result.start + (result.VideoInfo.length - 1) == result.total){break;}
	} else{
		break;
	}
	}


	if(result.VideoInfo.length) {
		result.end = result.start + (result.VideoInfo.length - 1);
	} else {
		result.total = 0;	// no results.
	}

	if(result.VideoInfo.length != 20 && result.start != 1){result.total = result.end;}

	return result;
}

// Service metadata
var YouTube = new Object();
YouTube.Name          = "YouTube";		/* Service name */
YouTube.Description   = "YouTube";		/* Service description */
YouTube.SearchDesc    = "YouTube";		/* Service search screen description */
YouTube.SearchOSKMode = 1;			/* OSK mode 1: English only  2: Japanese */
YouTube.CheckURL      = YouTube_CheckURL;	/* CheckURL function */
YouTube.GetURL        = YouTube_GetURL;		/* GetURL function */
YouTube.Search        = YouTube_Search;		/* Search function */

// add YouTube to site list
SiteList.push( YouTube );
