// ==UserScript==
// @name         Google Image Search Inline Loader
// @description  Display Google Image Search images after clicking on thumbnail
// @include      http://images.google.*
// @namespace    http://iescripts.org
// @date         28-5-2009
// @version      1.0
// @author       Protector one
// ==/UserScript==


(function(){
  var ni = document.createElement('img');
  var si = ni.style;
  ni.id = 'inlineP1';
  ni.disping = false;
  ni.onclick = function(){
    this.style.display = 'none';
    this.disping = false;
    this.src = '';
  };
  si.display = 'none';
  si.position = 'absolute';
  si.top = '5px';
  si.right = '5px';
  document.body.appendChild(ni);
  var im = document.getElementById('tDataImage0');
  var ul = document.getElementById('ImgContent').getElementsByTagName('span');
  var inx = 0;
  //var res;
  while(im && ul[inx]){
    var url = im.firstChild.href;
    var res = url.match(/\/?imgurl\=(.*?)&/ );
    if(!res){alert(im+'\n'+ul+'\n'+url); return;}
    res = res[1];
    ul[inx].outerHTML = '<a href="'+url+'">'+ul[inx].outerHTML+'</a>';
    im.firstChild.href = res;
    im.firstChild.onclick = function(){
      var imc = document.getElementById('inlineP1');
      var self = this;
      //if(imc.target == this.href) return false;
      imc.target = this.href;
      this.style.cursor = 'wait'
      document.body.style.cursor = 'wait'
      imc.onload = function(){if(this.src==this.target){document.body.style.cursor = 'auto'; self.style.cursor='auto';}};
      imc.onerror = function(){document.body.style.cursor = 'auto'; self.style.cursor='auto';};
      imc.src = this.firstChild.src;
      imc.src = this.href;
      var ims = imc.style;
      ims.display = 'block'; 
      imc.disping = true;
      ims.top = (5 + document.body.scrollTop) + 'px';
      if (document.body.clientWidth/2 < event.clientX){
        ims.left = '5px';
        ims.right = '';
      }else{
        ims.right = '5px';
        ims.left = '';
      }
      return false;
    };
    ++inx;
    im = document.getElementById('tDataImage'+inx);
  }
  document.body.onclick = function(){
    var tmp = document.getElementById('inlineP1');
    if (tmp.disping){
      tmp.disping = false;
    }else{
      tmp.style.display = 'none';
      document.body.style.cursor = 'auto';
      tmp.src = '';
    }
  }

})();
