// ==UserScript==
// @name Download embeds
// @author TarquinWJ 
// @namespace http://www.howtocreate.co.uk/ 
// @version 1.2
// @description  Puts a link beside every object and embed on the page
//			to allow you to download the associated file.
// @ujs:category general: enhancements
// @ujs:published 2005-05-30 23:02
// @ujs:modified 2005-09-19 09:19
// @ujs:documentation http://userjs.org/scripts/general/enhancements/download-embeds 
// @ujs:download http://userjs.org/scripts/download/general/enhancements/download-embeds.js
// ==/UserScript==


/* 
 * Please see
 * http://www.howtocreate.co.uk/operaStuff/userJavaScript.html#terms
 * for License and Terms of Use
 */

document.addEventListener( 'load', function () {
	if( !document.documentElement ) { return; }
	document.documentElement.addEventListener(
		'dblclick',
		function () {
			if( window.checkForPlugsFooBarObbaTinkle ) { return; }
			var y = [document.getElementsByTagName('object'),document.getElementsByTagName('embed'),document.getElementsByTagName('applet')];
			for( var x = 0; y[x]; x++ ) { for( var z = 0; y[x][z]; z++ ) {
				var oSrc = y[x][z].getAttribute('data');
				if( !oSrc ) { oSrc = y[x][z].getAttribute('src'); }
				if( !oSrc ) { for( var g = 0, h; h = y[x][z].childNodes[g]; g++ ) {
					if( !h.tagName ) { continue; }
					var pName = h.getAttribute('name');
					if( ( pName == 'data' ) || ( pName == 'movie' ) || ( pName == 'src' ) ) {
						oSrc = h.getAttribute('value');
						if( oSrc ) { break; }
				} } }
				if( !oSrc ) { oSrc = y[x][z].getAttribute('code'); }
				if( !oSrc ) { oSrc = y[x][z].getAttribute('archive'); }
				if( oSrc ) {
					var oLink = document.createElement('a');
					oLink.setAttribute('href',oSrc);
					oLink.appendChild(document.createTextNode(' Download '+y[x][z].tagName));
					oLink.className = 'pdownlink';
					y[x][z].parentNode.insertBefore(oLink,y[x][z]);
				}
			} }
			window.checkForPlugsFooBarObbaTinkle = true;
		},
		false
	);
}, false );
