// ==UserScript==
// @name           download videos found in search
// @namespace      http://ourfortsite.wetpaint.com
// @description    Scans the YouTube page for all formatsworks in ff + chrome 
// @version        0.9.2
// @author         rossy!
// @include        http://youtube.com/watch?*
// @include        http://*.youtube.com/watch?*
// @include        http://photobucket.com*
// @include        http://videos.google.com*
// @license        MIT License
// ==/UserScript==

function YouTubeVideoDownload()
{
	function createDLLink(title, bold, description, href)
	{
		var link = document.createElement("a");
		link.setAttribute("href", href);
		link.setAttribute("style", "display: block");
		
		var titleEle = document.createElement("span");
		if (bold)
			titleEle.setAttribute("style", "font-weight: bold;");
		titleEle.textContent = title + " ";
		link.appendChild(titleEle);
		
		var descEle = document.createElement("span");
		descEle.setAttribute("style", "opacity: .5;");
		descEle.textContent = description;
		link.appendChild(descEle);
		
		return link;
	}
	
	var formatList = {"5": 1, "6": 2, "34": 3, "18": 4, "35": 5, "22": 6, "37": 7};
	var downloadTabExists = true;
	
	if (document.getElementById("watch-action-download"))
		document.getElementById("watch-action-download-link").getElementsByClassName("watch-action-text")[0].innerHTML = " Download";
	else
	{
		downloadTabExists = false;
		
		var dlButton = document.createElement("div");
		dlButton.setAttribute("id", "watch-tab-download");
		dlButton.setAttribute("class", "watch-tab");
		dlButton.setAttribute("onclick", "yt.www.watch.actions.selectTab(this);");
		
		var dlButtonA = document.createElement("a");
		dlButtonA.setAttribute("id", "watch-action-download-link");
		dlButtonA.setAttribute("class", "watch-action-link");
		dlButtonA.setAttribute("onclick", "return false");
		dlButtonA.setAttribute("href", "#");
		dlButton.appendChild(dlButtonA);
	
		var dlButtonBtn = document.createElement("button");
		dlButtonBtn.setAttribute("id", "watch-action-download");
		dlButtonBtn.setAttribute("class", "master-sprite");
		dlButtonBtn.setAttribute("title", "Download");
		dlButtonA.appendChild(dlButtonBtn);
	
		var dlButtonSpan = document.createElement("span");
		dlButtonSpan.setAttribute("class", "watch-action-text");
		dlButtonSpan.innerHTML = " Download";
		dlButtonA.appendChild(dlButtonSpan);
		
		var dlButtonArrow = document.createElement("button");
		dlButtonArrow.setAttribute("class", "master-sprite watch-tab-arrow");
		dlButtonArrow.setAttribute("title", "");
		dlButton.appendChild(dlButtonArrow);
		
		var watchTabs = document.getElementById("watch-actions-area").getElementsByClassName("watch-tabs")[0];
		watchTabs.insertBefore(dlButton, watchTabs.getElementsByClassName("clear")[0]);
	}
	
	var tabBody = document.getElementById("watch-tab-download-body");
	if (!downloadTabExists)
		tabBody.setAttribute("style", "background-color: #F3F3F3 !important; padding: 8px 10px;");
	var tabBodyDiv = document.createElement("div");
	tabBodyDiv.setAttribute("style", "padding: 0 13px; font-size: 11px;");
	var tabBodyHeader = document.createElement("b");
	if (downloadTabExists)
		tabBodyHeader.textContent = "Or download in these alternative formats (Free):";
	else
		tabBodyHeader.textContent = "Choose a format:";
	tabBodyDiv.appendChild(tabBodyHeader);
	tabBodyDiv.appendChild(document.createElement("br"));
	var fmt_url_map = unescape(yt.getConfig("SWF_ARGS")["fmt_url_map"]).split(",");
	var video_id = yt.getConfig("SWF_ARGS")["video_id"];
	var token = yt.getConfig("SWF_ARGS")["t"];
	var title = yt.getConfig("VIDEO_TITLE").replace(/"/g, "-").replace(/%/g, "%25").replace(/=/g, "%3D").replace(/,/g, "%2C").replace(/&/g, "%26").replace(/#/g, "%23").replace(/\?/g, "%3F").replace(/\//g, "_").replace(/\\/g, "_");
	if (fmt_url_map[0] == "")
	{
		var video_id = yt.getConfig("SWF_ARGS")["video_id"];
		var token = yt.getConfig("SWF_ARGS")["t"];
		tabBodyDiv.appendChild(createDLLink("Low Definition, Low Quality (rename to video.flv)", false, "(FLV, Flash Video 1 (Sorenson Spark), MP3)", "http://" + document.location.host + "/get_video?video_id=" + video_id + "&t=" + token));
	}
	else
	{
		var recommended = null;
		var recommendedQual = 0;
		var mp4_listed = false;
		var mp4hd_listed = false;
		var numvideos = fmt_url_map.length;
		for (var fmt in fmt_url_map)
		{
			fmt = fmt_url_map[fmt].split("|");
			var href = fmt[1] + "&title=" + title;
			var dllink;
			switch (fmt[0])
			{
				case "5":
					dllink = createDLLink("Low Definition, Low Quality", false, "(Format 5) (FLV, Flash Video 1 (Sorenson Spark), MP3)", href);
					break;
				case "6":
					dllink = createDLLink("High Quality", false, "(Format 6) (FLV, Flash Video 1 (Sorenson Spark), MP3)", href);
					break;
				case "13":
					dllink = createDLLink("Low Quality Mobile Video", false, "(Format 13) (3GP, H.263, AMR)", href);
					break;
				case "17":
					dllink = createDLLink("High Quality Mobile Video", false, "(Format 17) (3GP, MPEG-4, AAC)", href);
					break;
				case "18":
					dllink = createDLLink("High Quality, iPod Compatible", true, "(Format 18) (MP4, H.264, AAC)", href);
					mp4_listed = true;
					break;
				case "22":
					dllink = createDLLink("High Definition, High Quality", true, "(720p, Format 22) (MP4, H.264, AAC)", href);
					mp4hd_listed = true;
					break;
				case "34":
					dllink = createDLLink("Low Definition, High Quality", false, "(Format 34) (FLV, H.264, AAC)", href);
					break;
				case "35":
					dllink = createDLLink("Standard Definition, High Quality", true, "(480p, Format 35) (FLV, H.264, AAC)", href);
					break;
				case "37":
					dllink = createDLLink("High Definition, Super High Quality", true, "(1080p, Format 37) (MP4, H.264, AAC)", href);
					break;
				default:
					dllink = createDLLink("Format " + fmt[0], false, "(Unknown)", href);
			}
			tabBodyDiv.appendChild(dllink);
			
			var qual = formatList[fmt[0]];
			if (qual)
				if (qual > recommendedQual)
				{
					recommended = dllink;
					recommendedQual = qual;
				}
		}
		if (!mp4_listed)
		{
			numvideos ++;
			var dllink = createDLLink("High Quality, iPod Compatible", true, "(Format 18, guessed) (MP4, H.264, AAC)", "http://" + document.location.host + "/get_video?video_id=" + video_id + "&t=" + token + "&fmt=18");
			var qual = formatList["18"];
			if (qual)
				if (qual > recommendedQual)
				{
					recommended = dllink;
					recommendedQual = qual;
				}
			tabBodyDiv.appendChild(dllink);
		}
		if (yt.getConfig('IS_HD_AVAILABLE') && !mp4hd_listed)
		{
			numvideos ++;
			var dllink = createDLLink("High Definition, High Quality", true, "(720p, Format 22, guessed) (MP4, H.264, AAC)", "http://" + document.location.host + "/get_video?video_id=" + video_id + "&t=" + token + "&fmt=22");
			var qual = formatList["22"];
			if (qual)
				if (qual > recommendedQual)
				{
					recommended = dllink;
					recommendedQual = qual;
				}
			tabBodyDiv.appendChild(dllink);
		}
		if (recommended && numvideos > 1)
		{
			var recommendedTag = document.createElement("b");
			recommendedTag.textContent = "(Recommended) ";
			recommended.insertBefore(recommendedTag, recommended.lastChild);
		}
	}
	tabBody.appendChild(tabBodyDiv);
}

var script = document.createElement("script");
script.type = "application/javascript";
script.textContent = "(" + YouTubeVideoDownload + ")();";

document.body.appendChild(script);
