/*
@source: http://drop.by/WebOsCmd-0.03.js
@licstart
Copyright (C) 2010  Richard H. Tingstad

The JavaScript code in this page is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version.  The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the GNU GPL for more details.
See <http://www.gnu.org/licenses/>.

As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend
Version 0.03 Beta
See bottom of file for init/client script.
*/
var c=new Object();
c['a']=['http://ask.com/web?q=%p','Ask.com search'];
c['b']=['http://thepiratebay.org/s/?q=%p&page=0&orderby=99','Bittorrent search (piratebay)'];
c['c']=[calc,'Calculate (eval)'];
c['d']=['http://www.google.com/search?q=define:%p','Define word (Google)'];
c['del']=['http://delicious.com/save?url=%u&title=%t&v=5&jump=yes&notes=%p','Post to delicious'];
c['e']=['mailto:?subject=%t&body=%p%0A%s%0A%22%22%0A%s%0A%u%0A%0A','Email link'];
c['f']=['http://flickr.com/search/?q=%p','Flickr search'];
c['fb']=['http://www.facebook.com/sharer.php?u=%u&t=%t','Share page on Facebook'];
c['g']=['http://www.google.com/search?q=%p','Google search'];
c['gc']=['http://google.com/search?strip=1&q=cache:%u%20%p','Google cache'];
c['gm']=['https://mail.google.com/mail?view=cm&tf=0&su=%t&body=%p%0A%s%0A%22%22%0A%s%0A%u%0A%0A','Gmail link'];
c['gs']=['http://google.com/search?q=%p%20site:%d','Search site with Google'];
c['h']=[h,'Help'];
c['H']=[he,'Help (all commands)'];
c['i']=['http://www.imdb.com/find?s=all&q=%p','Imdb search'];
c['l']=[len,'String length'];
c['ln']=['script:http://drop.by/files/direct-link.0.32.user.js','Link all in-URL URLs'];
c['m']=['http://maps.google.com/maps?q=%p','Google maps'];
c['num']=[num,'Eng. standard gematria (a=1,..,z=26)'];
c['nvm']=[nvm,'Eng. simple gematria (i=j,u=v)'];
c['o']=['http://%p','Open url'];
c['p']=['http://www.google.com/images?q=%p','Google image search'];
c['q']=['http://quixapp.com/go/?c=%p&t=%t&s=&v=080&u=%u&mode=direct','Quix'];
c['r']=['script:http://drop.by/regexp-find.user.js','Regular expression find'];
c['sms']=['https://www.gulesider.no/mypage/sendSms.c?text=%p','Send SMS'];
c['t']=['http://www.gulesider.no/gs/categoryList.c?q=%p&x=0&y=0','Gule sider'];
c['tw']=['http://search.twitter.com/search?q=%p','Twitter search'];
c['yt']=['http://www.youtube.com/results.php?search_query=%p','YouTube search'];
c['yub']=['http://yubnub.org/parser/parse?command=%p','YubNub'];
c['w']=['http://wikipedia.org/wiki/Special:Search/%p','Wikipedia search'];
c['z']=['http://www.amazon.com/s/?field-keywords=%p','Amazon search'];

function tingstad(u,selection,title,url){
 selection=''+selection;
 var p=u.indexOf(' ');
 var n=false;
 if(p==0){
  u=u.substr(1);
  p=u.indexOf(' ');
  n=true;
 }
 var t='';
 if(p>-1){
  t=u.substr(p+1);
  u=u.substr(0,p);
 }
 if(typeof c[u]=='undefined')return '';
 var f=c[u][0];
 if(typeof f=='function')f(t,selection,title,url);
 else if(f.indexOf('script:')==0)sc(f.substr(7));
 else if(n) return fix(f,t,selection,title,url);
 else r(f,t,selection,title,url);
 return '';
}
function fix(cmd,txt,selection,title,url){
 var i=url.indexOf('://')+3;
 var d=url.substring(i,url.indexOf('/',i));
 var e=encodeURIComponent;
 var u=e(url);
 var sel=e(selection);
 txt=txt.replace('%t',title).replace('%u',url).replace('%d',d).replace('%s',selection);
 var s=cmd.indexOf('%s');
 if(s==-1&&txt=='')txt=selection;
 else if(s>-1){
  var p=cmd.substr(s+1).indexOf('%s');
  if(p>-1){
   p+=s+1;
   if(selection=='')cmd=cmd.substr(0,s)+cmd.substr(p+2);
   else{
    var m=(s+p)/2+1;
    cmd=cmd.substr(0,s)+cmd.substring(s+2,m)+sel+cmd.substring(m,p)+cmd.substr(p+2);
   }
  }
  else cmd=cmd.replace('%s',sel);
 }
 return cmd.replace('%p',e(txt)).replace('%t',title).replace('%u',u).replace('%d',d);
}
function r(cmd,txt,selection,title,url){
 window.location=fix(cmd,txt,selection,title,url);
}
function sc(u){
 var n=u.replace(/[^a-zA-Z0-9]/,'');
 var s=document.getElementById(n);
 if(s)s.parentNode.removeChild(s);
 s=document.createElement('script');
 s.src=u;
 s.id=n;
 s.type='text/javascript';
 document.getElementsByTagName('HEAD')[0].appendChild(s);
}
function h(){
 var s='';
 var j=['d','fb','g','gm','gs','h','H','i','m','o','w'];
 for(var i in j){
  s+=j[i]+' '+c[j[i]][1]+'\n';
 }
 s+='\n%u url, %d domain, %s selected text\n\nExample: "g foo" searches for foo. %s is used if no argument is given.\n\nIf you start a command with space, it will open in a new tab or window.';
 alert(s);
 WebOsCmd();
}
function he(){
 var s='';
 for(var i in c){
  s+=i+' '+c[i][1]+'\n';
 }
 alert(s);
 WebOsCmd();
}
function len(t,s){
 if(t=='')t=s;
 alert(t.length);
}
function calc(t,s){
 if(t=='')t=s;
 else t=t.replace('%s',s);
 alert(eval(t));
}
function num(t,s){
 if(t=='')t=s;
 gematria(t,false);
}
function nvm(t,s){
 if(t=='')t=s;
 gematria(t,true);
}
function gematria(t,e){
 var s=0;
 t=t.toUpperCase();
 for(var i=0;i<t.length;i++){
  var j=t.charCodeAt(i);
  if(j>90) j=0;
  else if(j>64){
   if(e){
    if(j>85) j-=2;
    else if(j>73) j--;
   }
   j-=64;
  }
  else if(j>47 && j<58) j-=48;
  else j=0;
  s+=j;
 }
 alert(s);
}
/*
javascript:WebOsCmd();function WebOsCmd(){var b=false;var h=document.getElementsByTagName('head');if(h.length==0){alert('Error');return;}var s=document.getElementById('weboscmd');if(s)s.parentNode.removeChild(s);s=document.createElement('script');s.src='http://drop.by/WebOsCmd-0.03.min.js';s.id='weboscmd';s.type='text/javascript';s.onload=function(){b=true;};s.onreadystatechange=function(){if(s.readyState=='complete'||s.readyState=='loaded')b=true;};h[0].appendChild(s);var t=window.getSelection?window.getSelection():(document.getSelection?document.getSelection():(document.selection?document.selection.createRange().text:''));var c=window.prompt('Type \'h\' for a list of commands:','');if(c){var o=function(){if(b){s=tingstad(c,t,document.title?encodeURIComponent(document.title):'',''+document.location);if(s!=''){s=window.open(s);s.focus();}}else setTimeout(o,99);};o();}}

javascript:WebOsCmd();
function WebOsCmd(){
	var b=false; //Is script file loaded?
	var h=document.getElementsByTagName('head');
	if(h.length==0){
		alert('Error');
		return;
	}
	var s=document.getElementById('weboscmd');
	if(s)s.parentNode.removeChild(s);
	s=document.createElement('script');
	s.src='http://drop.by/WebOsCmd-0.03.min.js';
	s.id='weboscmd';
	s.type='text/javascript';
	s.onload=function(){b=true;};
	s.onreadystatechange=function(){if(s.readyState=='complete'||s.readyState=='loaded')b=true;};
	h[0].appendChild(s);
	var t=window.getSelection?window.getSelection():(document.getSelection?document.getSelection():(document.selection?document.selection.createRange().text:''));
	var c=window.prompt('Type \'h\' for a list of commands:','');
	if(c){
		var o=function(){
			if(b){
				s=tingstad(c,t,document.title?encodeURIComponent(document.title):'',''+document.location);
				if(s!=''){ //Open new window here so IE doesn't block.
					s=window.open(s);
					s.focus();
				}
			} //Do not send command until script file has loaded.
			else setTimeout(o,99);
		};
		o();
	}
}
*/
