// ==UserScript==
// @name          Google Image Search
// @namespace     http://www.serendip.ws/
// @description   Adds layer to show a google image search result.
// @include       http://images.google.com/*
// @include       http://images.google.co.jp/*
// @exclude       
// ==/UserScript==
// file created in 2008/07/03 13:34:09.
// LastUpdated :2008/10/03 17:40:37.
// author iNo
//

(function () {

// load jquery library
var GM_JQ = document.createElement('SCRIPT');
GM_JQ.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js';
GM_JQ.type= 'text/javascript';
var head = document.getElementsByTagName('HEAD')[0];
head.appendChild(GM_JQ);

function GM_wait() {
    if (typeof unsafeWindow.jQuery == 'undefined') {
        window.setTimeout(GM_wait, 100);
    } else {
        $ = unsafeWindow.jQuery;
        GM_setGoogleImageSearchStyle();
        GM_setShowLayerFunction();
        GM_setOpenImageFunction();
    }
}

GM_wait();

function GM_setGoogleImageSearchStyle() {
    // Size of navigation anchor's text
    const FONT_SIZE = "30px";
    const MARGIN = "0 10px";
    $('#navbar a[href]').css({
        "font-size" : FONT_SIZE,
        "margin" : MARGIN
    });
    $('.i').css({
        "font-size" : FONT_SIZE,
        "margin" : MARGIN
    });
}

function GM_setShowLayerFunction() {
    var ancs = document.evaluate('//a[@href]',
            document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    var ancsLen = ancs.snapshotLength;
    for (var i=0; i<ancsLen; i++) {
        var anc = ancs.snapshotItem(i);
        var href = anc.getAttribute('HREF');
        var uriRE = /\?imgurl=(http\:\/\/[^\&]*)\&/;
        if (href.match(uriRE)) {
            var uri = RegExp.$1;
            var id = 'serendip-gisl-' + new Date().getTime() + Math.round(Math.random() * 1000);
            anc.setAttribute('HREF', 'javascript:serendipGislOpenImage("' + uri + '", "' + id + '");');
            anc.id = id;
            var aAnc = document.createElement('A');
            aAnc.setAttribute('HREF', href);
            aAnc.setAttribute('TARGET', '_blank');
            aAnc.appendChild(document.createTextNode('IMAGE SEARCH'));
            anc.parentNode.appendChild(document.createElement('BR'));
            anc.parentNode.appendChild(aAnc);
        }
    }
}

function GM_setOpenImageFunction() {
    var script = document.createElement('SCRIPT');
    script.type = 'text/javascript';
    script.innerHTML = 'serendipGislOpenImage = ' + (function(file, id) {
        const LAYER_ID = 'serendip-gisl-layer';
        const IMG_ID = 'serendip-gisl-img';
        const LOADING_ID = 'serendip-gisl-loading';
        const LAYER_WIDTH = 9000;
        const LAYER_HEIGHT = 9000;
        const ANIMATION_SPEED = 'normal';
        var width = document.body.clientWidth;
        var height = document.body.clientHeight;
        if (!document.getElementById(LAYER_ID)) {
            var layer = document.createElement('DIV');
            layer.id = LAYER_ID;
            layer.style.display = 'none';
            layer.style.position = 'fixed';
            layer.style.top = 0;
            layer.style.left = 0;
            layer.style.width = LAYER_WIDTH + 'px';
            layer.style.height = LAYER_HEIGHT + 'px';
            layer.style.color = '#fff';
            layer.style.backgroundImage = 'url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB4AAAAeCAYAAAA7MK6iAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAABR0RVh0Q3JlYXRpb24gVGltZQAwOC43LjPrJVbAAAAAHHRFWHRTb2Z0d2FyZQBBZG9iZSBGaXJld29ya3MgQ1MzmNZGAwAAACxJREFUSIntzTEBAAAIA6Bp/5zmsIKfDxSgkkwe9EcqFovFYrFYLBaLxeKbBXIgASIpqO/TAAAAAElFTkSuQmCC)';
            layer.style.zIndex = 1000;
            layer.style.opacity = 1.0;
            $(layer).click(function() {
                $(this).fadeOut(ANIMATION_SPEED);
                $('#'+IMG_ID).fadeOut(ANIMATION_SPEED);
                $('#'+IMG_ID).remove();
                $('#'+LOADING_ID).remove();
            });
            $(document.body).append(layer);
        }
        var img = document.createElement('IMG');
        img.src = file;
        img.style.display = 'none';
        img.id = IMG_ID;
        $(img).css({
            "position" : "absolute",
            "top" : "0px",
            "left" : "0px",
            "border-width" : "2px",
            "border-style" : "solid",
            "border-color" : "#fff",
            "margin" : "5px",
            "z-index" : 1001
        });
        $(document.body).append(img);
        $('#'+LAYER_ID).append('<div id="' + LOADING_ID + '" style="color:#fff;font-size:50px;position:fixed;top:400px;left:' + (width-270)/2 + 'px;">Now Loading...</div>');
        $(img).load(function() {
            $(this).fadeIn(ANIMATION_SPEED);
            $('#'+LOADING_ID).remove();
            var w = $(this).width();
            var h = $(this).height();
            if (w < width) {
                $(this).css({
                    "margin-left" : (width - w) / 2 + "px"
                });
            } else {
                var origWidth = img.width;
                img.width = width - 20;
            }
            $(this).click(function() {
                $('#'+LAYER_ID).fadeOut(ANIMATION_SPEED);
                $(this).fadeOut(ANIMATION_SPEED);
                $(this).remove();
                $('#'+LOADING_ID).remove();
            });
            var cheight = document.body.clientHeight;
            var sTop = $(document).scrollTop();
            if (h < cheight) {
                $(img).css({
                    "margin-top" : sTop + ((cheight - h) / 2) + "px"
                });
            } else {
                $(img).css({
                    "margin-top" : sTop + 5 + "px"
                });
            }
            $("img", '#'+id).css({
                "outline-width" : "2px",
                "outline-style" : "dashed",
                "outline-color" : "purple"
            });
        });
        $(img).error(function() {
            $('#'+LOADING_ID).css({
                "color": "yellow",
                "font-size": "50px"
            });
            $('#'+LOADING_ID).html('Load Error!');
            $('#'+id).css({
                "outline-width" : "2px",
                "outline-style" : "dashed",
                "outline-color" : "purple",
                "opacity" : 0.2
            });
        });
        $('#'+LAYER_ID).fadeIn(ANIMATION_SPEED);
    });
    $(document.body).append(script);
}

})();

/*
vim:fdl=0 fdm=marker:ts=4 sw=4 sts=0:
*/

