// ==UserScript==
// @name Youtube Embedded Viewer Fix
// @author FyberOptic
// @namespace http://www.fybertech.com
// @version 1.0.3
// @description Fixes embedded Youtube videos, which
//				are problematic from the "type="
//				parameter in the embed tag.
// @exclude http://*youtube.com/*
// @ujs:category browser: fixes
// @ujs:published 2006-05-21 00:00
// @ujs:modified 2006-06-13 11:30
// ==/UserScript==

window.opera.addEventListener('BeforeEvent.load', // triggers right before the onload event
function(e) {	
	if (!(e.event.target instanceof Document)) return; // if not document loaded, halt processing
	
	// Find all EMBED tags
	var rows = document.getElementsByTagName('embed');
  	
	// Loop through EMBEDs
	for( var i = 0, row; row = rows[i]; i++ )
	{						
		if (row.parentNode.nodeName == 'OBJECT') continue;
		
		// Check if content is from Youtube
		if (row.getAttribute('src').indexOf('youtube.com') > 0)
		{
			// Create new EMBED tag, copy attributes
			var newElement = document.createElement('embed');			
			newElement.setAttribute('src',row.getAttribute('src'));
			newElement.setAttribute('width',row.getAttribute('width'));
			newElement.setAttribute('height',row.getAttribute('height'));					
			
			// Replace old EMBED with new working version
			row.parentNode.replaceChild(newElement,row);
			
		}
  	}
  	
  	
  	
  	// Find all OBJECT tags
  	rows = document.getElementsByTagName('object');
  	
  	// Loop through OBJECTs
	for( var i = 0, row; row = rows[i]; i++ )
	{
		var o_width = row.getAttribute('width');
		var o_height = row.getAttribute('height');
		var o_src = row.getAttribute('data');
		
		if (o_src.length < 1)
		{
			// Prepare to parse all inner elements of OBJECT
			var rows2 = row.all;
			
			// Parse through inner elements of OBJECT
			for (var n = 0,row2; row2 = rows2[n]; n++)
			{			
				// Look for PARAM tags
				if (row2.nodeName == 'PARAM')
				{
					// Look for Youtube movie
					if (row2.getAttribute('name') == 'movie') o_src = row2.getAttribute('value');							
				}
			}
		}
		
		if (o_src.indexOf('youtube.com') >= 0)
		{
			// Create new EMBED tag, copy attributes
			var newElement = document.createElement('embed');			
			newElement.setAttribute('src',o_src);
			newElement.setAttribute('width',o_width);
			newElement.setAttribute('height',o_height);
			
			// Replace OBJECT with new working EMBED version
			row.parentNode.replaceChild(newElement,row);			
			
			// We just dumped an OBJECT, so reduce i so that loop isn't incorrectly offset
			i--;
		}
	}
	
  	
}
,false);


