// ==UserScript==
// @name          Download Video from YouTube
// @description   adds a link to download flv from YouTube
// @include       *youtube.com/*v=*
// ==/UserScript==

//
// By IE7pro Team
// Ported from http://userscripts.org/scripts/show/3982
//    and from http://userscripts.org/scripts/show/4037
//

(function() {
 	function in2html(txt)
	{
		if (!txt) return '';
		var txthtml = txt.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
		return(txthtml);
	}

	function get_videourl()
	{
		var scriptmedia=new Array();

		scriptmedia=document.getElementsByTagName('SCRIPT');
		for(i=0;i<scriptmedia.length;++i) {
			source=scriptmedia[i].text.match(/video_id=\S+&.+&t=.+&/i);
			if (source!=null)
			{
				return in2html(String(source).replace(/(video_id=.+)&l=.+(&t=.+)&/i,'http:\/\/www.youtube.com\/get_video?$1$2'));
			}
		}
		return "";
	}

	//get video url from page source
	var videourl = get_videourl();
	//insert before the vidFacetsDiv
	var elm = document.getElementById("watch-video-details-inner");
	if(!elm) return;
	var elm_parent = elm.parentNode;

	var div = document.createElement("div");

	elm_parent.insertBefore(div, elm);

	div.innerHTML = '<style>#download{text-align:left;margin: 0 10px 0 20px;}.downloadImg{border: 0;float:left;}h2.downloadVid{font-size: 22px;display:inline;}</style><div id="download"><a href="'+videourl+'"><img src="data:image/gif;base64,R0lGODlhFAAUAOYAAP///3jJrvTl4+r38sjp3se0q67i0V2Wd9Xz6gCZZv3+/k2YdgCUX2rDpgCSW3HGqgCZZ2vEpvr9/ACQWf7//v7+/gCWYe/49W7FqPf8+nLGqq3i0Z3YxGPAoVS7mOT07gSXY9Xz62C/oHfJrVG5l02Yde349KXWxI3Ru5XUv4TNtf3z9PT6+QCOVXvLsMayqQCTXt3x6/D59hygcJbVwACVYcrv5HiWfVuUddr17c68tPz+/WrDpQCTXfX8+k6Ydtzx6ur383KSd6/h0uzo5KTayBqicyCmeQCQWK/j0tDs4gCOVvv6+UyXdUyWc5nWwVe8mo/SvAKXYwCWYvX7+eDz7ZHTvWbCo9Hv5tHt5JTVv2rCpJLTvvzy8m3Fp2zEpsbo3VGaeDesgkO0j8fo3fH692WihcLm2gCVX3rKr8Szqdjv6FyWd2m2mPDk4kK0jjethACXZACYZBijdO/59Rykd6aWh1e7mRyebVi8nOr28tHHv9Tu5SSne4TIsAAAACH5BAAAAAAALAAAAAAUABQAAAfygACCg4SFhoIyA4qLjHomFYQZKh08DQ1eD5kYllsRTz6DSh4fCqUZF3QXVKUKZVong2RphBwzdXguhENmK4IEAYQoEzUtV4QGOAK+wINWDAkOEccHygC/hFzPDl/T1dcUginaGILgBtTLAEFQYkdyCXFzcG9AAOfewApRE2gQCRAWkASQYA+dNWYSSMBIwLDHGBaC7qUTtKYPA4BGsgySeJBQERBTpNDoNlHQDhFL8igglIRNNTAjCsW4U6XQBoN8NGBBwBNBCBs5evL0c8ONoApn2jhpsmDBjzBNo5YQYocIoS46XhTYyrWrmj1MDok1FAgAOw==" class="downloadImg"><span class="downloadVid">Download Video</span></a></b></div>';

})();

