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

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

/*
 Get a MetaCafe URL
*/
function MetaCafe_GetURL( url, option )
{
	var contents = GetContents(url);
	if(contents == null) { return null; /* failed to get page */ }
	var video_url = contents.match(/&mediaURL\=(.*?)&postRollContentURL\=/);
	var video_url = PSPTube.decodeURI(video_url[1]).replace("&gdaKey=", "?__gda__=");
	return video_url;
}

function MetaCafe_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 = 15;


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


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


	if(contents.match("<h4><bdo dir=\"ltr\">") != "<h4><bdo dir=\"ltr\">"){ result.total = 0; return result; }
	var total = contents.match(/\<h4\>\<bdo dir\=\"ltr\"\>(.*?) Metacafe Videos\</);
	var total = total[1].replace(/,/g, "");
	result.total = total;


	while(1){
	var pos = contents.indexOf("<li id=\"i1_");

	if(pos != -1){
		contents = contents.substring(pos + 11, contents.length);
		//Get URL
		var url = contents.match(/\<a href\=\"(.*?)\"/);
		var url = "http://www.metacafe.com" + url[1];
		//Get Thumbnail
		var thumb_url = contents.match(/\<img src\=\"(.*?)\"/);
		var thumb_url = thumb_url[1];
		//Title
		var title = contents.match(/\" title\=\"(.*?)\"/);
		var title = ImproveText(title[1]);
		//Length
		var length = contents.match(/\<span\>(.*?)\<\/span\>/);
		var length = TimestringToSeconds(length[1]);
		//Description
		pos = contents.indexOf("<p class=\"ItemInfo\">");
		var description = contents.substring(pos + 25, contents.length);
		pos = description.indexOf("\t\t\t</p>");
		var description = splitTo2Lines(ImproveText(description.substring(0, pos)), 60);
		//Views
		var views = contents.match(/\<span class\=\"NumViews\"\>(.*?)\<\/span\>/);
		var views = parseInt(views[1].replace(/,/g, ""));
		//Comments
		var comments = contents.match(/\<span class\=\"NumComments\"\>(.*?)\<\/span\>/);
		var comments = parseInt(comments[1].replace(",", ""));
		
		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 MetaCafe = new Object();
MetaCafe.Name          = "MetaCafe";		/* Service name */
MetaCafe.Description   = "MetaCafe";		/* Service description */
MetaCafe.SearchDesc    = "MetaCafe";		/* Service search screen description */
MetaCafe.SearchOSKMode = 1;			/* OSK mode 1: English only  2: Japanese */
MetaCafe.CheckURL      = MetaCafe_CheckURL;	/* CheckURL function */
MetaCafe.GetURL        = MetaCafe_GetURL;	/* GetURL function */
MetaCafe.Search        = MetaCafe_Search;	/* Search function */

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