// RedTube.js for PSPTube20080224 (by FreePlay)

function RedTube_CheckURL( url, option )
{
	return 1;
}

/*
 Get a RedTube URL
*/
function RedTube_GetURL( url, option )
{
	// Grab video ID:
	// "movie=" video ID
	var video_id = url.match( /^http:\/\/www\.redtube\.com\/([0-9]*)/ );
	if(video_id == null) { return null; /* ID not valid */ }
	video_id = RegExp.$1;

	var contents = GetContents( "http://www.redtube.com/" + video_id );
	if(contents == null) { return null; /* error getting page */ }

	var s = "" + video_id;
	if (s == "")
	{
		s = "1";
	} // end if
	var pathnr = "" + Math.floor(parseFloat(s) / 1000);
	var l = s.length;
	var i = 1;
	while (i <= 7 - l)
	{
		s = "0" + s;
		++i;
	} // end while
	l = pathnr.length;
	for (i = 1; i <= 7 - l; i++)
	{
		pathnr = "0" + pathnr;
	} // end of for
	var xc = new Array("R", "1", "5", "3", "4", "2", "O", "7", "K", "9", "H", "B", "C", "D", "X", "F", "G", "A", "I", "J", "8", "L", "M", "Z", "6", "P", "Q", "0", "S", "T", "U", "V", "W", "E", "Y", "N");
	var code = "";
	var qsum = 0;
	for (i = 0; i <= 6; i++)
	{
		qsum = qsum + parseInt(s.charAt(i)) * (i + 1);
	} // end of for
	var s1 = "" + qsum;
	qsum = 0;
	for (i = 0; i < s1.length; i++)
	{
		qsum = qsum + parseInt(s1.charAt(i));
	} // end of for
	var qstr;
	if (qsum >= 10)
	{
		qstr = "" + qsum;
	}
	else
	{
		qstr = "0" + qsum;
	} // end else if
	code = code + xc[s.charCodeAt(3) - 48 + qsum + 3];
	code = code + qstr.charAt(1);
	code = code + xc[s.charCodeAt(0) - 48 + qsum + 2];
	code = code + xc[s.charCodeAt(2) - 48 + qsum + 1];
	code = code + xc[s.charCodeAt(5) - 48 + qsum + 6];
	code = code + xc[s.charCodeAt(1) - 48 + qsum + 5];
	code = code + qstr.charAt(0);
	code = code + xc[s.charCodeAt(4) - 48 + qsum + 7];
	code = code + xc[s.charCodeAt(6) - 48 + qsum + 4];
	content_video = pathnr + "/" + code + ".flv";
	return "http://dl.redtube.com/_videos_t4vn23s9jc5498tgj49icfj4678/"+content_video;
}

/*
 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
*/

// RedTube's search is currently nonexistent - you have to enter a video ID.
function RedTube_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 = "http://www.redtube.com/"+keyword;
	var contents = GetContents(url);
	var info = new Object();
	info.Title = contents.match(/<title>([^<]*)<\/title>/)[1].substring(6);
	info.Description = info.Title;
	info.URL = url;
	info.ThumbnailURL = "http://www.redtube.com/_pix/logo.png";
	
	info.RatingCount = contents.match(/<td align=center style='color:#666666; font-family:Tahoma,Helvetica,Arial;' id=anzrattd>([^<]*)<\/td>/)
	info.RatingCount = parseInt(info.RatingCount[1].substring(0, info.RatingCount[1].length-8).replace(/,/,''));
	info.attr = 7;
	result.VideoInfo.push(info);
	result.end = 1;
	result.total = 1;	// no results.
	return result;
}

// Service metadata
var RedTube = new Object();
RedTube.Name          = "RedTube";			/* Service name */
RedTube.Description   = "RedTube";			/* Service description */
RedTube.SearchDesc    = "RedTube";			/* Service search screen description */
RedTube.SearchOSKMode = 1;						/* OSK mode 1: English only  2: Japanese */
RedTube.CheckURL      = RedTube_CheckURL;	/* CheckURL function */
RedTube.GetURL        = RedTube_GetURL;		/* GetURL function */
RedTube.Search        = RedTube_Search;		/* Search function */

// add RedTube to site list
SiteList.push( RedTube );

