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

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

/*
 Get a Dailymotion URL
*/
function Dailymotion_GetURL( url, option )
{
	var contents = GetContents(url);
	if(contents == null){return null;}
	var video_url = contents.match(/addVariable\(\"video\", \"(.*?)\"/);
	var video_url = PSPTube.decodeURI(video_url[1]);
	var video_url = video_url.split("@@");
	var video_url = "http://www.dailymotion.com" + video_url[0];
	return video_url;
}

function Dailymotion_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 = 16;


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


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


	if(contents.match(">Sorry! Your search didn't return any videos.<br/>") == ">Sorry! Your search didn't return any videos.<br/>"){result.total = 0; return result;}
	result.total = 10000;


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

	if(pos != -1){
		contents = contents.substring(pos + 27, contents.length);
		//Get URL
		var url = contents.match(/\" href\=\"(.*?)\"/);
		var url = "http://www.dailymotion.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(/\<div class\=\"dmco_text duration\"\>(.*?)\<\/div\>/);
		var length = length[1];
		var length = TimestringToSeconds(length);
		//Description
		pos = contents.indexOf("<div class=\"dmpi_video_description\">");
		var description = contents.substring(pos + 36, contents.length);
		pos = description.indexOf("<div>");
		description = description.substring(pos + 5, description.length);
		pos = description.indexOf("</div>");
		description = description.substring(0, pos);
		if(description == null || description == ""){description = title;}
		description = splitTo2Lines(description, 60);
		//Views
		pos = contents.indexOf("<div class=\"dmco_counter views block\">");
		var views = contents.substring(pos + 38, contents.length);
		pos = views.indexOf("<span class=\"value\">");
		views = views.substring(pos + 20, views.length);
		pos = views.indexOf("</span>");
		views = parseInt(views.substring(0, pos));
		//Comments
		pos = contents.indexOf("<div class=\"dmco_counter com\">");
		var comments = contents.substring(pos + 30, contents.length);
		pos = comments.indexOf("<span class=\"value\">");
		comments = comments.substring(pos + 20, comments.length);
		pos = comments.indexOf("</span>");
		comments = parseInt(comments.substring(0, pos));

		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 Dailymotion = new Object();
Dailymotion.Name          = "Dailymotion";		/* Service name */
Dailymotion.Description   = "Dailymotion";		/* Service description */
Dailymotion.SearchDesc    = "Dailymotion";		/* Service search screen description */
Dailymotion.SearchOSKMode = 1;				/* OSK mode 1: English only  2: Japanese */
Dailymotion.CheckURL      = Dailymotion_CheckURL;	/* CheckURL function */
Dailymotion.GetURL        = Dailymotion_GetURL;		/* GetURL function */
Dailymotion.Search        = Dailymotion_Search;		/* Search function */

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