// ==UserScript==
// @name           IH8MUD Automatic YouTube Embedder
// @description	   Please report any problems to NaterGator
// @namespace      http://nate-online.com
// @include        http://forum.ih8mud.com/*
// ==/UserScript==

var allAnchors,ytParseObj = new Object();

function makeNewParseObject(thisAnchor) { 
 return function() {
   GM_xmlhttpRequest({method: 'POST', headers: {'Content-type':'application/x-www-form-urlencoded'}, data: encodeURI('next_url=' + thisAnchor.pathname + thisAnchor.search + '&action_confirm=Confirm Birth Date') , url: 'http://youtube.com/verify_age?next_url=' + escape(thisAnchor.pathname + thisAnchor.search), onload: function(ytPg) {
    var ytContainer, vidTitle, queryParts;
    ytContainer = document.createElement("table");
    ytContainer.setAttribute('class','tborder');
    ytContainer.cellPadding='6';
    ytContainer.cellSpacing='1';
    ytContainer.border='0';
    ytContainer.width='466';
    queryParts = thisAnchor.search.substr(1).split('&');
    for(var j=0; j < queryParts.length; j++)
      if(queryParts[j].split('=',1) == 'v') {
       if(thisAnchor.text.replace(' ','') == thisAnchor.href) {
  	    if( ytPg.status == 200 ) {
	      var titleRegex = /<meta name="title" content="(.+?)">/gmi;
	      var titleResult = titleRegex.exec( ytPg.responseText );
	      titleRegex.lastIndex=0;
	      if( titleResult )
		vidTitle = titleResult[1];
	      else
		vidTitle = thisAnchor.innerHTML;
	    } else
	      vidTitle = thisAnchor.href;
	    titleResult=null;
    	} else
	  vidTitle = thisAnchor.innerHTML.replace('YouTube - ','');
	  ytContainer.innerHTML = '<tr><td style="text-align:center"><a href="' + thisAnchor.href + '" target="_blank">' + vidTitle + '</a></strong></td></tr>';
	  ytContainer.innerHTML += '<tbody><tr><td class="alt1" align="center"><object width="419" height="351" type="application/x-shockwave-flash" data="http://www.youtube.com/v/' + queryParts[j].split('=')[1] + '&ap=%2526fmt%3D18&rel=0"><param name="movie" value="http://www.youtube.com/v/' + queryParts[j].split('=')[1] + '&ap=%2526fmt%3D18&rel=0" /><param name="wmode" value="transparent" /><em><strong>ERROR:</strong> If you can see this, then <a href="http://www.youtube.com/">YouTube</a> is down or you don\'t have Flash installed.</em></object></td></tr></tbody>';
	  break;
     }
    
    queryParts = null;
    vidTitle = null;
    thisAnchor.parentNode.insertBefore(ytContainer, thisAnchor);
    thisAnchor.parentNode.removeChild(thisAnchor);
    thisAnchor = null;
    ytContainer = null;
  }});
 }
}

allAnchors = document.evaluate(
    "//div[contains(@id,'post_message')]/a[contains(@href,'youtube.com/watch')]", //only includes youtube links inside posts, not profiles
    document,
    null,
    XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
    null);

var max = allAnchors.snapshotLength;
if(allAnchors.snapshotLength >= 10)
  if(!confirm('There are '+ allAnchors.snapshotLength + ' youtube links on this page.\nPress Ok to load them all, or Cancel to only load the first 10.')) max = 10;

for (var i = 0; i < max; i++) {
  ytParseObj[i] = makeNewParseObject(allAnchors.snapshotItem(i));
  ytParseObj[i]();
 }
allAnchors = null;