//---------------------------------------------------------------------
// ianimeEx.js (default extension for ianime.js)
//   version 0.28 (Dec. 13th, 2007)
// Copyright (c) 2007-2008 Satoshi Nakajima
//
// See ianime.js regarding the MIT License
//---------------------------------------------------------------------

(function(){

iAnime.effects.fadeout = function(element, t, x, y) {
    this.move(element, t, x, y);
    iBrowse.setOpacity(element, 1-t);
};

iAnime.effects.fadein = function(element, t, x, y) {
    this.move(element, t, x, y);
    iBrowse.setOpacity(element, t);
};

iAnime.effects.show = function(element, t, x, y) {
    this.move(element, t, x, y);
    element.style.visibility = 'visible';
};

iAnime.effects.hide = function(element, t, x, y) {
    element.style.visibility = 'hidden';
    this.move(element, t, x, y);
};

iAnime.effects.blink = function(element, t, x, y) {
    this.move(element, t, x, y);
    var index = Math.floor(t * ((this.x_times-1)*2+1)) % 2;
    element.style.visibility = iAnime.effects.blink.visibility[index];
};

iAnime.effects.blink.visibility = ['hidden', 'visible'];

iAnime.effects.bounce = function(e,t,x,y) {
    // micro second faster than this.move(element, t, x, y);
    e.style.left = x + "px";
    e.style.top = y + "px";
};

iAnime.effects.bounce.ratio = function(t) {
    if (t<0.66)
        return (t*t/0.66/0.66);
    var r0;
    if (t<0.88)
        r0 = 2*(0.11-(t-0.77)*(t-0.77)/0.11);
    else if (t<0.96)
        r0 = (0.04-(t-0.92)*(t-0.92)/0.04);
    else
        r0 = (0.02-(t-0.98)*(t-0.98)/0.02);
    return 1-r0;
};

iAnime.effects.easein = function(e,t,x,y) {
    // micro second faster than this.move(element, t, x, y);
    e.style.left = x + "px";
    e.style.top = y + "px";
};

iAnime.effects.easein.ratio = function(t) {
    return t*t;
};

iAnime.effects.easeout = function(e,t,x,y) {
    // micro second faster than this.move(element, t, x, y);
    e.style.left = x + "px";
    e.style.top = y + "px";
};

iAnime.effects.easeout.ratio = function(t) {
    return 1-(1-t)*(1-t);
};

iAnime.effects.easeinout = function(e,t,x,y) {
    // micro second faster than this.move(element, t, x, y);
    e.style.left = x + "px";
    e.style.top = y + "px";
};

iAnime.effects.easeinout.ratio = function(t) {
    if (t<0.5)
        return t*t/0.5;
    return 1-(1-t)*(1-t)/0.5;
};

iAnime.effects.elastic = function(e,t,x,y) {
    // micro second faster than this.move(element, t, x, y);
    e.style.left = x + "px";
    e.style.top = y + "px";
};

iAnime.effects.elastic.ratio = function(t) {
    return 1-(1-t)*(1-t)*Math.cos(Math.PI*3.5*t);
};

iAnime.effects.jump = function(e,t,x,y) {
    var h = (1-4*(t-0.5)*(t-0.5));
    e.style.left = x + this.j_x * h + "px";
    e.style.top = y + this.j_y * h + "px";
};

iAnime.effects.jump.init = function() {
    var dist = Math.sqrt(this._dx*this._dx + this._dy*this._dy);
    this.j_x = -this._dy * this.x_height / dist;
    this.j_y = this._dx * this.x_height / dist;
};

iAnime.effects.jumpbounce = function(e,t,x,y) {
    var r0;
    if (t<0.66)
        h = 1-(t-0.33)*(t-0.33)/0.33/0.33;
    else if (t<0.88)
        h = (1-(t-0.77)*(t-0.77)/0.11/0.11)/2;
    else if (t<0.96)
        h = (1-(t-0.92)*(t-0.92)/0.04/0.04)/4;
    else
        h = (1-(t-0.98)*(t-0.98)/0.02/0.02)/8;
    e.style.left = x + this.j_x * h + "px";
    e.style.top = y + this.j_y * h + "px";
};

iAnime.effects.jumpbounce.init = iAnime.effects.jump.init;

iAnime.effects.settext = function(element, t, x, y) {
    var text = this.text;
    iBrowse.setText(element, text.substr(0, Math.floor(text.length*t)));
    this.move(element, t, x, y);
};

iAnime.effects.style = function(element, t, x, y) {
    this.move(element, t, x, y);
    for (var i in this.x_styles) {
        this.x_styles[i](element, t);
    }
};

var patterns = [
    {
        match:/#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/, 
        push:function(key,ret1,ret2) {
            var r0 = parseInt(ret1[1],16);
            var r1 = parseInt(ret2[1],16) - r0;
            var g0 = parseInt(ret1[2],16);
            var g1 = parseInt(ret2[2],16) - g0;
            var b0 = parseInt(ret1[3],16);
            var b1 = parseInt(ret2[3],16) - b0;
            return function(e, t) {
                e.style[key] = "rgb(" 
       	            + Math.floor(r0+t*r1) + "," +
                    + Math.floor(g0+t*g1) + "," +
                    + Math.floor(b0+t*b1) + ")";
            };
        } 
    },
    {
        match:/([\d\.\-]+)([a-z]+)/, 
        push:function(key,ret1,ret2) {
            var from = parseInt(ret1[1]); 
            var delta = parseInt(ret2[1]) - from; 
            var suffix = ret1[2];
            return function(e, t) {
                e.style[key] = (from + t*delta) + suffix; 
            };
        } 
    }
];

function _cap(str)
{
   return str.charAt(1).toUpperCase();
}

// Converts "font-size" to "fontSize"
function _prop(str)
{
   return str.replace(/-[a-z]/g, _cap);    
}

iAnime.effects.style.init = function(item) {
    this.x_styles = [];
    for (var key in this.from) {
        var val1 = this.from[key];
        var val2 = this.to[key];
        if (val2 == null) continue;
        for (var i in patterns) {
            var ret1, ret2;
            ret1 = val1.match(patterns[i].match);
            ret2 = val2.match(patterns[i].match);
            if (ret1 != null && ret2 != null) {
                this.x_styles.push(patterns[i].push(_prop(key), ret1, ret2));
                break;
            }
        }
    }
}

})();

