// MySpace Videos.js for PSPTube
// Made By JK108

function MySpaceVideos_CheckURL( url, option )
{
	if (url.match("http://mediaservices.myspace.com/") == "http://mediaservices.myspace.com/"){
		return 1;
	} else{
		return 0;
	}
}

/*
 Get a MySpaceVideos URL
*/
function MySpaceVideos_GetURL( url, option )
{
	var contents = GetContents(url);
	if(contents == null) { return null; /* failed to get page */ }
	var video_url = contents.match(/\<media\:content url\=\"(.*?)\"/);
	var video_url = video_url[1];
	return video_url;
}

function MySpaceVideos_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://searchservice.myspace.com/index.cfm?fuseaction=sitesearch.results&type=MySpaceTV&qry=" + keyword.replace(/ /g, "%20") );
	} else{
	var num = 1 + ((result.start - 1) / perpage);
	var contents = GetContents( "http://searchservice.myspace.com/index.cfm?fuseaction=sitesearch.results&type=MySpaceTV&qry=" + keyword.replace(/ /g, "%20") + "&pg=" + num );
	}
	if(contents == null) { return null; /* failed to get page */ }


	if(contents.match("</span> - did not match any documents.<") == "</span> - did not match any documents.<"){ result.total = 0; return result; }
	var total = contents.match(/\<\/span\> of (.*?) results for \<span/);
	var total = total[1].replace(/,/g, "");
	result.total = total;


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

	if(pos != -1){
		contents = contents.substring(pos + 24, contents.length);
		//Get URL
		var url = contents.match(/&videoid\=(.*?)&searchid\=/);
		var url = "http://mediaservices.myspace.com/services/rss.ashx?type=video&mediaID=" + url[1];
		//Get Thumbnail
		var thumb_url = contents.match(/\<img src\=\"(.*?)\"/);
		var thumb_url = thumb_url[1];		
		//Title
		pos = contents.indexOf("<div class=\"videoInfo\">");
		contents = contents.substring(pos + 23, contents.length);
		pos = contents.indexOf(">");
		contents = contents.substring(pos + 1, contents.length);
		pos = contents.indexOf("</a>");
		var title = ImproveText(contents.substring(0, pos));
		//Description
		pos = contents.indexOf("</span></a><br/>");
		contents = contents.substring(pos + 16, contents.length);
		pos = contents.indexOf("<br/>");
		var description = ImproveText(contents.substring(0, pos));
		if(description.substring(0, 13) == "Categories: <" || description == "" || description == null){description = title;}
		description = splitTo2Lines(description);
		//Length
		var length = contents.match(/labelPrefix\"\>Time\:\<\/span\> (.*?)\<\/li\>/);
		var length = TimestringToSeconds(length[1]);
		//Views
		var views = contents.match(/labelPrefix\"\>Plays\:\<\/span\> (.*?)\<\/li\>/);
		var views = parseInt(views[1].replace(/,/g, ""));
		//Comments
		var comments = contents.match(/labelPrefix\"\>Comments\:\<\/span\> (.*?)\<\/li\>/);
		var comments = parseInt(comments[1].replace(",", ""));
		
		info = new Object();
		info.URL = url;
		info.ThumbnailURL = thumb_url;
		info.Title = title;
		info.Description = description;
		info.LengthSeconds = length;
		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 MySpaceVideos = new Object();
MySpaceVideos.Name          = "MySpace Videos";		/* Service name */
MySpaceVideos.Description   = "MySpace Videos";		/* Service description */
MySpaceVideos.SearchDesc    = "MySpace Videos";		/* Service search screen description */
MySpaceVideos.SearchOSKMode = 1;			/* OSK mode 1: English only  2: Japanese */
MySpaceVideos.CheckURL      = MySpaceVideos_CheckURL;	/* CheckURL function */
MySpaceVideos.GetURL        = MySpaceVideos_GetURL;	/* GetURL function */
MySpaceVideos.Search        = MySpaceVideos_Search;	/* Search function */

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