// 画面に天使の羽根が舞う

var feather_params = new Array();

/* ------------------------------------
　羽のデータ
------------------------------------ */
function Feather (obj) {
    this.y = Math.random() * (feathers_winheight() - 50);
    this.obj = obj;
    this.move = move_feather;
    this.reset = reset_feather;
    this.reset();
}

/* ------------------------------------
　羽データ再初期化
------------------------------------ */
function reset_feather () {
    this.dx = 0;
    this.x = Math.random() * (feathers_winwidth() - 50);
    this.swing = feathers_swing * 0.1 + 
        Math.random() * (feathers_swing * 0.9);
    this.xstep = 0.02 + Math.random() / 10;
    this.ystep = 1 + Math.random();
    if(feathers_direction == 1) this.ystep *= -1;
    this.wind = feathers_wind * Math.random();
    if(navigator.userAgent.indexOf("Gecko") > 0) {
        this.ystep *= 2;
        this.wind  *= 2;
    }
}

/* ------------------------------------
　羽を動かす
------------------------------------ */
function move_feather () {
    this.y += this.ystep;
    this.x += this.wind;
    if(this.x < 0) this.x = feathers_winwidth()
        - (this.obj.width ? this.obj.width+1 : 50);
    else if(feathers_winwidth()
        - (this.obj.width ? this.obj.width+1 : 50) < this.x) this.x = 0;
    
    var f = 0;
    if(feathers_direction == 1 && this.y < 0) {
        this.y  = feathers_winheight()
            - (this.obj.height ? this.obj.height+1 : 50);
        this.reset();
    }
    else if (feathers_direction == 0 && 
        this.y > feathers_winheight()
            - (this.obj.height ? this.obj.height+1 : 50)) {
        this.y = 0;
        this.reset();
    }
    
    this.dx += this.xstep;
    this.obj.style.top  = (this.y + feathers_scrolltop()) + "px";
    var x = this.x + this.swing * Math.sin(this.dx);
    if(x > feathers_winwidth() - (this.obj.width ? this.obj.width+1 : 50))
        x = feathers_winwidth() - (this.obj.width ? this.obj.width+1 : 50);
    this.obj.style.left = (x + feathers_scrollleft()) + "px";
    if(this.obj.style.visibility == "hidden")
        this.obj.style.visibility = "visible";
}

/* ------------------------------------
　初期化
------------------------------------ */
function feathers_start () {
    if(!document.getElementById) return;
    
    if(feathers_speed < 10) feathers_speed = 10;
    if(navigator.userAgent.indexOf("Gecko") > 0) feathers_bg = 0;
    
    var div = document.getElementById(feathers_id);
    if(!div) { setTimeout("feathers_start()", 500); return; }
    
    var i, j = 0;
    for(i = 0; i < div.childNodes.length ;i++) {
        var img = div.childNodes[i];
        if(img.nodeName != "IMG") continue;
        var img2 = document.createElement("IMG");
        img2.src = img.src;
        img2.style.position = "absolute";
        img2.style.visibility = "hidden";
        img2.style.left = "0px";
        img2.style.top = "0px";
        // 背景に降らせる効果
        if(feathers_bg) img2.style.zIndex = -1;
        if(img.width) img2.width = img.width;
        if(img.height) img2.height = img.height;
        document.body.appendChild(img2);
        feather_params[j] = new Feather(img2);
        j++;
    }
    feathers_timer();
}
/* ------------------------------------
　アニメーション処理
------------------------------------ */
function feathers_timer () {
    for(i = 0; i < feather_params.length; i++)
        feather_params[i].move();
    setTimeout("feathers_timer();", feathers_speed);
}

/* ------------------------------------
　ウィンドウの幅取得
------------------------------------ */
function feathers_winwidth () {
    if(document.compatMode == "CSS1Compat") {
        return document.body.parentNode.clientWidth;
    }
    else if(document.body && document.body.clientWidth)
        return document.body.clientWidth;
    else if(self.innerWidth)
        return self.innerWidth;
    return 400;
}

/* ------------------------------------
　ウィンドウの高さ取得
------------------------------------ */
function feathers_winheight () {
    if(document.compatMode == "CSS1Compat") {
        return document.body.parentNode.clientHeight;
    }
    else if(document.body && document.body.clientHeight)
        return document.body.clientHeight;
    else if(self.innerHeight)
        return self.innerHeight;
    return 600;
}

/* ------------------------------------
　スクロール領域の上端
------------------------------------ */
function feathers_scrolltop () {
    if(document.compatMode == "CSS1Compat") {
        return document.body.parentNode.scrollTop;
    }
    else if(document.body && document.body.scrollTop) {
        return document.body.scrollTop;
    }
    else if(self.pageYOffset)
        return self.pageYOffset;
    return 0;
}

/* ------------------------------------
　スクロール領域の左端
------------------------------------ */
function feathers_scrollleft () {
    if(document.compatMode == "CSS1Compat") {
        return document.body.parentNode.scrollLeft;
    }
    else if(document.body && document.body.scrollLeft) {
        return document.body.scrollLeft;
    }
    else if(self.pageXOffset)
        return self.pageXOffset;
    return 0;
}
