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

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

/*
 Get a NothingToxic URL
*/
function NothingToxic_GetURL( url, option )
{
	var contents = GetContents(url);
	if(contents == null) { return null; /* failed to get page */ }
	var video_url = contents.match(/var flv_media_url \= \"(.*?)\";/);
	var video_url = video_url[1];
	return video_url;
}

function NothingToxic_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 = 10;


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


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


	if(contents.match("<div style=\"height:400px\">We have no results for ") == "<div style=\"height:400px\">We have no results for "){ result.total = 0; return result; }
	result.total = 10000;
	if(contents.match("<div class=\"pages\" style=\"width:385px;\"") != "<div class=\"pages\" style=\"width:385px;\""){ result.total = 10;}


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

	if(pos != -1){
		contents = contents.substring(pos + 25, contents.length);
		//Get URL
		var url = contents.match(/\<a href\=\"(.*?)\"/);
		var url = "http://www.nothingtoxic.com" + url[1];
		//Get Thumbnail
		var thumb_url = contents.match(/\<img src\=\"(.*?)\"/);
		var thumb_url = thumb_url[1];
		//Title
		var title = contents.match(/class\=\"big-link\"\>(.*?)\<\/a\>/);
		var title = ImproveText(title[1]);
		//Description
		var description = contents.match(/\<p class\=\"text\"\>(.*?)&nbsp;&nbsp;/);
		var description = splitTo2Lines(ImproveText(description[1]), 60);
		//Views
		var views = contents.match(/\<\/span\>  views\: (.*?) \<span/);
		var views = parseInt(views[1]);
		//Comments
		var comments = contents.match(/\<p class\=\"small-text\"\>\((.*?)\)/);
		var comments = parseInt(comments[1]);
		
		info = new Object();
		info.URL = url;
		info.ThumbnailURL = thumb_url;
		info.Title = title;
		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 NothingToxic = new Object();
NothingToxic.Name          = "NothingToxic";		/* Service name */
NothingToxic.Description   = "NothingToxic";		/* Service description */
NothingToxic.SearchDesc    = "NothingToxic";		/* Service search screen description */
NothingToxic.SearchOSKMode = 1;				/* OSK mode 1: English only  2: Japanese */
NothingToxic.CheckURL      = NothingToxic_CheckURL;	/* CheckURL function */
NothingToxic.GetURL        = NothingToxic_GetURL;	/* GetURL function */
NothingToxic.Search        = NothingToxic_Search;	/* Search function */

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