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

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

/*
 Get a TubeIso URL
*/
function TubeIso_GetURL( url, option )
{
	var contents = GetContents(url);
	if(contents == null) { return null; /* failed to get page */ }
	if(contents.match("<embed src=\"http://www.youtube.com/v/") == "<embed src=\"http://www.youtube.com/v/"){
		var video_url = contents.match(/\<embed src\=\"http\:\/\/www.youtube.com\/v\/(.*?)&/);
		var urltoken = "http://www.youtube.com/api2_rest?method=youtube.videos.get_video_token&video_id=" + video_url[1];
        	var contents = GetContents(urltoken);
		var token = contents.match( /<t>(.*)<\/t>/ );
       		var token = token[1];
       		var video_url = "http://youtube.com/get_video?video_id=" + video_url[1] + "&t=" + token;
	} else{
		var video_url = url.match(/http\:\/\/www.tubeiso.com\/video\/(.*?)\//);
		var video_url = "http://www.tubeiso.com/flvideo/" + video_url[1] + ".flv";
	}
	return video_url;
}

function TubeIso_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 = 20;


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


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


	if(contents.match(">\nYour search did not return any results!\n") == ">\nYour search did not return any results!\n"){ result.total = 0; return result; }
	var pos = contents.indexOf("<div class=\"videopaging\">");
	contents = contents.substring(pos + 25, contents.length);
	pos = contents.indexOf(" of ");
	contents = contents.substring(pos + 4, contents.length);
	pos = contents.indexOf("\n");
	var total = contents.substring(0, pos).replace(/,/g, "");
	result.total = total;


	while(1){
	pos = contents.indexOf("<div class=\"listchannel\">");

	if(pos != -1){
		contents = contents.substring(pos + 25, contents.length);
		//Get URL
		var url = contents.match(/\<a href\=\"(.*?)\"/);
		var url = url[1];
		//Get Thumbnail
		var thumb_url = contents.match(/src="(.*?)\"/);
		var thumb_url = thumb_url[1];
		//Title
		pos = url.lastIndexOf("/");
		var title = ImproveText(url.substring(pos + 1, url.length).replace(/-/g, " "));
		//Length
		var length = contents.match(/\<span class\=\"duration\"\>(.*?)\<\/span\>/);
		var length = TimestringToSeconds(length[1]);
		//Description
		pos = contents.indexOf("<span class=\"info\">Added:</span>");
		var description = contents.substring(pos + 74, contents.length);
		pos = description.indexOf("\n");
		description = splitTo2Lines("Added " + ImproveText(description.substring(0, pos).replace(/  /g, " ")), 60);
		//Views
		pos = contents.indexOf("<span class=\"info\">Views:</span>");
		var views = contents.substring(pos + 53, contents.length);
		pos = views.indexOf("\n");
		views = parseInt(views.substring(0, pos).replace(/,/g, ""));
		//Comments
		pos = contents.indexOf("<span class=\"info\">Comments:</span>");
		var comments = contents.substring(pos + 56, contents.length);
		pos = comments.indexOf("\n");
		comments = parseInt(comments.substring(0, pos).replace(/,/g, ""));
		
		info = new Object();
		info.URL = url;
		info.ThumbnailURL = thumb_url;
		info.Title = title;
		info.LengthSeconds = length;
		info.Description = description;
		info.ViewCount = views;
		info.CommentCount = comments;
		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 TubeIso = new Object();
TubeIso.Name          = "TubeIso";		/* Service name */
TubeIso.Description   = "TubeIso";		/* Service description */
TubeIso.SearchDesc    = "TubeIso";		/* Service search screen description */
TubeIso.SearchOSKMode = 1;			/* OSK mode 1: English only  2: Japanese */
TubeIso.CheckURL      = TubeIso_CheckURL;	/* CheckURL function */
TubeIso.GetURL        = TubeIso_GetURL;		/* GetURL function */
TubeIso.Search        = TubeIso_Search;		/* Search function */

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