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

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

/*
 Get a LiveLeak URL
*/
function LiveLeak_GetURL( url, option )
{
	var contents = GetContents("http://keepvid.com/?url=" + PSPTube.encodeURI(url));
	if(contents == null) { return null; /* failed to get page */ }
	var video_url = contents.match(/\<\/script\>\<br \/\>\<br \/\>\<a href\=\"(.*?)\"/);
	var video_url = video_url[1];
	if(video_url.match(".jpg?") == ".jpg?"){return null;}
	return video_url;
}

function LiveLeak_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 = 12;


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


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


	if(contents.match("<center><p>Sorry, no results found!</p></center>") == "<center><p>Sorry, no results found!</p></center>"){ result.total = 0; return result; }
	result.total = 10000;


	while(1){
	var pos = contents.indexOf("<li><a href=\"view");

	if(pos != -1){
		contents = contents.substring(pos + 4, contents.length);
		//Get URL
		var url = contents.match(/href\=\"(.*?)\"/);
		var url = "http://www.liveleak.com/" + url[1];
		//Get Thumbnail
		var thumb_url = contents.match(/\<img src\=\"(.*?)\"/);
		var thumb_url = thumb_url[1];
		//Title
		pos = contents.indexOf("\" title=\"");
		var title = contents.substring(pos + 9, contents.length);
		pos = title.indexOf("\"");
		var title = ImproveText(title.substring(0, pos).replace(/\n/g, " "));
		//Length
		var length = contents.match(/Type\: Video \((.*?) Secs\)/);
		var ind1 = contents.indexOf("Type: Video (");
		var ind2 = contents.indexOf("<li><a href=\"view");
		if(length != null && (ind1 < ind2 || result.VideoInfo.length == perpage - 1)){
			var length = parseInt(length[1]);
		} else{
			var length = 0;
		}
		//Description
		var description = contents.match(/Leaked\: (.*?)\<\/p\>/);
		var tofind = "Leaked: " + RegExp.$1 + "</p>";
		pos = contents.indexOf(tofind);
		var description = contents.substring(pos + tofind.length + 11, contents.length);
		pos = description.indexOf("</div>");
		var description = splitTo2Lines(ImproveText(description.substring(0, pos).replace(/\n/g, " ")), 60);
		if(description == null || description == ""){description = title;}
		//Views
		var views = contents.match(/<\/a\>\<br \/\>(.*?) View/);
		var views = parseInt(views[1]);
		//Comments
		var comments = contents.match(/c\=1#comments\"\>(.*?) Comment/);
		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 LiveLeak = new Object();
LiveLeak.Name          = "LiveLeak";		/* Service name */
LiveLeak.Description   = "LiveLeak";		/* Service description */
LiveLeak.SearchDesc    = "LiveLeak";		/* Service search screen description */
LiveLeak.SearchOSKMode = 1;			/* OSK mode 1: English only  2: Japanese */
LiveLeak.CheckURL      = LiveLeak_CheckURL;	/* CheckURL function */
LiveLeak.GetURL        = LiveLeak_GetURL;		/* GetURL function */
LiveLeak.Search        = LiveLeak_Search;		/* Search function */

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