var undefined;

//bookmarklet
//--------------------------------------------------------------------------
function dr4gg3r() {
  var _dragged={px:0,py:0};
  var _doky=document.body.style.background;
  document.body.style.background='red';
  setTimeout(function(){document.body.style.background=_doky;},1000);
  
  window.onmousemove=function(e){
   return function(e) {
     e=window.event || e;
     
     if (e.target && typeof e.target.____________________no=='undefined') {
       var tete=e.target;
       if (typeof tete.______________y=='undefined') {
         tete.______________y=1;
  
         tete.__________onselectstart=tete.onselectstart;
         tete.__________onclick=tete.onclick;
         tete.__________onselect=tete.onselect;
         tete.__________onmousedown=tete.onmousedown;
         tete.__________onmouseover=tete.onmouseover;
         tete.__________onmouseout=tete.onmouseout;
         if (typeof tete.style!='undefined')
           tete.__________cursor=tete.style.cursor;
  
         tete.onselectstart=function(){ return false; };
         tete.onclick=function(){ return false; };
         tete.onselect=function(){ return false; };
         tete.onmousedown=function(){ return false; };
         if (typeof tete.style!='undefined')
           tete.style.cursor='default';
       }  
     }
     
     var ttt=_dragged.target;
     if (typeof ttt!='undefined') {
       var xx=0;
       var yy=0;
       if (typeof ttt.____________x!=='undefined')
         xx=ttt.____________x;
       if (typeof ttt.____________y!=='undefined')
         yy=ttt.____________y;
       
       var divider=12;
       ttt.style.left=(parseInt((e.clientX-_dragged.px+xx)/divider)*divider)+'px';
       ttt.style.top =(parseInt((e.clientY-_dragged.py+yy)/divider)*divider)+'px';
     }
   }  
  }();
  
  window.onmouseup=function(e){
   return function(e) {
     e=window.event || e;
     _dragged={px:0,py:0};
     var tete=e.target;
     tete.____________x=parseInt(tete.style.left);
     tete.____________y=parseInt(tete.style.top);
   }  
  }();
  
  window.onmousedown=function(e){
   var origWMD=window.onmousedown;           
   return function(e) {
     e=window.event || e;             
     if (origWMD) origWMD(e);
     if (!e) e=window.event;
     
     var trg=e.target;
     if (trg.style.position!=='relative')
       trg.style.position='relative';
     _dragged.target=trg;
     _dragged.px=e.clientX;
     _dragged.py=e.clientY;
   }
  }();
  return void(0);
}


//bookmarklet
//--------------------------------------------------------------------------
function cl0cky(){
  var _dragged={px:0,py:0};
  var _doky=document.body.style.background;
  document.body.style.background='red';
  setTimeout(function(){document.body.style.background=_doky;},1000);
  
  function size(o,x,y,w,h){var s=o.style; s.position='absolute';s.left=x+'px';s.top=y+'px';s.width=w+'px';s.height=h+'px';};
  var l='0',t='0',w='100',h='100';
  var C=document.createElement('div');
  
  C._____________x=1;
  size(C,l,t,w,h);
  //C.style.border='1px solid white';
  //C.style.background='orange';
  //C.style.padding='1px';

  var captionHeight=15;  
  var D=document.createElement('div');
  size(D,l,t,w,captionHeight);
  D.style.background='red';
  D.____________________no=1;
  C.appendChild(D);
  
  var IF=document.createElement('iframe');
  var ifname='_________________________cl0cky________';
  IF.name=ifname;
  IF.style.border='none';
  IF.____________________no=1;      
  size(IF,l,t,w,h-captionHeight);
  IF.src='http://www.sebastian.it/website/href/2010-05/21/theClock.html';

  C.appendChild(IF);
  document.body.appendChild(C);

  D.dragThis=C;      
  
  window.onmousemove=function(e){
   return function(e) {
     e=window.event || e;
     
     if (e.target && typeof e.target.____________________no!='undefined') {
       var tete=e.target;
       tete.style.top='15px';
       if (typeof tete.______________y=='undefined') {
         tete.______________y=1;
         tete.onselectstart=function(){ return false; };
         tete.onclick=function(){ return false; };
         tete.onselect=function(){ return false; };
         tete.onmousedown=function(){ return false; };
         tete.style.cursor='default';
       }  
     }
     
     var ttt=_dragged.target;

     if (typeof ttt!='undefined') {
       var xx=0;
       var yy=0;
       if (typeof ttt.____________x!=='undefined')
         xx=ttt.____________x;
       if (typeof ttt.____________y!=='undefined')
         yy=ttt.____________y;

       if (typeof ttt.dragThis!='undefined')
         ttt=ttt.dragThis;
       
       var divider=1;
       var bl=ttt.offsetLeft;
       var by=ttt.offsetTop;
       
       ttt.style.left=(parseInt((e.clientX-_dragged.px+xx)/divider)*divider)+'px';
       ttt.style.top =(parseInt((e.clientY-_dragged.py+yy)/divider)*divider)+'px';
       var bl1=ttt.offsetLeft;
       var by1=ttt.offsetTop;
       if (typeof ttt.linked!=='undefined') {
         for (var z=0,zEnd=ttt.linked.length;z<zEnd;z++) {
           var ll=ttt.linked[z];
           ll.style.left=(ll.offsetLeft+(bl1-bl))+'px';
           ll.style.top =(ll.offsetTop+(by1-by))+'px';
         }
       }
     }
   }  
  }();
  
  IF.contentWindow.onmouseup=function(e){
   return function(e) {
     e=window.event || e;
     if (e.target && typeof e.target.____________________no!='undefined') {
       _dragged={px:0,py:0};
       var tete=e.target;
       if (typeof tete.dragThis!='undefined')
         tete=tete.dragThis;
       tete.____________x=parseInt(tete.style.left);
       tete.____________y=parseInt(tete.style.top);
     }  
   }  
  }();
  
  IF.contentWindow.onmousedown=function(e){
   alert('x');
   var origWMD=IF.contentWindow.onmousedown;           
   return function(e) {
     e=window.event || e;             
     if (origWMD) origWMD(e);
     alert(e);
     if (e.target && typeof e.target.____________________no!='undefined') {
       var trg=e.target;
       if (typeof trg.dragThis!='undefined')
         trg=trg.dragThis;
       if (trg.style.position!=='absolute')
         trg.style.position='absolute';
       _dragged.target=trg;
       _dragged.px=e.clientX;
       _dragged.py=e.clientY;
     }  
   }
  }();
  return void(0);
}


function ImgGrabb3r() {
  function n(o,p) {
    for (var z in p) {
      o.style[z]=p[z];
    }  
  }  
  var imgg=document.getElementsByTagName('img');
  var _doky=document.body.style.background;
  document.body.style.background='red';
  setTimeout(function(){document.body.style.background=_doky;},1000);
  var sid='_____________________glo_____ImgGrabb3r';
  var dd;
  dd=document.getElementById(sid);
  if (!dd)
    dd=document.createElement('div');
  n(dd,{
    id:sid,position:'absolute',left:'10px',top:'10px',width:'200px',
    heigh:'200px',overflow:'auto',background:'black',color:'white',
    border:'1px solid white',padding:'5px'
  })
  document.body.appendChild(dd);

  var s=[];
  for (var z=0,zEnd=imgg.length;z<zEnd;z++) {
    s.push('<a title="'+imgg[z].src+'" target="_blank" style="border:none;text-decoration:none;font-size:10px;font-family:arial;color:white" href="'+imgg[z].src+'"><img style="border:none" src="'+imgg[z].src+'" width="25" height="25"/></a>');
  }  
  dd.innerHTML=s.join(' ');
  return void(0);
}




//------------------------------------------
//------------------------------------------
function buildBookmarkletPage(cfg){
  /*
  cfg {
  icon
  name
  help
  }
  */
  var nameId=cfg.name+'_______________';
  return [
    '\'',
    '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"',
    '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
    '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">',
    '<head>',
    '<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />',
    '<title>'+cfg.name+'</title>',
    '<link rel="stylesheet" href="website/style.css" type="text/css" media="screen" />',
    '<link rel="icon" href="website/'+cfg.icon+'" type="image/png" />',
    '<meta name="generator" content="Seba" />',
    '<meta name="copyright" content="Sebastian Garcia" />',
    '</head>',  
    '<body>',
    '<div id="main">',
    '<div id="column2">',
    '<h3>Yummy!!</h3>',
    '<p>',
    'You have clicked a bookmarklet.<br />A bookmarklet is a little javascript application that lives in your bookmarks.',
    '</p>',
    '<h2>My name is '+cfg.name+',<br /> nice to meet you!</h2>',
    '<p class="cite">',cfg.help,
    '</p>',
    '<p>',
    'Drag my tab to your bookmarks toolbar to add me to your developing army.<br />', 
    'Alternatively, you can go back and drag my link to your bookmarks.',
    '<br /><br />',
    'Click my resulting button on any page to run me.<br /><br />',
    'After running, reload the page to restore it to its normal state.<br /><br />',
    '<a href="javascript:back()">Back to the future!</a>',
    '</p>',
    '</div>',
    '<img alt="Certified by the gato" src="website/sebin.png" style="position:absolute;left:50%;margin-top:50px;margin-left:-630px"/>',
    '<scr'+'ipt>',
    'window["'+nameId+'"]=0;',
    '</scr'+'ipt>',
    '\''
  ].join('').replace(/"/g,'\\\'');
} 
    

//------------------------------------------
//------------------------------------------
function buildBookmarklet(cfg){
  /*
  cfg {
  icon
  name
  help
  version
  debug
  }
  */
  var nameId=cfg.name+'_______________';  
  var buildBookmarkPageCondition='       if (window[\''+nameId+'\']) {';
  if (cfg.debug!==undefined && cfg.debug===true)
    buildBookmarkPageCondition='       if (0) {'; 
  
  document.write([
    '<li>',
    '  <img class="icon" alt="icon" src="website/'+cfg.icon+'" />',
    '  <span class="bookmarkletlink">',
    '  <a ',
    '     onmouseup="window[\''+nameId+'\']=0" ',
    '     onmousedown="window[\''+nameId+'\']=0" ',
    '     onclick="window[\''+nameId+'\']=1" ',
    '     href="javascript:',
    buildBookmarkPageCondition,
              buildBookmarkletPage(cfg),
    '       } else {',
              '(function(){ ',
                'window[\''+nameId+'\']=0;',
                'return ',
                (window[cfg.name]+'').replace(/"/g,"'"),
              '})()();void(0);',  
    '       }',  
    '  ">'+cfg.name+'</a>&nbsp;v'+cfg.version,  
    '</span>',
    '</li>'
  ].join(''));
}

