// Google Video.js for PSPTube
// Made By JK108

function GoogleVideo_CheckURL( url, option )
{
	if (url.match("http://video.google.com/videofile/") == "http://video.google.com/videofile/"){
		return 1;
	} else{
		return 0;
	}
}

/*
 Get a GoogleVideo URL
*/
function GoogleVideo_GetURL( url, option )
{
	return url;
}

function GoogleVideo_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;

	
	if(result.start < 10){
	var contents = GetContents( "http://video.google.co.uk/videosearch?hl=en&q=" + keyword.replace(/ /g, "%20") + "+site%3Avideo.google.com&__q=" + keyword.replace(/ /g, "%20") + "&num=10" );
	} else{
	var num = result.start - 1;
	var contents = GetContents( "http://video.google.co.uk/videosearch?hl=en&q=" + keyword.replace(/ /g, "%20") + "+site%3Avideo.google.com&__q=" + keyword.replace(/ /g, "%20") + "&num=10" + "&start=" + num );
	}
	if(contents == null) { return null; /* failed to get page */ }


	if(contents.match("</strong> did not return any results.<br/><br/>") == "</strong> did not return any results.<br/><br/>"){result.total = 0; return result;}
	var total = contents.match(/\<\/b\> of about \<b\>(.*?)\<\/b\> for \<b\>/);
	var total = total[1].replace(/,/g, "");
	result.total = total;


	while(1){
	var pos = contents.indexOf("<div class=\"rl-item\">");

	if(pos != -1){
		contents = contents.substring(pos + 21, contents.length);
		//Thumbnail
		var thumb_url = contents.match(/\" src\=\"(.*?)\">/);
		var thumb_url = thumb_url[1];
		//Get URL
		var url = contents.match(/\<a href\=\"\/videoplay\?docid\=(.*?)&ei/);
		var url = url[1];
		//Get Title
		var title = contents.match(/\" target\=\"_top\">\n(.*?)\n<\/a>/);
		var title = removeUnidentifiedChars(ImproveText(title[1]));
		var abbtitle = removeDiacritics(title.replace(/ /g, "").replace(/\./g, "").replace(/-/g, "").replace(/\!/g, "").replace(/\?/g, ""));
		if(abbtitle.length > 20){abbtitle = abbtitle.substring(0, 20);}
		url = "http://video.google.com/videofile/" + abbtitle + ".flv?docid=" + String(url) + "&itag=5";
		//Length
		pos = contents.indexOf("<div class=\"description\" style=\"display: none\">");
		var length = contents.substring(pos + 47, contents.length);
		pos = length.indexOf("\n");
		var length = length.substring(pos + 1, length.length);
		pos = length.indexOf("\s");
		var length = TimestringToSeconds(length.substring(0, pos));
		//Rating
		var rating = contents.match(/<span class\=\"rl-stars\" ratings\=\"(.*?)\"><\/span>/);
		var rating = parseFloat(rating[1]);
		//Votes
		var votes = contents.match(/<span class\=\"rl-votes\">\n\((.*?) Ratin(.*?)\)\n<\/span>/);
		var votes = parseInt(votes[1]);
		//Description
		var description = contents.match(/<div class\=\"rl-snippet\">\n(.*?)\n<\/div>/);
		var description = removeUnidentifiedChars(ImproveText(description[1]));
		if(description == null || description == ""){description = title;}
		description = splitTo2Lines(description, 60);

		info = new Object();
		info.ThumbnailURL = thumb_url;
		info.URL = url;
		info.Title = title;
		info.LengthSeconds = length;
		info.RatingAvg = rating;
		info.RatingCount = votes;
		info.Description = description;
		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 != 10){result.total = result.end;}

	return result;
}

// Service metadata
var GoogleVideo = new Object();
GoogleVideo.Name          = "Google Video";		/* Service name */
GoogleVideo.Description   = "Google Video";		/* Service description */
GoogleVideo.SearchDesc    = "Google Video";		/* Service search screen description */
GoogleVideo.SearchOSKMode = 1;				/* OSK mode 1: English only  2: Japanese */
GoogleVideo.CheckURL      = GoogleVideo_CheckURL;	/* CheckURL function */
GoogleVideo.GetURL        = GoogleVideo_GetURL;		/* GetURL function */
GoogleVideo.Search        = GoogleVideo_Search;		/* Search function */

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