// Add Styling
function addGlobalStyle(css) {
    var head, style;
    head = document.getElementsByTagName('head')[0];
    if (!head) { return; }
    style = document.createElement('style');
    style.type = 'text/css';
    style.innerHTML = css;
    head.appendChild(style);
}

function main() {

var videoPlayer = document.getElementById('watch-player');
if (videoPlayer==null) return;
var flashValues=videoPlayer.innerHTML;

addGlobalStyle('.spaced { padding: 4px 1px 4px 1px !important; font-size:100% !important; margin-left: 1px !important;}');

var videoIdMatches=flashValues.match(/\&video_id=([^(\&|$)]*)/);
var id=(videoIdMatches!=null)?videoIdMatches[1]:null;
var videoTicketMatches=flashValues.match(/\&t=([^(\&|$)]*)/);
var t=(videoTicketMatches!=null)?videoTicketMatches[1]:null;
if (id==null || t==null) return;  

//get fmt
var videoFormatsMatches=flashValues.match(/\&fmt_url_map=([^(\&|$)]*)/);
var videoFormats=(videoFormatsMatches!=null)?videoFormatsMatches[1]:null; 
// parse fmt_url_map
var videoURL=new Array();
var isFinalURL=new Array();
var videoFormatsGroup=videoFormats.split('%2C');
for (var i=0;i<videoFormatsGroup.length;i++){
    var videoFormatsElem=videoFormatsGroup[i].split('%7C');
    videoURL[videoFormatsElem[0]]=unescape(videoFormatsElem[1]);
    isFinalURL[videoFormatsElem[0]]=true;
  }

// Set the basic download url to add formats to it later
var url = 'http://youtube.com/get_video?video_id='+id+'&t='+t+'&asv=';
var yturl = 'http://www.youtube.com/watch?v=';

// Flv download
var dflv = document.createElement('a');
dflv.textContent = 'FLV';
dflv.setAttribute('title', 'Click to download video in FLV format');
dflv.setAttribute('rel', 'nofollow');
dflv.setAttribute('class', 'actionLink spaced'); //master-sprite-new yt-uix-button yt-uix-tooltip active
dflv.href = url;
dflv.setAttribute('onclick', 'EasyYoutubeLinkEvent(event);');


// 3gp download
var d3gp = document.createElement('a');
d3gp.textContent = '3GP';
d3gp.setAttribute('title', 'Click to download video in 3GP format');
d3gp.setAttribute('rel', 'nofollow');
d3gp.setAttribute('class', 'actionLink spaced')//;master-sprite-new yt-uix-button yt-uix-tooltip active
d3gp.href = url + '&fmt=17';
d3gp.setAttribute('onclick', 'EasyYoutubeLinkEvent(event);');

// Mp4 download
var dmp4 = document.createElement('a');
dmp4.textContent = 'MP4';
dmp4.setAttribute('title', 'Click to download video in MP4 format');
dmp4.setAttribute('rel', 'nofollow');
dmp4.setAttribute('class', 'actionLink spaced'); //master-sprite-new yt-uix-button yt-uix-tooltip active
dmp4.href = url + '&fmt=18';
dmp4.setAttribute('onclick', 'EasyYoutubeLinkEvent(event);');

// HD download
var d720p = document.createElement('a');
d720p.textContent = 'HD 720p';
d720p.setAttribute('title', 'Click to download video in 720p High-Definition format');
d720p.setAttribute('rel', 'nofollow');
d720p.setAttribute('class', 'actionLink spaced')//;master-sprite-new yt-uix-button yt-uix-tooltip active
d720p.href = url + '&fmt=22';
d720p.setAttribute('onclick', 'EasyYoutubeLinkEvent(event);');

// Full-HD download
var d1080p = document.createElement('a');
d1080p.textContent = 'Full-HD 1080p';
d1080p.setAttribute('title', 'Click to download video in 1080p High-Definition format');
d1080p.setAttribute('rel', 'nofollow');
d1080p.setAttribute('class', 'actionLink spaced'); //master-sprite-new yt-uix-button yt-uix-tooltip active
d1080p.href = url + '&fmt=37';
d1080p.setAttribute('onclick', 'EasyYoutubeLinkEvent(event);');

// MP3
//Retrieve Video Name
var vdtitle = document.title.split('YouTube - ')[1];
// Clean out illegal filename chracters
ilchr = /\$|,|@|#|~|`|\%|\*|\^|\&|\(|\)|\+|\=|\_|\}|\{|\;|\:|\'|\"|\<|\>|\?|\||\\|\!|\$|\./g;
// remove special characters like "$" and "," etc...
vdtitle = vdtitle.replace(ilchr, "");
vdtitle = vdtitle.replace(/\//g, "");
vdtitle = vdtitle.replace("[", "");
vdtitle = vdtitle.replace("]", "");

//retrieve video id
vname = 'v';
var regexS = "[\\?&]"+vname+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
    vname = "";
  else    
    vname = results[1];

var dmp3 = document.createElement('a');
dmp3.textContent = 'MP3';
dmp3.setAttribute('title', 'Get this video as MP3');
dmp3.setAttribute('target', '_blank');
dmp3.setAttribute('class', 'actionLink spaced'); //master-sprite-new yt-uix-button yt-uix-tooltip active
dmp3.href = 'http://www.youtubedroid.com/list.php?term='+vdtitle+'&v='+id+'&new=y&org=ch-addon';

var dhlp = document.createElement('a');
dhlp.textContent = 'Help [?]';
dhlp.setAttribute('title', 'Get Help');
dhlp.setAttribute('target', '_blank');
dhlp.setAttribute('class', 'actionLink spaced'); //master-sprite-new yt-uix-button yt-uix-tooltip active
dhlp.href = 'http://www.megaleecher.net/Free_Google_Chrome_Extension_To_Download_YouTube_Videos';

var d720pn = document.createElement('span');
d720pn.textContent = '720p Not available';

var d1080pn = document.createElement('span');
d1080pn.textContent = '1080p Not available';

var liflv = document.createElement('li');
liflv.setAttribute('class', 'yt-uix-button-menu-item');
liflv.appendChild(dflv);
var li3gp = document.createElement('li');
li3gp.setAttribute('class', 'yt-uix-button-menu-item');
li3gp.appendChild(d3gp);
var limp4 = document.createElement('li');
limp4.setAttribute('class', 'yt-uix-button-menu-item');
limp4.appendChild(dmp4);

var lihd = document.createElement('li');
lihd.setAttribute('class', 'yt-uix-button-menu-item');
if (videoURL['22']!=undefined){
lihd.appendChild(d720p);
}
else{
lihd.appendChild(d720pn);
}

var lifhd = document.createElement('li');
lifhd.setAttribute('class', 'yt-uix-button-menu-item');
if (videoURL['37']!=undefined){
lifhd.appendChild(d1080p);
}
else{
lifhd.appendChild(d1080pn);
}
var limp3 = document.createElement('li');
limp3.setAttribute('class', 'yt-uix-button-menu-item');
limp3.appendChild(dmp3);

var lihlp = document.createElement('li');
lihlp.setAttribute('class', 'yt-uix-button-menu-item');
lihlp.appendChild(dhlp);
 
var ul = document.createElement('ul');
ul.setAttribute ('class', 'yt-uix-button-menu hid');
ul.setAttribute ('style', 'left: 411px; top: 588px; min-width: 76px; display: none;');
ul.appendChild(liflv);
ul.appendChild(li3gp);
ul.appendChild(limp4);
ul.appendChild(lihd);
ul.appendChild(lifhd);
ul.appendChild(limp3);
ul.appendChild(dhlp);

var img = document.createElement('img');
img.setAttribute('class', 'yt-uix-button-arrow');
img.setAttribute('src', 'http://s.ytimg.com/yt/img/pixel-vfl73.gif');

var span = document.createElement('span');
span.setAttribute('class', 'yt-uix-button-content');
span.textContent =('Save video as');

var button = document.createElement('button');
button.setAttribute('class', 'yt-uix-button yt-uix-tooltip');
button.setAttribute('onclick', ';return false;');
button.setAttribute('data-tooltip-title', "Right click and select click 'Save Link As..' to download");
button.setAttribute('data-tooltip-timer', '625');
button.appendChild(span);
button.appendChild(img);
button.appendChild(ul);
 
var wpd = document.getElementById('watch-actions-right'),

div = document.createElement('span');
div.setAttribute('id', 'downloadfromyoutube');
div.setAttribute('align', 'center');
div.appendChild(button);

wpd.appendChild(div);


// content.document is current web page, or use the page document that your creating links in
var doc = content.document;
var script = doc.createElement('script');
script.type = 'application/x-javascript';
script.textContent = ' function EasyYoutubeLinkEvent(event){ '
                     +'    event.preventDefault(); ' // stop normal action
                     +'    var element = event.target; ' // should be the link
                     +'    var ev = document.createEvent("Events"); '
                     +'    ev.initEvent("EasyYoutubeLinkEvent", true, false); '
                     +'    element.dispatchEvent(ev); '
                     +' } ';
doc.getElementsByTagName("head")[0].appendChild(script);
}

window.addEventListener('load', main(), false);