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

function ThatVideoSite_CheckURL( url, option )
{
	if (url.match("http://www.thatvideosite.com") == "http://www.thatvideosite.com"){
		return 1;
	} else{
		return 0;
	}
}

/*
 Get a ThatVideoSite URL
*/
function ThatVideoSite_GetURL( url, option )
{
	var video_url = url.match(/http\:\/\/www.thatvideosite.com\/video\/(.*)/);
	var video_url = "http://cdn.thatvideosite.com/" + video_url[1] + "/M" + video_url[1] + "T0C00.flv";
	return video_url;
}

function ThatVideoSite_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;
	var perpage = 63;


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


	if(result.start < perpage){
	var contents = GetContents( "http://www.thatvideosite.com/?search=" + keyword.replace(/ /g, "+") );
	} else{
	var num = 1 + ((result.start - 1) / perpage);
	var contents = GetContents( "http://www.thatvideosite.com/?search=" + keyword.replace(/ /g, "%20") + "&page=" + num );
	}
	if(contents == null) { return null; /* failed to get page */ }


	var pos = contents.indexOf("' SELECTED>Page ");
	var total = contents.substring(pos + 16, contents.length);
	pos = total.indexOf(" of ");
	total = total.substring(pos + 4, contents.length);
	pos = total.indexOf("</option>");
	total = total.substring(0, pos).replace(/,/g, "");
	var total = parseInt(total) * 59;
	result.total = total;


	while(1){
	pos = contents.indexOf("<td width='50%' valign='top'>");

	if(pos != -1){
		contents = contents.substring(pos + 29, contents.length);
		//Get URL
		var url = contents.match(/\<a href\=\'(.*?)\'/);
		var url = "http://www.thatvideosite.com" + url[1];
		//Get Thumbnail
		var thumb_url = contents.match(/\<img src\=\'(.*?)\'/);
		var thumb_url = thumb_url[1];
		//Title
		pos = contents.indexOf("2px solid #215985; padding: 4px;'>");
		contents = contents.substring(pos + 34, contents.length);
		var title = contents.match(/\'\>(.*?)\<\/a\>/);
		var title = ImproveText(title[1]);
		//Description
		var description = splitTo2Lines(title, 60);
		//Views
		var views = contents.match(/\<b\>Views\:\<\/b\> (.*?)\n/);
		var views = parseInt(views[1].replace(/,/g, ""));
		
		info = new Object();
		info.URL = url;
		info.ThumbnailURL = thumb_url;
		info.Title = title;
		info.Description = description;
		info.ViewCount = views;
		info.SaveFilename = ToFilename(title);
		info.attr = 7;
		result.VideoInfo.push( info );
		if(result.VideoInfo.length == perpage){break;}
		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 != perpage){result.total = result.end;}

	return result;
}

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

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