// ==UserScript==
// @name           Download YouTube Videos as MP4
// @description    Adds an option to download YouTube videos.
// @namespace      http://googlesystem.blogspot.com
// @include        http://*.youtube.com/watch?*
// @version        0.97
// ==/UserScript==

(function () {
  
    if (document.getElementById('download-youtube-video')) return;

    var args=null, video_title=null, video_id = null, video_hash = null;
    var download_code = new Array();
    var fmt_labels = {'18':'standard MP4', '22':'HD 720p', '37':'HD 1080p'};
    
    try{
      args = unsafeWindow.yt.getConfig('SWF_ARGS');
      video_title = unsafeWindow.yt.getConfig('VIDEO_TITLE');
    }
    catch(e){}
    if (args) {      
      var fmt_url_map = args['fmt_url_map'];
      
      if (fmt_url_map=='') return;
      
      video_id = args['video_id'];
      video_hash = args['t'];
      video_title = video_title.replace(/[\"\'\?\\\/\:\*\<\>]/g, '');
           
      var fmt = new Array();
      var formats = fmt_url_map.split('%2C');
      var format;
      for (var i=0; i<formats.length; i++) {
        var format_elems = formats[i].split('%7C');
        fmt[format_elems[0]]=unescape(format_elems[1]);
      }
      for (format in fmt_labels) {
        if (fmt[format]!=null) {
        download_code.push('<a href=\''+(fmt[format]+'&title='+video_title)+'\'>'+fmt_labels[format]+'</a>');
        } else if (format=='18') { 
        download_code.push('<a href=\'http://www.youtube.com/get_video?fmt=18&video_id='+video_id+'&t='+video_hash+'\'>'+fmt_labels[format]+'</a>')
        }
      }
           
    }
    
    if (video_id==null || video_hash==null) {
    
      var video_player = document.getElementById('movie_player');
    
      if (video_player) {
      
        var flash_variables = video_player.attributes.getNamedItem('flashvars');  
        if (flash_variables) {
          var flash_values = flash_variables.value;
          if (flash_values) {
            var video_id_match = flash_values.match(/[^a-z]video_id=([^(\&|$)]*)/);
            if (video_id_match!=null) video_id = video_id_match[1];
            var video_hash_match = flash_values.match(/[^a-z]t=([^(\&|$)]*)/);
            if (video_hash_match!=null) video_hash = video_hash_match[1];        
          }
        }
        
        download_code.push('<a href=\'http://www.youtube.com/get_video?fmt=18&video_id='+video_id+'&t='+video_hash+'\'>'+fmt_labels['18']+'</a>');
        
        try{
           if (unsafeWindow.yt.getConfig('IS_HD_AVAILABLE')) {
        download_code.push('<a href=\'http://www.youtube.com/get_video?fmt=22&video_id='+video_id+'&t='+video_hash+'\'>'+fmt_labels['22']+'</a>');
           }
         }
         catch(e){} 

      } 
      
   }      
        
  if (video_id==null || video_hash==null) return;
 
    var div_embed = document.getElementById('watch-embed-div');
    if (div_embed) {
      var div_download = document.createElement('div');
      div_download.innerHTML = '<br /> <span id=\'download-youtube-video\'>Download: ' + download_code.join(' | ') +'</span>';         
      div_embed.appendChild(div_download);
    }       
  
})();
