// GoogleVideo.js for PSPTube20080224 (by FreePlay) -total and page number fix by zmathue

function GoogleVideo_CheckURL( url, option )
{
	return 1;
}

/*
 Get a GoogleVideo URL
*/
function GoogleVideo_GetURL( url, option )
{
	// THIS FUNCTION ISN'T EVEN *USED*.
	return url;
}

/*
 Video search
 
 keyword     : keyword to search
 start_index : starting index (based on 1)
 length      : number of videos to search for
 option      : The current search criteria, defaults to 0 (?)
 
 Return info
 
 .keyword                            : search string
 .total                              : total number of results
                                                -1 : error
                                                 0 : no results
                                                 >0: total results
 .start                              : starting number (based on 1)
 .end                                : ending number (based on 1)
 .VideoInfo                          : video sequence information
 .VideoInfo[n].Author                : Author (string)
 .VideoInfo[n].Title                 : Title (string)
 .VideoInfo[n].LengthSeconds         : LengthSeconds: total video playback time (in seconds) (integer)
 .VideoInfo[n].RatingAvg             : RatingAvg: rating (float)
 .VideoInfo[n].RatingCount           : RatingCount: number of votes (integer)
 .VideoInfo[n].Description           : Description (string)
 .VideoInfo[n].ViewCount             : ViewCount (integer)
 .VideoInfo[n].UploadTime            : UploadTime: Video upload time (UNIX style)
 .VideoInfo[n].CommentCount          : CommentCount (integer)
 .VideoInfo[n].MylistCount           : MylistCount: ? (integer)
 .VideoInfo[n].Tags                  : Tags: Space-delimited string of tags, e.g. "tag1 tag2 tag3"
 .VideoInfo[n].URL                   : URL: Link to viewing page (string)
 .VideoInfo[n].ThumbnailURL          : ThumbnailURL: URL to thumbnail (string)
 .VideoInfo[n].SaveFilename          : SaveFilename (string)
                                                "Movie deprecated set unique ID please."
 .VideoInfo [n]. Attr: Integer OR video attribute set to the following value
                                                 1: read-only (not delete)
                                                 2: download
                                                 4: Play is a need to connect to the network
                                                 Usually, 1 +2 +4, 7
                                                 MS is a directory, so one can not be removed
                                                 MS files, 0
 .VideoInfo[n].child				 : children
 .VideoInfo[n].parent				 : parents
*/
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;

	// API calls for searching
	var url_base = "http://video.google.com/videofeed?type=search&q=" + PSPTube.encodeURI( keyword )+"+site%3Avideo.google.com&so=0&num=20&output=rss&start="+(start_index-1);

	var nPage = Math.floor( (start_index - 1) / 20 ) + 1;	/* begin the search page */
	var nIndex = (nPage - 1) * 20 + 1;						/* begin index */
	while(nIndex < (start_index + length)) {
		var url = url_base;
		var contents = GetContents( url );
		if(contents.match( /<openSearch:totalResults>([^<]*)<\/openSearch:totalResults>/ )) { //totalResults>(1)<
			result.total = RegExp.$1;
		}
		if(contents == null) { return null; /* failed to get page */ }
		/* total number of results */
		var i = nIndex;
		while(contents.indexOf("<item>") >= 0) {
			if((start_index <= i) && (i < (start_index + length))) {
				contents = contents.substring(contents.indexOf("<item>"));
				var e_end = contents.indexOf("</item>");
				var item = contents.substring(0, e_end+8);
				contents = contents.substring(item.length);
				info = new Object();

				// Title
				var title;
				if(title = item.match( /<media:title>([^<]*)<\/media:title>/ )) {
					info.Title = title[1].replace(/&amp;/g,"&").replace(/&quot;/g,'"');
					info.SaveFilename = info.Title.replace(/\*/g,'_').replace(/\"/g,'_').replace(/\</g,'_').replace(/\>/g,'_').replace(/\|/g,'_').replace(/\\/g,'_').replace(/\//g,'_').replace(/\:/g,'_').replace(/\?/g)+'.flv';
				}
				// Description
				var description;
				if(description = item.match( /<media:description>([^<]*)<\/media:description>/ )) {
					info.Description = description[1].replace(/&amp;/g,"&").replace(/&quot;/g,'"');
				}
				// Thumbnail
				var thumbnail;
				if(thumbnail = item.match( /<media:thumbnail url="([^"]*)"/ )) {
					info.ThumbnailURL = thumbnail[1].replace(/&amp;/g,"&");
				}
				// URL
				var file_and_length;
				if(file_and_length = item.match( /<media:content url="([^"]*)".*duration="([^"]*)"/ )) {
					var pattern = /<media:content url="([^"]*)".*duration="([^"]*)"/;
					var ind = item.indexOf(file_and_length[0]);
					file_and_length = item.substring(ind+1).match(pattern);
					info.URL = file_and_length[1].replace(/&amp;/g,"&");
					info.LengthSeconds = file_and_length[2];
				}
				info.attr = 7;
				result.VideoInfo.push( info );
			}
			i++;
		}
		nIndex = nIndex + 20;
		nPage++;
	}

		result.end   = result.start + result.VideoInfo.length;

	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 = 2;						/* 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 GoogleVideo to site list
SiteList.push( GoogleVideo );
