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

function MyVideo_CheckURL( url, option )
{
	if (url.match("http://www.myvideo.de/") == "http://www.myvideo.de/"){
		return 1;
	} else{
		return 0;
	}
}

/*
 Get a MyVideo URL
*/
function MyVideo_GetURL( url, option )
{
	var contents = GetContents(url);
	if(contents == null) { return null; /* failed to get page */ }
	var video_id = url.match(/www.myvideo.de\/watch\/(.*?)\//);
	var video_id = video_id[1];
	var pos = contents.indexOf("thumbs/" + video_id);
	var pos2 = contents.lastIndexOf("'", pos);
	contents = contents.substring(pos2 + 1, contents.length);
	pos = contents.indexOf("thumbs/" + video_id);
	var video_url = contents.substring(0, pos) + video_id + ".flv";
	return video_url;
}

function MyVideo_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.myvideo.de/news.php?rubrik=uoijv&searchWord=" + keyword.replace(/ /g, "+") );
	} else{
	var num = 1 + ((result.start - 1) / perpage);
	var contents = GetContents( "http://www.myvideo.de/news.php?lpage=" + num + "&rubrik=uoijv&searchWord=" + keyword.replace(/ /g, "+") );
	}
	if(contents == null) { return null; /* failed to get page */ }



	if(contents.match("<p class='error'> Für Deine Suche konnte kein passendes Ergebnis gefunden werden.</p>") == "<p class='error'> Für Deine Suche konnte kein passendes Ergebnis gefunden werden.</p>"){ result.total = 0; return result; }
	var total = contents.match(/von (.*?)\<\/span\>\<\/td\>\<\/tr\>\<\/table\>/);
	var total = total[1];
	result.total = total;



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

	if(pos != -1){
		contents = contents.substring(pos + 20, contents.length);
		//Get URL
		var url = contents.match(/\<a href\=\'(.*?)\'/);
		var url = "http://www.myvideo.de" + url[1];
		//Get Thumbnail
		var thumb_url = contents.match(/\' src\=\'(.*?)\'/);
		var thumb_url = thumb_url[1];
		//Title
		var title = contents.match(/\stitle\=\'(.*?)\'\>/);
		var title = ImproveText(title[1]);
		//Length
		var length = contents.match(/onmousemove\=\"ST\(event, this\);\"\/\> (.*?)\<\/span\>/);
		var length = TimestringToSeconds(length[1]);
		//Description
		var description = contents.match(/\<span class\=\'nick\'\> Von\:(.*?)\'\>/);
		var description = description[1] + "'";
		var description = description.match(/title\=\'(.*?)\'/);
		var description = "Von: " + description[1];
		//Views
		var views = contents.match(/onmousemove\=\"ST\(event, this\);\"\/\> \<span id\=\'(.*?)\<\/span\>/);
		var views = views[1] + "'";
		var views = views.match(/\'\>(.*?)\'/);
		var views = parseInt(views[1]);
		//Comments
		var comments = contents.match(/title\=\' Kommentare \' alt\=\'\' onmouseover\=\"ST\(event, this\);\"(.*?)\<\/span\>/);
		var comments = comments[1] + "'";
		var comments = comments.match(/\"\/\> (.*?)\'/);
		var comments = parseInt(comments[1]);
		
		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 MyVideo = new Object();
MyVideo.Name          = "MyVideo";		/* Service name */
MyVideo.Description   = "MyVideo";		/* Service description */
MyVideo.SearchDesc    = "MyVideo";		/* Service search screen description */
MyVideo.SearchOSKMode = 1;			/* OSK mode 1: English only  2: Japanese */
MyVideo.CheckURL      = MyVideo_CheckURL;	/* CheckURL function */
MyVideo.GetURL        = MyVideo_GetURL;		/* GetURL function */
MyVideo.Search        = MyVideo_Search;		/* Search function */

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