// ==UserScript==
// @name         Google Image Search Inline Loader
// @description  Display Google Image Search images after clicking on thumbnail
// @include      http://images.google.*
// @include      http://google.*/images?*
// @include      http://www.google.*/images?*
// @namespace    http://iescripts.org
// @date         30-5-2010
// @version      1.4.1
// @author       Protector one
// ==/UserScript==

(function(){

  if (window.giilP1)
    return;
  else
    window.giilP1 = true;

  function onloaderFactory(ob){
    return function(){
      if(this.src==this.target){
        this.loading = false;
        document.body.style.cursor = 'auto';
        ob.cursor='auto';
        if ((document.body.clientWidth-10)<this.width)
          document.getElementById('inlineP1').style.left = 5;
      }
    };
  }
  
  function onerrorFactory(ob){
    return function(){
      this.loading = false;
      document.body.style.cursor = 'auto';
      ob.cursor='auto';
    };
  }

  function onclicker(){
    var ims, imc = document.getElementById('inlineP1'),
        url = this.href;
    if (url.match(/^javascript:/))
      url = url.match(/.*?'(.*)'/)[1];
    if ((imc.loading) || (!imc.disping && imc.target == url)){
      imc.style.display = 'none';
      imc.style.cursor = 'auto';
      document.body.style.cursor = 'auto';
      imc.src = '';
      imc.target = '';
      return false;
    }
    imc.target = url;
    this.style.cursor = 'wait'
    document.body.style.cursor = 'wait'
    imc.onload = onloaderFactory(this.style);
    imc.onerror = onerrorFactory(this.style);
    imc.src = this.firstChild.src;
    imc.loading = true;
    imc.src = url;
    this.href = 'javascript:\''+url+'\';void(0);';
    ims = imc.style;
    ims.display = 'block'; 
    imc.disping = true;
    ims.top = (5 + document.documentElement.scrollTop) + 'px';
    if (document.body.clientWidth/2 < event.clientX){
      ims.left = '5px';
      ims.right = '';
    }else{
      ims.right = '5px';
      ims.left = '';
    }
    return false;
  }
  
  function main(){
    var res, url,
        ni = document.createElement('img'),
        si = ni.style;
        im = document.getElementById('tDataImage0'),
        ul = document.getElementsByTagName('cite'),
        inx = 0;
    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';
    si.zIndex = 9999;
    document.body.appendChild(ni);
    while(im && ul[inx]){
      url = im.firstChild.href;
      res = url.match(/\/?imgurl\=(.*?)&/ );
      res = res[1];
      ul[inx].outerHTML = '<a href="'+url+'">'+ul[inx].outerHTML+'</a>';
      im.firstChild.href = res;
      im.firstChild.onclick = onclicker;
      ++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 = '';
        tmp.target = '';
      }
    }
  }
  main();
  main = null;

})();

