// ==UserScript==
// @name		Plugsome Bar
// @version		1.0
// @date		2008-05-30
// @author		Jo.o Eiras
// ==/UserScript==

/*
* Copyright (c) 2008, Jo.o Eiras
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of the <organization> nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY JO.O EIRAS ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL JO.O EIRAS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/*
	Plugsome Bar
	
	This script is an utility that add a small toolbar to plugins in webpages.
	The toolbar by default has 3 buttons:
	 - the 1st button is a REload button which reload the plugin content
	 - the 2nd button is a link to the external file that the plugin loaded.
	   Somefiles might get special treatment, like in youtube. Instead of linking
	   to youtube's media player swf, the script links to the video file.
	 - the 3rd button is a X which if clicked closes the toolbar, and if 
	   double-clicked removes the toolbar and plugin from the page. 
	   The space that the plugin occupied is preserved though.
	
	If the toolbar is attached to a Flash file, then you get two extra button s:
	a play and a pause button. Note however, that play and pause might not pause/resume
	plyback in all flash files. This is due to the file's internal structure, and the
	way content is layed out in the Flash's timeline.
	
	History:
	1.0 - 2008-05-30
	  * Initial release
	  
*/
(function(){
	/* Config */
	var youtubeHightQuality = false;

	/* rest of script */
	var script_version = '1.0';
	
	var urlHandlers = {};
	urlHandlers['.youtube.']=function(object, link){
		var fv = object.getAttribute('flashvars');
		if( fv ){
			var vidarg='',targ='';
			fv.replace(/[\?&](video_id|t)=([^&]+)/g,function($0,$1,$2){
				switch(String($1).toLowerCase()){
				case 'video_id':vidarg = $2;break;
				case 't': targ = $2;break;
				};
			});
			if( vidarg && targ ){
				link.href = 'http://www.youtube.com/get_video?video_id='+vidarg+'&t='+targ+(youtubeHightQuality?'&fmt=18':'');
				return true;
			};
		};
	};
	
	function isNodeAncestorOrSelfOf(ancestor,node){
		if( ancestor && node )
			do{
				if(node == ancestor)
					return true;
			}while(node=node.parentNode);
		return false;
	};
	function get_y(obj){
		var y = 0;
		if(obj)
			do{
				y += obj.offsetTop;
			}while(obj=obj.offsetParent);
		return y;
	}
	function get_x(obj){
		var x = 0;
		if(obj)
			do{
				x += obj.offsetLeft;
			}while(obj=obj.offsetParent);
		return x;
	}
	function fillObjectLink(o, link){
		var host = location.host, linkhref;
		//youtube
		for(var h in urlHandlers){
			if( host.indexOf(h)>=0 ){
				if( urlHandlers[h](o,link) )
					return true;
			}
		}
		return fillObjectLinkFallback(o, link);
		
	};
	function fillObjectLinkFallback(o, link){
		//generic treatment
		linkhref = o.getAttribute('data')||o.getAttribute('src')||
			(o.getAttribute('type')&&o.getAttribute('type').indexOf('java')>=0?o.getAttribute('archive'):'');
		if( linkhref ){
			link.href = linkhref;
			return true;
		};
		var names=['movie','data','src','code','filename','url'];
		for(var m=0,n;n=names[m];m++){
			link = o.selectSingleNode('param[translate(@name,"'+n.toUpperCase()+'","'+n+'")='+
				'translate("'+n+'","'+n.toUpperCase()+'","'+n+'")][@value]');
			if( link ){
				link.href = link.getAttribute('value');
				return true;
			};
		};
		return false;
	};
		
	var clones = [];
	function resetPositions(){
		for(var k=0,m;k<clones.length;k++){
			m=clones[k];
			var o = m.__object;
			if( o && o.parentNode ){
				m.style.top =(get_y(o)-m.offsetHeight)+'px!important';
				m.style.left = get_x(o)+'px!important';
			}
			else if( m && m.parentNode ){
				//if object is deleted from document, then remove toolbar
				m.parentNode.removeChild(m);
				clones[k] = null;
			}
		};
	};
	var def_styles = 'border:steelblue thin solid!important;vertical-align:middle!important;text-align:center!important;'+
		'background-color:white!important;padding:0!important;margin:0!important;display:inline-block!important;'+
		'font-family:sans-serif!important;font-weight:bold!important;color:steelblue!important;height:100%!important;'+
		'font-size:10px!important;text-decoration:underline!important;width:48px!important;box-sizing:border-box!important;';
		
	function patchObject(o,data){
		var is_flash = (o.getAttribute('type')||'').indexOf("shockwave")>=0;
		if(!data.template){
			data.fragment = document.createDocumentFragment();
			
			var template = data.template = document.createElement('plugsometoolbar');
			template.style='border:white thin solid!important;padding:0!important;margin:0!important;position:absolute!important;font-size:10px!important;z-index:9999!important;height:18px!important;width:218px!important;box-sizing:border-box!important;opacity:0.08!important';
			template.innerHTML = '<button style="'+def_styles+'">Reload</button>'+
				'<a style="'+def_styles+'color:gray!important;">No link</a>'+
				'<button title="Click to close, double-click to delete." style="'+def_styles+
					';font-family:Verdana!important;font-size:110%!important;width:24px!important">X</button>';
		}
		if( is_flash && !data.flash_template){
			var template = data.flash_template = document.createElement('plugsometoolbarflash');
			template.innerHTML += 
				'<button title="Click to pause" style="'+def_styles+';font-family:Verdana!important;width:48px!important">Pause</button>'+
				'<button title="Click to play" style="'+def_styles+';font-family:Verdana!important;width:48px!important">Play</button>';
		}
		var menu = data.template.cloneNode(true);
			
		clones.push(menu);
		menu.__object = o;
		o.__plugin_menu = menu;

		menu.onmouseover=function(e){
			resetPositions();
			this.style.opacity='1!important';
		};
		menu.onmouseout=function(e){
			if(!isNodeAncestorOrSelfOf(this,e.relatedTarget))
			this.style.opacity='0.08!important';
		};
		menu.firstChild.onclick=function(){
			var o = this.parentNode.__object;
			var ns = o.nextSibling;
			var p = o.parentNode;
			o.outerHTML = o.outerHTML;
			this.parentNode.__object = (ns ? ns.previousSibling : p.lastChild);
		}
		var closeButton = menu.getElementsByTagName('button')[1];
		closeButton.ondblclick=function(){
			this.__clicking = false;
			var o = this.parentNode.__object;
			if( o ){ o.outerHTML = o.outerHTML.replace(/(src|data|value|archive|type)=(["'])[^"']*\2/g,''); }
			this.parentNode.parentNode.removeChild(this.parentNode);
		};
		closeButton.onclick=function(){
			this.__clicking = true;
			setTimeout(function(obj){
				if(obj.__clicking)
					obj.parentNode.parentNode.removeChild(obj.parentNode);
			},250,this);
		};
			
		if( is_flash ){
			var flashcontrols = data.flash_template.cloneNode(true);
			menu.appendChild(flashcontrols);
						
			var pauseButton = flashcontrols.getElementsByTagName('button')[0];
			var playButton = flashcontrols.getElementsByTagName('button')[1];
			
			pauseButton.onclick=function(){
				var o = this.parentNode.parentNode.__object;
				o.StopPlay();
			};
			playButton.onclick=function(){
				var o = this.parentNode.parentNode.__object;
				o.Play();
			};
		};
		
		var link=menu.childNodes[1];
		if( fillObjectLink(o,link) ){
			link.style.color='steelblue!important';
			link.textContent='Link';
		};
		
		data.fragment.appendChild(menu);
	};
	
	var data = {template:null,fragment:null};
	if( opera ){
		opera.addEventListener('PluginInitialized',function(e){
			patchObject(e.element,data);
			resetPositions();
			document.documentElement.appendChild(data.fragment);
		},false);
		opera.addEventListener('AfterEvent.resize',resetPositions,false);
		
		opera.PlugsomeToolbar = {
			version: script_version,
			URLHandlers: urlHandlers,
			URLHandlerFallback: fillObjectLinkFallback,
			AllToolbars: clones
		};
	}
	else{
		addEventListener('load',function(){
			var k=0;
			for(var o,os=document.selectNodes('//object[@type and @data] | //embed[not(ancestor::object[@type and @data]) and @type and @src]');o=os[k];k++){
				setTimeout(patchObject,k*50,o,data);
			};
			setTimeout(function(){
				if(data.fragment)
					document.documentElement.appendChild(data.fragment);
				resetPositions();
			},k*50);
		},false);
		addEventListener('resize',resetPositions,false);
	}

})( window.opera );










