var imgsrcs = [
    'https://secure02.chicappa.jp/~weblike.jp-kamuju/_www-uranai/common/images/flower.gif', // nozomu
    'https://secure02.chicappa.jp/~weblike.jp-kamuju/_www-uranai/common/images/flower.gif', // matoi
    'https://secure02.chicappa.jp/~weblike.jp-kamuju/_www-uranai/common/images/flower.gif', // takashi
    'https://secure02.chicappa.jp/~weblike.jp-kamuju/_www-uranai/common/images/flower.gif', // kyabajo
    'https://secure02.chicappa.jp/~weblike.jp-kamuju/_www-uranai/common/images/flower.gif', // danna
    'https://secure02.chicappa.jp/~weblike.jp-kamuju/_www-uranai/common/images/flower.gif', // yome
    'https://secure02.chicappa.jp/~weblike.jp-kamuju/_www-uranai/common/images/cursor.gif'  // maedax
];

// utility functions.
function log(msg) {
    return;
    var logger = document.getElementById('logger');
    logger.innerhtml = msg + '<br>' + logger.innerhtml;
}
function createImage(src) {
    var img = document.createElement('img');
    img.src = src;
    img.style.position = 'absolute';
    img.style.left = document.body.clientWidth - 180 + 'px'; // 右上の方から(Quirks)
    img.style.top  = '-150px';
    document.body.appendChild(img);
    return img;
}

/**
 * class Timer(simple)
 * @see http://d.hatena.ne.jp/amachang/20060924/1159084608
 */
function Timer(interval) {
    this.interval = interval || 10;
    this.tasks = {};
    this.counts = {};
    this.periods = {};
    this.counter = 0;
    var self = this;
    this.tid = setInterval( function() {
        var f = self.tasks, c = self.counts, n = self.periods;
        for(var i in f) {
            if( --c[i] <= 0 ) {
                f[i]();
                c[i] = n[i];
            }
        }
    }, this.interval);
}
Timer.prototype.schedule = function (task, period) {
    this.tasks[this.counter] = task;
    this.counts[this.counter] = this.periods[this.counter] = Math.ceil(period / this.interval);
    this.counter++;
};

/**
 * class Stalker
 */
var timer = new Timer(100);
function Stalker (target, ele, coe) {
    this.target = target;
    this.ele = ele;
    this.coe = coe;
    this.curX = ele.offsetLeft; // 手抜き
    this.curY = ele.offsetTop;
    var self = this;
    timer.schedule( function() { self.stalk() }, 100);
}
Stalker.prototype.isMissingTarget = function() {
    return ( !this.target || this.target.curX == void(0) || this.target.curY == void(0) );
    //  0        ==  void(0) → false. 
    // null      ==  void(0) → true.
    // undefined ==  void(0) → true.
    // null      === void(0) → false.
    // undefined === void(0) → true.
    // @see http://ja.doukaku.org/comment/499/
}
Stalker.prototype.stalk = function() {
    if (this.isMissingTarget()) return;
    var destX = this.target.curX,
        destY = this.target.curY;
    this.curX = (destX-this.curX)*this.coe + this.curX;
    this.curY = (destY-this.curY)*this.coe + this.curY;
    this.ele.style.left = this.curX + 'px';
    this.ele.style.top  = this.curY + 'px';
};

// main 
window.onload = function() {

    var mouse = new Object();
    if (window.addEventListener) {
        document.addEventListener('mousemove', function(e) {
            mouse.curX = e.pageX - 170;
            mouse.curY = e.pageY - 135;
        }, false);
    } 
    else {
        document.attachEvent('onmousemove', function(e) {
            mouse.curX = document.documentElement.scrollLeft + e.clientX - 170;
            mouse.curY = document.documentElement.scrollTop  + e.clientY - 135;
        });
    }
    
    var target = mouse;
    for (var i=0; i<imgsrcs.length; i++) {
        var img = createImage(imgsrcs[i]);
        img.style.zIndex = 100 - i;
        target = new Stalker(target, img, 0.3 + 0.03*i); // 後の方を速くしている
    }


};