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

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

/*
 Get a CrunchyRoll URL
*/
function CrunchyRoll_GetURL( url, option )
{
	var contents = GetContents(url);
	if(contents == null) { return null; /* failed to get page */ }
	var video_url = contents.match(/\%3Cfile\%3E(.*?)\%3C\%2Ffile\%3E/);
	var video_url = PSPTube.decodeURI(video_url[1]).replace(/&amp;/g, "&");
	return video_url;
}

function CrunchyRoll_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.crunchyroll.com/search?adv=1&o=m&q=" + keyword.replace(/ /g, "%20") );
	} else{
	var num = (result.start - 1) / perpage;
	var contents = GetContents( "http://www.crunchyroll.com/search?adv=1&o=m&q=" + keyword.replace(/ /g, "%20") + "&pg=" + num );
	}
	if(contents == null) { return null; /* failed to get page */ }


	if(contents.match("text-align:center;\">No results found</h3>") == "text-align:center;\">No results found</h3>"){ result.total = 0; return result; }
	var total = contents.match(/out of (.*?) results for\: \<span style\=\"font-weight\:bold;\"\>/);
	var total = total[1];
	result.total = total;


	while(1){
	var pos = contents.indexOf("<div class=\"search-list-item clearfix\">");

	if(pos != -1){
		contents = contents.substring(pos + 39, contents.length);
		//Get URL
		var url = contents.match(/\<a href\=\"(.*?)\"/);
		var url = url[1] + "?hires=1";
		//Get Thumbnail
		var thumb_url = contents.match(/background\: url\(&#039;(.*?)&#039;\)/);
		var thumb_url = thumb_url[1];
		//Title
		var title = contents.match(/ title\=\"(.*?)\"/);
		var title = ImproveText(title[1]);
		//Description
		var user = contents.match(/\<a href\=\"http\:\/\/www.crunchyroll.com\/user\/(.*?)\"/);
		var ind1 = contents.indexOf("<a href=\"http://www.crunchyroll.com/user/");
		var ind2 = contents.indexOf("<div class=\"search-list-item clearfix\">");
		if(user != null && (ind1 < ind2 || result.VideoInfo.length == perpage - 1)){
			var user = "Uploaded By: " + user[1];
		} else{
			var user = "";
		}
		var thedate = contents.match(/clearfix\"\>\<dt\>Date\:\<\/dt\>\<dd\>(.*?)\<\/dd\>\<\/dl\>/);
		var ind1 = contents.indexOf("clearfix\"><dt>Date:</dt><dd>");
		if(thedate != null && (ind1 < ind2 || result.VideoInfo.length == perpage - 1)){
			var thedate = "Date: " + thedate[1];
		} else{
			var thedate = "";
		}
		var description = contents.match(/clearfix\"\>\<dt\>Description\:\<\/dt\>\<dd\>(.*?)\<\/dd\>\<\/dl\>/);
		var ind1 = contents.indexOf("clearfix\"><dt>Description:</dt><dd>");
		if(description != null && (ind1 < ind2 || result.VideoInfo.length == perpage - 1)){
			var description = "Description: " + splitTo2Lines(ImproveText(description[1]), 47);
		} else{
			var description = "";
		}
		if(description != ""){
			if(user != ""){description = description + "\n" + user;}
		} else{
			if(user != ""){description = user;}
		}
		if(description != ""){
			if(thedate != ""){description = description + "\n" + thedate;}
		} else{
			if(thedate != ""){description = thedate;}
		}

		if(description == null || description == ""){description = title;}

		info = new Object();
		info.URL = url;
		info.ThumbnailURL = thumb_url;
		info.Title = title;
		info.Description = description;
		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 CrunchyRoll = new Object();
CrunchyRoll.Name          = "CrunchyRoll";		/* Service name */
CrunchyRoll.Description   = "CrunchyRoll";		/* Service description */
CrunchyRoll.SearchDesc    = "CrunchyRoll";		/* Service search screen description */
CrunchyRoll.SearchOSKMode = 1;				/* OSK mode 1: English only  2: Japanese */
CrunchyRoll.CheckURL      = CrunchyRoll_CheckURL;	/* CheckURL function */
CrunchyRoll.GetURL        = CrunchyRoll_GetURL;		/* GetURL function */
CrunchyRoll.Search        = CrunchyRoll_Search;		/* Search function */

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